[Bug c++/96181] New: Missing return statement now leads to crashes

arturo.laurenzi at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Jul 13 10:40:26 GMT 2020


            Bug ID: 96181
           Summary: Missing return statement now leads to crashes
           Product: gcc
           Version: 8.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arturo.laurenzi at gmail dot com
  Target Milestone: ---

Consider the simple code snippet where a function returning an int is missing a
return statement, BUT client code is actually not using the return value at all

#include <cstdio>

int glob = 1;

bool func(int i)
    printf("setting glob to %d.. \n", i);
    glob = i;
}  // missing return statement

int main()
    func(10);  // return value is not used!
    printf("cleanly exiting..\n");

G++ up to 7.5 would compile such code in a way that makes it run just fine
(remember, return value is actually ignored), even with optimizations turned
on. G++ 8.1 (and above) will instead omit the ret instruction inside the
assembly for func, causing the execution to crash systematically:

        .string "setting glob to %d.. \n"
        push    rbx
        mov     esi, edi
        mov     ebx, edi
        xor     eax, eax
        mov     edi, OFFSET FLAT:.LC0
        call    printf
        mov     DWORD PTR glob[rip], ebx
        sub     rsp, 8
        mov     edi, 10
        call    func(int)

Now, I understand the code snipped is probably broken. However, this change
breaks old code that would work just fine by ignoring the undefined return
value. Was this intentional? Why was it done, if so?


More information about the Gcc-bugs mailing list