public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "adl at gnu dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function
Date: Mon, 21 Jun 2021 11:48:22 +0000	[thread overview]
Message-ID: <bug-101150-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 101150
           Summary: null pointer dereference false positive disappears
                    when compiling an additional function
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adl at gnu dot org
  Target Milestone: ---

Created attachment 51042
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51042&action=edit
source code, plus the three preprocessed versions

I'm observing the following behavior with gcc-snapshot on Debian
unstable as well as when using "x86-64 gcc (trunk)" and "x86-64 gcc
(11.1)" on Godbolt.  This false positive is not reported by gcc 10.

% g++ --version | sed 1q
g++ (Debian 20210527-1) 12.0.0 20210527 (experimental) [master revision
262e75d22c3:7bb6b9b2f47:9d3a953ec4d2695e9a6bfa5f22655e2aea47a973]

% cat foo.cc
#include <vector>

#ifdef FOO
void foo(const std::vector<int>& other)
{
  std::vector<int> v;
  std::size_t sz = other.size();
  v.resize(sz);
  int i = 0;
  for (int o: other)
    v[i++] = o;
}
#endif

#ifdef BAR
void bar(const std::vector<int>& other)
{
  std::vector<int> v;
  unsigned sz = other.size();
  v.resize(sz);
  int i = 0;
  for (int o: other)
    v[i++] = o;
}
#endif

% g++ -O3 -Wnull-dereference -c foo.cc -DBAR
% g++ -O3 -Wnull-dereference -c foo.cc -DFOO -DBAR
% g++ -O3 -Wnull-dereference -c foo.cc -DFOO
In function 'void foo(const std::vector<int>&)':
cc1plus: warning: potential null pointer dereference [-Wnull-dereference]

The two functions differ only by the type of sz, and the warning
occurs only if foo() is compiled but bar() is not.


I *believe* the warning comes from the fact that if sz is 0, the data
pointer of v will still be nullptr after resize(), and that would
render v[i++]=o invalid.  However if sz is 0, the loop will not do
any iteration, so that's a false positive.

However I can't explain
- why changing size_t into unsigned makes the warning go away,
- why compiling the two functions makes the warning go away.

I was expecting the diagnostics about foo() to be independent of the presence
of bar(), and I was expecting to get the same diagnostics for both functions
(preferably none, but I understand it's only a "potential" issue)

             reply	other threads:[~2021-06-21 11:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-21 11:48 adl at gnu dot org [this message]
2021-06-22  0:11 ` [Bug tree-optimization/101150] " pinskia at gcc dot gnu.org
2021-07-28  7:07 ` [Bug tree-optimization/101150] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-01-20 12:55 ` rguenth at gcc dot gnu.org
2022-04-21  7:49 ` rguenth at gcc dot gnu.org
2023-03-23 18:05 ` [Bug tree-optimization/101150] [11/12/13 " pinskia at gcc dot gnu.org
2023-05-29 10:05 ` [Bug tree-optimization/101150] [11/12 " jakub 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-101150-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).