[cfe-dev] Strict overflow warning

Richtarsky, Martin martin.richtarsky at sap.com
Fri Sep 21 01:12:50 PDT 2012


Hi all,

a few days ago I ran across code that relied on signed overflow being defined. With gcc this worked fine, clang optimized and the program broke.

With gcc it is possible to specify -Wstrict-overflow=x to get a warning in cases where the compiler relies on undefined behaviour (which will usually happen only with -O2 or above). 

I tried this with clang, but got no warning (but clang seems to know the option). Am I  doing something wrong? 


overflow.cpp

#include <iostream>
using namespace std;

class A {
public:
    int func()
    {
        unsigned long &unsignedValue = (unsigned long&)signedValue;
        unsignedValue = 9223372036854775808ul;
        cout << "signedValue " << signedValue << endl;
        signedValue = -signedValue;
        if (signedValue > 0)
        {
            return 1;
        }
        return 0;
    }
private:
    long signedValue;
} ;

int main()
{
    return A().func();
}


clang++ -O3 -Wstrict-overflow=5 -o overflow overflow.cpp
--> produces no warning

./overflow
$? == 1 afterwards


Best regards,
Martin




More information about the cfe-dev mailing list