https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86761
Bug ID: 86761
Summary: Code corruption with missing pointer return
Product: gcc
Version: 8.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: r.j.dejong at student dot utwente.nl
Target Milestone: ---
Created attachment 44473
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44473&action=edit
g++ -O3 -save-temps bug.c
GCC version: 8.1.0 (Arch builds)
System: x86-64 and arm-none-eabi compilers
Compile options (x86): gcc -O3 -Wall bug.c
Command line:
hans-pc% gcc -O3 bug.c -Wall
bug.c: In function ‘setFlags’:
bug.c:7:1: warning: control reaches end of non-void function [-Wreturn-type]
hans-pc% ./a.out
0 == 0
1 == 1
2 == 2
hans-pc% g++ -O0 bug.c
bug.c: In function ‘uint8_t* setFlags(uint8_t)’:
bug.c:7:1: warning: no return statement in function returning non-void
[-Wreturn-type]
hans-pc% ./a.out
0 == 0
1 == 1
2 == 2
hans-pc% g++ -O1 bug.c
bug.c: In function ‘uint8_t* setFlags(uint8_t)’:
bug.c:7:1: warning: no return statement in function returning non-void
[-Wreturn-type]
hans-pc% ./a.out
hans-pc% # No output
hans-pc% g++ -O3 bug.c
bug.c: In function ‘uint8_t* setFlags(uint8_t)’:
bug.c:7:1: warning: no return statement in function returning non-void
[-Wreturn-type]
hans-pc% ./a.out
zsh: segmentation fault (core dumped) ./a.out
Comments:
Code runs fine on GCC, despite the warning.
Code runs only on G++ with no optimizations. Unpredictable fatal behaviour
occurs on various optimization levels.
Originally the function getFlags and setFlags were in a class, but outside a
class the fault still occurs.
When debugging in my application, I've seen non-terminating fixed length for
loops and if branches completely stripped from assembly with the offending
function call. Although the warning is a good pointer to this error, it was
hard to track down since the function "setFlags" was nested 3 levels down in
the code.
The code runs fine with GCC 7.x branches. I've been unable to test GCC 8.2 or
9.0 builds.