This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/18635] use of uninitialised reference accepted in C++ front end
- From: "adam.rak at streamnovation dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 26 Nov 2010 00:30:36 +0000
- Subject: [Bug c++/18635] use of uninitialised reference accepted in C++ front end
- Auto-submitted: auto-generated
- References: <bug-18635-4@http.gcc.gnu.org/bugzilla/>
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.