public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ssofroni at cytanet dot com.cy" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/108083] New: Code with memory leak does not get triggered when I run the executable
Date: Tue, 13 Dec 2022 14:07:08 +0000	[thread overview]
Message-ID: <bug-108083-4@http.gcc.gnu.org/bugzilla/> (raw)

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?

             reply	other threads:[~2022-12-13 14:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13 14:07 ssofroni at cytanet dot com.cy [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-108083-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).