Bug 31545 - No warning on missing return in if construct
Summary: No warning on missing return in if construct
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.1
: P3 major
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-12 10:47 UTC by Walter Schreppers
Modified: 2007-04-13 11:36 UTC (History)
2 users (show)

See Also:
Host: i686-apple-darwin8
Target: i686-apple-darwin8
Build: 5367
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Compile with any optimization (i used -O2) (279 bytes, text/plain)
2007-04-12 10:55 UTC, Walter Schreppers
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Walter Schreppers 2007-04-12 10:47:58 UTC
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
Comment 1 Walter Schreppers 2007-04-12 10:55:14 UTC
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!).
Comment 2 Drea Pinski 2007-04-13 08:47:04 UTC
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.
Comment 3 schreppers@gmail.com 2007-04-13 11:32:57 UTC
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.
>


Comment 4 Walter Schreppers 2007-04-13 11:36:32 UTC
Apparantly this has been fixed in some newer version of g++. Keep up the good work!