public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/108083] New: Code with memory leak does not get triggered when I run the executable
@ 2022-12-13 14:07 ssofroni at cytanet dot com.cy
  2022-12-13 14:17 ` [Bug c++/108083] " ssofroni at cytanet dot com.cy
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: ssofroni at cytanet dot com.cy @ 2022-12-13 14:07 UTC (permalink / raw)
  To: gcc-bugs

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?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-12-13 16:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-13 14:07 [Bug c++/108083] New: Code with memory leak does not get triggered when I run the executable ssofroni at cytanet dot com.cy
2022-12-13 14:17 ` [Bug c++/108083] " ssofroni at cytanet dot com.cy
2022-12-13 15:04 ` redi at gcc dot gnu.org
2022-12-13 16:13 ` [Bug sanitizer/108083] " ssofroni at cytanet dot com.cy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).