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
--
Summary: No warning on missing return in if construct
Product: gcc
Version: 4.0.1
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: walter at schreppers dot com
GCC build triplet: 5367
GCC host triplet: i686-apple-darwin8
GCC target triplet: i686-apple-darwin8
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31545