egcs-19990714 C++ -O Creates Wrong Code on SPARC

Michael Arras arras@intertv.com
Thu Jul 15 13:05:00 GMT 1999


// The following C++ program demonstrates a bug in egcs-1990714
// and egcs-1.1.2 on Solaris 2.5, 2.6 and 7 SPARC.
//
// To see bug compile with `g++ -O optbug.cxx'.  The output
// should be "Catch 0" 4 times.  "Catch 1" should never be
// printed.
//
// The bug does not occur with egcs compiled on IRIX 6.5 MIPS or
// Linux 2.2 x86.
//
// When compiled with -O(1-3), value is set to true, even though
// f2() always throws an exception.
//
// When compiled with -O3 -fno-delayed-branch (or no optimization
// at all) the program works as it should.
//
// The loop and "++odd %2" are there so egcs does not optimize the
// bug out.  The bug was discovered in production code.

#include <iostream>
#include <exception>

int odd = 0;

void f1(void) throw(exception)
{
    if (++odd % 2)
        throw exception();
}

void f2(void) throw(exception)
{
    throw exception();     // Always
}

main(int c, char **v)
{
    for (int i = 0; i < 4; i++)
    {
        bool value = false;

        try
        {
            f1();
            f2();
            // Should never get here
            value = true;
            cout << "Try ";
        }
        catch (...)
        {
            cout << "Catch ";
        }

        // value should never be true
        cout << value << endl;
    }
}


More information about the Gcc-bugs mailing list