https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78891
Bug ID: 78891
Summary: Array out of bound access generates wrong code in O2
or higher optimizations
Product: gcc
Version: 5.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: gcc at fischerpaulsen dot dk
Target Milestone: ---
Created attachment 40391
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40391&action=edit
Faulty code
The included isSet function will return 1 without checking the contents of
array if compiled with optimization -O2 -O3 or -Os. Especially it will return 1
if array contains 16 0's - which is wrong.
It will work correctly if compiled with lower optimization.
It will also work correctly if
- the array is defined as unsigned char array[17];
or
- the expression array[index] == 0x0 is changed to *(array+index) == 0x0
Note that the code might access the element array[16] - which is outside the
array - but the contents of this is irrelvant for the result.
The command line used for compilation is
gcc -O2 -save-temps -Wall -Wextra -c prog.c
It was run on a standard ubuntu PC
uname -a
Linux cpa-ThinkPad-T530 4.4.0-53-generic #74-Ubuntu SMP Fri Dec 2 15:59:10 UTC
2016 x86_64 x86_64 x86_64 GNU/Linux
The problem has been observed on a number of gcc versions (e.g. cygwin on
windows 7 using gcc version 4.9.2)