public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55767] New: flowing off end of function which returns a value isn't treated as an error by default
@ 2012-12-20 19:16 rui.maciel at gmail dot com
  2012-12-20 19:35 ` [Bug c++/55767] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: rui.maciel at gmail dot com @ 2012-12-20 19:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55767

             Bug #: 55767
           Summary: flowing off end of function which returns a value
                    isn't treated as an error by default
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: rui.maciel@gmail.com


Consider the following code:

<code>
#include <iostream>

int& foo() {}

int main(void)
{
    foo() = 1 + 1;

    std::cout << foo() << std::endl;

    return 0;
}
</code>

Function foo() returns a value, which is a reference to an int, and in spite of
no return statement being provided, g++ compiles the above code without
throwing any error.  It does throw a warning when compiling with -Wall.

In the C++ standard, in section 6.6.3, it is stated that "A return statement
without an expression can be used only in functions that do not return a
value".  It is also stated that "Flowing off the end of a function is
equivalent to a return with no value", following that "this results in
undefined behavior in a value-returning function."

In spite of this behavior being explicitly left in the standard as "undefined
behavior", this loophole contradicts other behavior specifications made by the
standard.  Even then, its definition of "permissible undefined behavior" the
standard also includes "terminating a translation or execution (with the
issuance of a diagnostic message)".

As the example above shows, by ignoring the situation completely without even
issuing any diagnostic message, g++ is opening the doors to results which
aren't easily explained or expected, which constitutes a problem.

I've noticed that clang++ throws a warning by default for this particular
example, and I've read reports that MSVC++ 2010 actually throws a compiler
error, which is the best possible result for this kind of problem.

It would be nice if g++ handled functions that flowed off the end as errors
instead of silently accepting them by default.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/55767] flowing off end of function which returns a value isn't treated as an error by default
  2012-12-20 19:16 [Bug c++/55767] New: flowing off end of function which returns a value isn't treated as an error by default rui.maciel at gmail dot com
@ 2012-12-20 19:35 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2012-12-20 19:35 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55767

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-12-20 19:34:55 UTC ---
It's not always possible to make it a hard error and refuse to compile the
code. The function might call a function that never returns (but isn't marked
with a noreturn attribute) or might be of the form:

  int& f(bool b) {
    if (b) {
      static int i;
      return i;
    }
  }

If this is never called with a false argument there's no problem. If it's never
called at all there's no problem.

Unless GCC's flow analysis is improved I think the most you can hope for is
enabling -Wreturn-type by default.

I use -Werror=return-type, the warning's there already, use it if you want it.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-12-20 19:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-20 19:16 [Bug c++/55767] New: flowing off end of function which returns a value isn't treated as an error by default rui.maciel at gmail dot com
2012-12-20 19:35 ` [Bug c++/55767] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).