public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "gimira8592 at aramask dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/110244] New: False error of vector memory leak
Date: Wed, 14 Jun 2023 01:34:30 +0000	[thread overview]
Message-ID: <bug-110244-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110244

            Bug ID: 110244
           Summary: False error of vector memory leak
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gimira8592 at aramask dot com
  Target Milestone: ---

Created attachment 55320
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55320&action=edit
the preprocessed file (*.i*) that triggers the bug, generated by adding
-save-temps

Version of GCC: (Ubuntu 12.2.0-17ubuntu1) 12.2.0
OS and Version: Ubuntu 23.04, Linux 6.2.0-20-generic
GCC configuration: -fanalyzer std=c++20

To trigger bug run this code

int main()
{
    std::vector<int> vec(2);
}

and when you try to compile it, you should see this error

leak of ‘vec.std::vector<int>::<anonymous>.std::_Vector_base<int,
std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int>
>::_Vector_impl_data::_M_start’ [CWE-401] [-Werror=analyzer-malloc-leak]

Looks like a bug in GCC's static analyzer, because that code certainly does not
leak. Interestingly it only happens with C++20

It still happens with the latest GCC versions: https://godbolt.org/z/o6GbGcMaP 

Apparently its trigger by the addition of constexpr to the ctor/dtor in C++20
(and beyond). 

Apparently this error does not seem to occur with std::string but it do occur
with data type like int, double, long and char and fails to compile.

If you just create an uninitialized vector or default initialize with brace, it
will compile without any error but if you set it size or initialize with a
value then it will give memory leak error.

To show that this is related to constexpr on the ctor/dtor, you can #define
_GLIBCXX20_CONSTEXPR before #including <vector> in the C++20 version.

If you do that to effectively "define away" the constexpr in the C++20 version,
the static analyzer wont fail. Of course that is very much UB and pretty
dangerous (as you are actively meddling with the internals of the library and
the compilers assumptions), but it shows the point.

             reply	other threads:[~2023-06-14  1:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-14  1:34 gimira8592 at aramask dot com [this message]
2023-06-14  1:37 ` [Bug analyzer/110244] False error of vector leaking memory pinskia at gcc dot gnu.org

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-110244-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).