public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash.
@ 2023-03-31 18:34 contact at thunderperfectwitchcraft dot org
  2023-03-31 18:38 ` [Bug c++/109364] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: contact at thunderperfectwitchcraft dot org @ 2023-03-31 18:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

            Bug ID: 109364
           Summary: Missing return statement in a non void function gives
                    only a warning but produces a forced crash.
           Product: gcc
           Version: 13.0
               URL: https://www.reddit.com/r/gcc/comments/123ojov/no_retur
                    n_in_function_binary_crashes_with/
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: contact at thunderperfectwitchcraft dot org
  Target Milestone: ---

Calling

int Test()
{
        std::cout << "Test";
}

from main causes the program to crash, even though only a warning is given.
As pointed out in this thread
https://www.reddit.com/r/gcc/comments/123ojov/no_return_in_function_binary_crashes_with/
this is because a invalid instruction is returned to cause the crash to prevent
people from letting out the return statement. But here
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943
it is stated that it should be only a warning since it can work if the return
value isn't used anywhere. This is no longer the case, as there is no way to
bring this code to run as far as I can see. To reduce the confusion it should
be a error by default, everything else is only frustrating.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
@ 2023-03-31 18:38 ` pinskia at gcc dot gnu.org
  2023-03-31 18:40 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 18:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>it should be only a warning since it can work if the return value isn't used anywhere

No that is for C, for C++: "The point being that it's undefined behaviour to
/return/ from such a function, not to /write/ such a function."
See comment #5

*** This bug has been marked as a duplicate of bug 43943 ***

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
  2023-03-31 18:38 ` [Bug c++/109364] " pinskia at gcc dot gnu.org
@ 2023-03-31 18:40 ` pinskia at gcc dot gnu.org
  2023-03-31 22:17 ` contact at thunderperfectwitchcraft dot org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 18:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
For GCC 13+, you can use -funreachable-traps which is enabled at -Og at least.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
  2023-03-31 18:38 ` [Bug c++/109364] " pinskia at gcc dot gnu.org
  2023-03-31 18:40 ` pinskia at gcc dot gnu.org
@ 2023-03-31 22:17 ` contact at thunderperfectwitchcraft dot org
  2023-03-31 22:19 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: contact at thunderperfectwitchcraft dot org @ 2023-03-31 22:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #3 from contact at thunderperfectwitchcraft dot org ---
I'm not sure if you read the thread I linked: If the statements there are
correct, atm a instruction that causes a crash under any circumstances is
generated and returned if the statement is missing.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (2 preceding siblings ...)
  2023-03-31 22:17 ` contact at thunderperfectwitchcraft dot org
@ 2023-03-31 22:19 ` pinskia at gcc dot gnu.org
  2023-03-31 22:21 ` contact at thunderperfectwitchcraft dot org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 22:19 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to contact from comment #3)
> I'm not sure if you read the thread I linked: If the statements there are
> correct, atm a instruction that causes a crash under any circumstances is
> generated and returned if the statement is missing.

Yes the code is undefined so anything can happen.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (3 preceding siblings ...)
  2023-03-31 22:19 ` pinskia at gcc dot gnu.org
@ 2023-03-31 22:21 ` contact at thunderperfectwitchcraft dot org
  2023-03-31 22:37 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: contact at thunderperfectwitchcraft dot org @ 2023-03-31 22:21 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #5 from contact at thunderperfectwitchcraft dot org ---
As mentioned, it isn't anymore: According to the linked Thread in gcc 13 a
return value that contains a invalid instruction is generated on purpose if
there is no return statement, forcing a crash.
To my understanding, this is the only thing that can happen now.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (4 preceding siblings ...)
  2023-03-31 22:21 ` contact at thunderperfectwitchcraft dot org
@ 2023-03-31 22:37 ` pinskia at gcc dot gnu.org
  2023-03-31 22:46 ` contact at thunderperfectwitchcraft dot org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 22:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
As I will mention it again falling through from a function which has a non void
return type is undefined. So gcc thinks it is unreachable. With the option is
specify in comment #2, gcc 13 will cause a trap (usually an illegal instruction
but could be an abort call).

