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.
Confirmed, only warns with GCC 9 for me.
I see a warning with GCC 10 when passed -O instead of -O2.
It's gone since r10-3311-gff6686d2e5f797d6.
(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.
You are right, it started with r9-7156-g33579b59aaf02eb7.
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?
Confirmed on the GCC 10 branch. GCC 11 seems to be unaffected though.
GCC 9.4 is being released, retargeting bugs to GCC 9.5.
GCC 9 branch is being closed
GCC 10.4 is being released, retargeting bugs to GCC 10.5.
Fixed in GCC 11.