https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108083
Bug ID: 108083 Summary: Code with memory leak does not get triggered when I run the executable Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: ssofroni at cytanet dot com.cy Target Milestone: --- I have a very weird behavior with gcc version 12.2.0 (Debian 12.2.0-9). My code is the following: #include <iostream> int main() { [[maybe_unused]] int x{64}; std::cout << new char[]{"hi"}; return 0; } With my Makefile, I generate an executable with the following steps: ccache g++ -Wall -Wextra -Werror -Wpedantic -std=c++20 -g -Og -D_GLIBCXX_DEBUG -I src -c src/tmp.cpp -o obj/tmp.o ccache g++ obj/tmp.o -o bin/tmp -fno-strict-aliasing -fwrapv -lfmt -lm -fsanitize=address,undefined If I execute `bin/tmp`, it prints 'hi' and does not trigger the sanitizer. If I replace `g++` to `clang++`, the generated executable triggers the sanitizer and catches the memory leak. Here's the output: hi ================================================================= ==12782==ERROR: LeakSanitizer: detected memory leaks Direct leak of 3 byte(s) in 1 object(s) allocated from: #0 0x55fafedff07d in operator new[](unsigned long) (/home/stefanos/code/cpp/tmp/bin/tmp+0xdf07d) (BuildId: 0903120ed7ac810b75b124e3d84396bbe7870f32) #1 0x55fafee0156a in main /home/stefanos/code/cpp/tmp/src/tmp.cpp:6:18 SUMMARY: AddressSanitizer: 3 byte(s) leaked in 1 allocation(s). To make GCC catch the leak, I either have to add a newline at the end of {"hi"} or add `std::flush;`: stefanos@debian:~/code/cpp/tmp $ cat src/tmp.cpp #include <iostream> int main() { [[maybe_unused]] int x{64}; std::cout << new char[]{"hi"} << '\n'; } stefanos@debian:~/code/cpp/tmp $ make ccache g++ -Wall -Wextra -Werror -Wpedantic -std=c++20 -g -Og -D_GLIBCXX_DEBUG -I src -c src/tmp.cpp -o obj/tmp.o ccache g++ obj/tmp.o -o bin/tmp -fno-strict-aliasing -fwrapv -lfmt -lm -fsanitize=address,undefined make -j4 --jobserver-auth=3,4 got executed in debug mode... stefanos@debian:~/code/cpp/tmp $ bin/tmp hi ================================================================= ==12975==ERROR: LeakSanitizer: detected memory leaks Direct leak of 3 byte(s) in 1 object(s) allocated from: #0 0x7f0437de7628 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98 #1 0x556a5b56e1bc in main src/tmp.cpp:6 SUMMARY: AddressSanitizer: 3 byte(s) leaked in 1 allocation(s). Am I doing something wrong?