So there is nothing to change about gcc. Undefined code means just that if you
invoke undefined code, anything can happen.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (5 preceding siblings ...)
  2023-03-31 22:37 ` pinskia at gcc dot gnu.org
@ 2023-03-31 22:46 ` contact at thunderperfectwitchcraft dot org
  2023-03-31 22:54 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: contact at thunderperfectwitchcraft dot org @ 2023-03-31 22:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #7 from contact at thunderperfectwitchcraft dot org ---
I'm not sure if I understand you correct (as I'm not a native speaker): You say
that it crashes by chance because it is undefined behavior, right?

On reddit, I got this as a reply:
"I noticed that GCC13 seems to now add a purposeful invalid instruction to
functions that have a return type, like Clang does. So your program crashes
instead of attempting to continue with some trash return value."

Which wouldn't be by chance to my understanding. What is correct?

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (6 preceding siblings ...)
  2023-03-31 22:46 ` contact at thunderperfectwitchcraft dot org
@ 2023-03-31 22:54 ` pinskia at gcc dot gnu.org
  2023-03-31 22:57 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-31 22:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
That is because -funreachable-traps is also enabled at -O0. And disabled for
-O1 and above except for -Og. That changes all places where you either
__builtin_unreachable or places which gcc inserts that like this place, gcc
will cause a trap.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (7 preceding siblings ...)
  2023-03-31 22:54 ` pinskia at gcc dot gnu.org
@ 2023-03-31 22:57 ` redi at gcc dot gnu.org
  2023-03-31 23:00 ` contact at thunderperfectwitchcraft dot org
  2023-03-31 23:24 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-31 22:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is an intentional change in GCC 13, see PR 104642.

The comments in Bug 43943 describe old behaviour, things have changed.

The crash is not guaranteed though. The missing return is treated as
unreachable by the compiler. If the -funreachable-traps option is enabled, then
calls to that function will result in a trap (and crash). The
-funreachable-traps option is enabled by default for -O0 and -Og but at higher
optimization levels the code is not guaranteed to crash unless you explicitly
use -funreachable-traps.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (8 preceding siblings ...)
  2023-03-31 22:57 ` redi at gcc dot gnu.org
@ 2023-03-31 23:00 ` contact at thunderperfectwitchcraft dot org
  2023-03-31 23:24 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: contact at thunderperfectwitchcraft dot org @ 2023-03-31 23:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #10 from contact at thunderperfectwitchcraft dot org ---
Now I get it, thanks to you both.
Why not additionally make the -Werror=return-type option to default? Would make
it easier to detect and solve the issue, compared to a crashing binary.

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

* [Bug c++/109364] Missing return statement in a non void function gives only a warning but produces a forced crash.
  2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
                   ` (9 preceding siblings ...)
  2023-03-31 23:00 ` contact at thunderperfectwitchcraft dot org
@ 2023-03-31 23:24 ` redi at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: redi at gcc dot gnu.org @ 2023-03-31 23:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109364

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I already explained this on reddit, and it's already explained in PR 43943.

There are programs that are valid and must not give an error.

    int f() { }
    int main() { }

This never calls f() so there is no problem. -Werror=return-type would fail to
compile this valid program.

    int g(bool b) { if (b) return 1; }
    int main() { g(true); }

This program calls g(true) which is OK, it never calls g(false).
-Werror=return-type would fail to compile this valid program.

The C++ standard says these programs must be accepted without errors. What GCC
does is allow you to compile broken code, but it warns, and since GCC 13 it
tries to prevent completely unpredictable undefined behaviour if you
accidentally do fall off the end of a function without returning.

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

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

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-31 18:34 [Bug c++/109364] New: Missing return statement in a non void function gives only a warning but produces a forced crash contact at thunderperfectwitchcraft dot org
2023-03-31 18:38 ` [Bug c++/109364] " pinskia at gcc dot gnu.org
2023-03-31 18:40 ` pinskia at gcc dot gnu.org
2023-03-31 22:17 ` contact at thunderperfectwitchcraft dot org
2023-03-31 22:19 ` pinskia at gcc dot gnu.org
2023-03-31 22:21 ` contact at thunderperfectwitchcraft dot org
2023-03-31 22:37 ` pinskia at gcc dot gnu.org
2023-03-31 22:46 ` contact at thunderperfectwitchcraft dot org
2023-03-31 22:54 ` pinskia at gcc dot gnu.org
2023-03-31 22:57 ` redi at gcc dot gnu.org
2023-03-31 23:00 ` contact at thunderperfectwitchcraft dot org
2023-03-31 23:24 ` 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).