This happens on other gcc versions too. It seems the checks on whether the necessary return statement is used fail when using an if statement. I included a little code snippet here. It compiles without warnings or errors (all optimizations and even with -Wall no warnings are given). When executed it crashes ofcourse because a function is called and return value expected which is never pushed onto stack: walter-schreppers-computer:~/ClassGen wschrep$ cat cppbug.cpp #include <iostream> using namespace std; class Test{ public: Test(){ fOk = false; } ~Test(){} string indent(){ return " "; } string faultyReturn(){ if( !fOk ) indent(); //forgot return here (but compiler does not warn me!) else return indent(); } private: bool fOk; }; int main(){ cout << "testing faulty class we forgot to return in member faultyReturn" << endl; cout << "this should not compile, but it does and then crashes on runtime!" << endl; Test t; cout << "'" << t.faultyReturn() << "'" << endl; return 0; } walter-schreppers-computer:~/ClassGen wschrep$ g++ --version i686-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367) Copyright (C) 2005 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. walter-schreppers-computer:~/ClassGen wschrep$ g++ -Wall -O2 cppbug.cpp -o cppbug walter-schreppers-computer:~/ClassGen wschrep$ ./cppbug testing faulty class we forgot to return in member faultyReturn this should not compile, but it does and then crashes! Bus error
Created attachment 13355 [details] Compile with any optimization (i used -O2) A similar type of bug occured to me a lot in the past. When using if's sometimes a warning is issued that a return statement is needed (even if it's not the case, or rather there is a return issued in every part of the if construct but compiler insists on writing another dummy return on the end of function just to get rid of the warning when using -Wall). Might be related to current problem where no warning or error is given when there really should be because code will crash on runtime because of the missing return statement (could be hard to find bug in a large project!).
This was already fixed: [pinskia-laptop:gcc/objdir-noboot/gcc] pinskia% ./cc1plus t5.ii -quiet -fdump-tree-all -W -Wall -O2 t5.cc: In member function 'std::string Test::faultyReturn()': t5.cc:16: warning: control reaches end of non-void function I forgot when it was fixed.
Subject: Re: No warning on missing return in if construct Ah ok, great! Hope the fixes arrive soon in darwin too :D On 13 Apr 2007 07:47:05 -0000, pinskia at gcc dot gnu dot org <gcc-bugzilla@gcc.gnu.org> wrote: > > > ------- Comment #2 from pinskia at gcc dot gnu dot org 2007-04-13 08:47 ------- > This was already fixed: > [pinskia-laptop:gcc/objdir-noboot/gcc] pinskia% ./cc1plus t5.ii -quiet > -fdump-tree-all -W -Wall -O2 > t5.cc: In member function 'std::string Test::faultyReturn()': > t5.cc:16: warning: control reaches end of non-void function > > > I forgot when it was fixed. > > > -- > > > http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31545 > > ------- You are receiving this mail because: ------- > You reported the bug, or are watching the reporter. >
Apparantly this has been fixed in some newer version of g++. Keep up the good work!