https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64996
Bug ID: 64996
Summary: UBsan check optimized out by -O
Product: gcc
Version: 4.9.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: sanitizer
Assignee: unassigned at gcc dot gnu.org
Reporter: sami.liedes at iki dot fi
CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
jakub at gcc dot gnu.org, kcc at gcc dot gnu.org
Consider this program:
#include "limits.h"
int main() {
int i=INT_MIN;
i--;
}
When compiled with -fsanitize=undefined -O0, ub is correctly detected:
$ gcc -fsanitize=undefined ub.c -o ub && ./ub
ub.c:4:10: runtime error: signed integer overflow: -2147483648 + -1 cannot be
represented in type 'int'
When compiled with -O, the entire check is optimized out, even if the program
still clearly exhibits ub:
$ gcc -fsanitize=undefined ub.c -o ub -O && ./ub
$ gcc -fsanitize=undefined ub.c -o ub.s -O -S
$ cat ub.s
.file "ub.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
rep ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Debian 4.9.2-10) 4.9.2"
.section .note.GNU-stack,"",@progbits
$