https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108029
Bug ID: 108029 Summary: GCC'ASAN at -O0 failed to detect a memory leak 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, GCC trunk at -O0 won't report the memory leak. However, -O1 and above can successfully report the leak. I checked the assembly code at -O0, which indeed allocated the memory. This issue seems to start from GCC-9, GCC-8 can detect the leak at all optimization levels. Clang can also detect it at -O0. Compiler explorer: https://godbolt.org/z/vxoxxY55x % cat a.c short a = 1; int main() { int *b = malloc(sizeof(int)); for (;a<1;a++) { int *c = &b[0]; free(b); int d[1]; int **e = malloc(sizeof &d); e[0] = &c; if (a || b[0]) { *c = 4 ^ *c; *c = 5; } } } % % gcc-tk -O0 -fsanitize=address a.c % ASAN_OPTIONS=detect_leaks=1 ./a.out % % gcc-tk -O1 -fsanitize=address a.c % ASAN_OPTIONS=detect_leaks=1 ./a.out ================================================================= ==2169810==ERROR: LeakSanitizer: detected memory leaks Direct leak of 4 byte(s) in 1 object(s) allocated from: #0 0x7fe9fc8a98bf in __interceptor_malloc ../../../../gcc/libsanitizer/asan/asan_malloc_linux.cpp:69 #1 0x401234 in main (/zdata/shaoli/sanitizertesting_UseAfterFree/mutate/bugs/gcc/a.out+0x401234) SUMMARY: AddressSanitizer: 4 byte(s) leaked in 1 allocation(s). %