Bug 94867 - [10 Regression] New (since gcc 8) false positive with -Wnull-dereference in very simple code
Summary: [10 Regression] New (since gcc 8) false positive with -Wnull-dereference in v...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.3.0
: P2 normal
Target Milestone: 11.0
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wnull-dereference
  Show dependency treegraph
 
Reported: 2020-04-30 00:25 UTC by Vadim Zeitlin
Modified: 2023-07-07 08:49 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work: 11.1.0, 8.4.0
Known to fail: 10.3.0, 10.5.0, 9.1.0, 9.3.0
Last reconfirmed: 2020-04-30 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vadim Zeitlin 2020-04-30 00:25:10 UTC
Consider:

% cat -n gcc-null-deref.cpp
     1  #include <vector>
     2
     3  void test()
     4  {
     5      std::vector<int> p;
     6      p.push_back(1);
     7      std::vector<int> q(p.size());
     8      q[0] = p[0];
     9  }
% g++-9 -O2 -c gcc-null-deref.cpp -Wnull-dereference
gcc-null-deref.cpp: In function ‘void test()’:
gcc-null-deref.cpp:8:10: warning: potential null pointer dereference [-Wnull-dereference]
    8 |     q[0] = p[0];


This is with gcc version 9.3.0 (Debian 9.3.0-11). It doesn't happen with g++-8 but does still happen with the latest version available in Debian which is gcc version 10.0.1 20200418 (experimental) [master revision 27c171775ab:4c277008be0:c5bac7d127f288fd2f8a1f15c3f30da5903141c6] (Debian 10-20200418-1)

FWIW it looks like -Wnull-dereference really doesn't like push_back() for whatever reason. Even initializing the vector with fixed size doesn't help to make the warning go away -- however doing this _and_ removing push_back() does help.
Comment 1 Richard Biener 2020-04-30 07:16:48 UTC
Confirmed, only warns with GCC 9 for me.
Comment 2 Patrick Palka 2020-04-30 18:43:37 UTC
I see a warning with GCC 10 when passed -O instead of -O2.
Comment 3 Martin Liška 2020-06-01 12:46:22 UTC
It's gone since r10-3311-gff6686d2e5f797d6.
Comment 4 Vadim Zeitlin 2020-06-01 13:09:50 UTC
(In reply to Martin Liška from comment #3)
> It's gone since r10-3311-gff6686d2e5f797d6.

This commit is included in releases/gcc-10.1.0 tag, but I still see the warning with the provided example when using g++ 10.1 with -O, as mentioned in comment #2, even though I indeed don't see it with -O2 any longer.
Comment 5 Martin Liška 2020-06-01 13:53:07 UTC
You are right, it started with r9-7156-g33579b59aaf02eb7.
Comment 6 Patrick Palka 2020-07-24 19:18:31 UTC
I see this warning with GCC 10.2 and with trunk when using the flags -O -std=c++14 -Wnull-dereference as well.  Should this be marked as a 9/10/11 regression then?
Comment 7 Richard Biener 2021-04-30 08:05:33 UTC
Confirmed on the GCC 10 branch.  GCC 11 seems to be unaffected though.
Comment 8 Richard Biener 2021-06-01 08:17:24 UTC
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
Comment 9 Richard Biener 2022-05-27 09:42:33 UTC
GCC 9 branch is being closed
Comment 10 Jakub Jelinek 2022-06-28 10:40:23 UTC
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Comment 11 Richard Biener 2023-07-07 08:49:54 UTC
Fixed in GCC 11.