2

I want to get some warnings if I use the following code:

class Danger
{   
    private:
        int* buff;

    public:
        Danger( int& _in): buff(&_in) {}
        int LetsCrash() { *buff=42; return 0;} 
};  

class DangerUser
{   
    private:
        int otherVar;

    public:
        DangerUser( Danger& d): otherVar( d.LetsCrash()) {}
};  

class Example
{   
    private:
        int x;
        DangerUser dangerUser;
        Danger danger;

    public:
        Example( ):
            dangerUser( danger ),
            danger( x)
    {}
};  

int main()
{   
    Example ex; 
}   

The code compiles with

g++ -O0 -Wall -pedantic -Wextra x.cpp

and also with

clang++ -O0 -Wall -pedantic -Wextra x.cpp

without any warning but crashes as expected.

I use gcc in version gcc (GCC) 6.2.1 20160916 (Red Hat 6.2.1-2) and also 6.1.0. Clang comes in version clang version 3.8.0 (tags/RELEASE_380/final).

I already red Warnings for uninitialized members disappear on the C++11 but this points to a gcc bug which was already fixed in the versions I use.

Because the bug is trivial to detect ( using of a variable which is not constructed at the point of usage ) I wonder why none of my compilers I use detect that problems.

Remark: Compiling in -O0 is only necessary to get the crash in real life :-) Otherwise the bug is optimized out.

Is there maybe an additional -Wxyz flag to get this warning alive?

Community
  • 1
  • 1
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • 1
    I'm not convinced this situation is "trivial to detect" since taking the address/reference to an uninitialized variable should be just fine (as long as it's constructed before you use the reference), so the check is not obviously simple. – Mark B Nov 28 '16 at 19:49

0 Answers0