public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "fcontact at cuveland dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/108554] New: Warning "null pointer dereferece" raised when extracting a unique_ptr from a map and any "-O" flag
Date: Thu, 26 Jan 2023 09:13:35 +0000	[thread overview]
Message-ID: <bug-108554-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108554
           Summary: Warning "null pointer dereferece" raised when
                    extracting a unique_ptr from a map and any "-O" flag
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fcontact at cuveland dot de
  Target Milestone: ---

I found what I believe might be a false positive with g++ 12 when using the
"-Wnull-dereference" warning and a map of unique_ptr with any level of
optimisation.

This behavior does not show up in GCC 9, 10, and 11.

Small but complete example:

#include <map>
#include <memory>
#include <string>
#include <utility>

int main()
{
  // create and initialize a map
  std::map<std::string, std::unique_ptr<int>> my_map;
  my_map["my_key"] = std::make_unique<int>(1);

  auto it = my_map.find("my_key");
  if (it != my_map.end()) {
    // extract an item from the map
    auto item = std::move(my_map.extract(it).mapped());
    return *item;
  }
  return 0;
}

Compiled with:
g++-12 -O -Wnull-dereference <filename>

Compiler version:
g++ (Compiler-Explorer-Build-gcc--binutils-2.38) 12.2.0
(see https://godbolt.org/z/E9KhTT4f6)

             reply	other threads:[~2023-01-26  9:13 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-26  9:13 fcontact at cuveland dot de [this message]
2023-01-26  9:51 ` [Bug libstdc++/108554] " redi at gcc dot gnu.org
2023-01-26  9:56 ` fcontact at cuveland dot de
2023-01-26 10:11 ` redi at gcc dot gnu.org
2023-01-26 10:14 ` redi at gcc dot gnu.org
2023-01-26 13:38 ` cvs-commit at gcc dot gnu.org
2023-01-26 13:41 ` [Bug libstdc++/108554] [12 Regression] " redi at gcc dot gnu.org
2023-01-27 15:43 ` cvs-commit at gcc dot gnu.org
2023-03-28 23:35 ` cvs-commit at gcc dot gnu.org
2023-03-28 23:35 ` redi 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-108554-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).