https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109050
Bug ID: 109050 Summary: UBsan failed to detect out-of-bound at -O0/1/2/s Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: sanitizer Assignee: unassigned at gcc dot gnu.org Reporter: shaohua.li at inf dot ethz.ch 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, marxin at gcc dot gnu.org Target Milestone: --- For the following code, UBsan failed to detect the out-of-bound access at all opt levels. Clang can detect it at all opt levels. Compiler explorer: https://godbolt.org/z/TK35hvPrK % cat a.c long a; int b; int main() { int c[4]={0, 1, 2, 3}; a = 0; for (; a <= 2; a++) c[a - 9806816] |= b; } % % gcc-tk -O0 -fsanitize=undefined a.c && ./a.out % % gcc-tk -O1 -fsanitize=undefined a.c && ./a.out % % clang -O1 -fsanitize=undefined a.c && ./a.out /a.c:7:5: runtime error: index -9806816 out of bounds for type 'int[4]' ... % gcc-tk -v Using built-in specs. COLLECT_GCC=gcc-tk COLLECT_LTO_WRAPPER=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-7e9dd9de169034810b92d47bf78284db731fa5da/libexec/gcc/x86_64-pc-linux-gnu/13.0.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure --disable-multilib --disable-bootstrap --enable-languages=c,c++ --prefix=/zdata/shaoli/compilers/ccbuilder-compilers/gcc-7e9dd9de169034810b92d47bf78284db731fa5da Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.0.1 20230221 (experimental) (GCC) %