Bug 108083 - Code with memory leak does not get triggered when I run the executable
Summary: Code with memory leak does not get triggered when I run the executable
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-12-13 14:07 UTC by stefanos
Modified: 2022-12-13 16:13 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description stefanos 2022-12-13 14:07:08 UTC
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?
Comment 1 stefanos 2022-12-13 14:17:29 UTC
I forgot to mention that if I try *directly* the following command

g++ -Wall -Wextra -Werror -Wpedantic -std=c++20 -g -g0 -fsanitize=address,undefined -D_GLIBCXX_DEBUG -fno-strict-aliasing -fwrapv -lm -o tmp src/tmp.cpp

in place of Makefile, it triggers the memory leak.
Comment 2 Jonathan Wakely 2022-12-13 15:04:20 UTC
(In reply to stefanos from comment #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

You're not compiling with sanitizers, only linking:

> ccache g++ obj/tmp.o -o bin/tmp -fno-strict-aliasing -fwrapv -lfmt -lm
> -fsanitize=address,undefined

Your makefile is incorrect.
Comment 3 stefanos 2022-12-13 16:13:12 UTC
(In reply to Jonathan Wakely from comment #2)
> (In reply to stefanos from comment #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
> 
> You're not compiling with sanitizers, only linking:
> 
> > ccache g++ obj/tmp.o -o bin/tmp -fno-strict-aliasing -fwrapv -lfmt -lm
> > -fsanitize=address,undefined
> 
> Your makefile is incorrect.

Even if I do so, it does not trigger the memory leak...unless I misunderstood you.


stefanos@debian:~/code/cpp/tmp $ make
ccache g++ -Wall -Wextra -Werror -Wpedantic -std=c++20 -g -Og -D_GLIBCXX_DEBUG -fsanitize=address,undefined  -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 
histefanos@debian:~/code/cpp/tmp $