public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/43943]  New: "warning: no return statement in function returning non-void" should be an error
@ 2010-04-29 20:13 david at rothlis dot net
  2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: david at rothlis dot net @ 2010-04-29 20:13 UTC (permalink / raw)
  To: gcc-bugs

I know that dozens of bugs have been opened for this over the years, but please
read this through.

If you have a C++ program missing a return statement from a function that is
supposed to return a value, g++ will compile it happily with no errors (or even
a warning, unless -Wreturn-type or -Wall is used). Trying to use the return
value from the function will most likely cause a segmentation fault. Dozens of
bugs have been opened for this over the years (e.g. bug 11474), all rejected as
invalid based on the following statement from section 6.6.3 of the C++
standard:

  Flowing off the end of a function is equivalent to a return with no value;
  this results in undefined behavior in a value-returning function.

However, the very same paragraph begins with:

  A return statement without an expression can be used only in functions 
  that do not return a value, that is, a function with the return type void,
  a constructor (12.1), or a destructor (12.4).

In my opinion, the standard contradicts itself. Either flowing off the end of a
function is "equivalent to a return with no value" (i.e. invalid according to
the beginning of the paragraph) or it is "undefined behaviour". It can't be
both.

Appendix C is informative rather than normative, but can help us understand the
intent of the standard. Section C.1.4 says:

  Change: It is now invalid to return (explicitly or implicitly) from a
  function which is declared to return a value without actually returning a
  value.
  Rationale: The caller and callee may assume fairly elaborate return-value
  mechanisms for the return of class objects. If some flow paths execute a
  return without specifying any value, the implementation must embody many more
  complications. Besides, promising to return a value of a given type, and then
  not returning such a value, has always been recognized to be a questionable
  practice, tolerated only because very-old C had no distinction between void
  functions and int functions.

The number of bugs opened for this issue over the years indicates how
surprising the current behaviour is. Since it is not always practical to enable
-Werror on large codebases, I think this should be an error by default.


-- 
           Summary: "warning: no return statement in function returning non-
                    void" should be an error
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: david at rothlis dot net


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


^ permalink raw reply	[flat|nested] 10+ messages in thread
[parent not found: <bug-43943-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2023-03-31 18:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-29 20:13 [Bug c++/43943] New: "warning: no return statement in function returning non-void" should be an error david at rothlis dot net
2010-04-29 20:32 ` [Bug c++/43943] " pinskia at gcc dot gnu dot org
2010-04-29 20:36 ` redi at gcc dot gnu dot org
2010-04-30  6:05 ` david at rothlis dot net
2010-04-30 11:22 ` redi at gcc dot gnu dot org
2010-04-30 11:26 ` redi at gcc dot gnu dot org
2010-04-30 14:01 ` david at rothlis dot net
     [not found] <bug-43943-4@http.gcc.gnu.org/bugzilla/>
2020-04-26  9:44 ` redi at gcc dot gnu.org
2022-04-19  9:55 ` kdevel at vogtner dot de
2023-03-31 18:38 ` pinskia 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).