This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/18635] use of uninitialised reference accepted in C++ front end


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18635

ÃdÃm RÃk <adam.rak at streamnovation dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |adam.rak at streamnovation
                   |                            |dot com

--- Comment #8 from ÃdÃm RÃk <adam.rak at streamnovation dot com> 2010-11-26 00:30:17 UTC ---
in g++-4.6 (and maybe all before) this bug can be even more troublesome:
struct AA
{
 int &a;
 AA() : a(a)
 {
 }
};

int main()
{
        AA aa;
        cout << &aa.a << endl;
        return 0;
}

compiled without a warning even with
g++ main.cpp -O3 -Wall -pedantic -Wextra -Winit-self -Wuninitialized

And in -O0 it prints some address, probably the address of the reference as
suggested before. But in -O1..3 it prints a 0, which means we made an
nullreference. 

The practical problem is that because of this, the code can be easily messed up
like this:

class AA
{
...int &aaa;

   AA(int& aaaa) : aaa(aaa) {...

A single typo and the compiled does really strange things, the segfault is best
case, sometimes the reference points a valid address. It is very hard to debug
too. And when the programmer checks the code he/she can naively think that the
compiler should check it, so "why bother checking whether they are spelled
exactly the same?"

The old testcase was a bit harder to do accidentally, this one can happen more
easily. A self-init warning might enough to clue the programmer if this
happens. An error would be better if we are sure this is invalid.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]