WARNING: g++ version 4.x still has internal bugs!

Written by Walter

< >

This is where the g++ compiler should give an error or atleast a warning. Submitted this bug to gcc bugzilla : http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31545. I call a function faultyReturn which should return a string, but I forget (on purpose) to write return. This gives no warnings and no compile errors (even with -Wall ) but ofcourse this crashes on runtime. For this little example it is easy to locate and solve (just write return indent() instead of indent() ). But a correct C++ compiler is supposed to give an error here!

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();
      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!" << 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

Back to archive