From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9AA8F3938C0F; Mon, 21 Jun 2021 11:48:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9AA8F3938C0F From: "adl at gnu dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: adl at gnu dot org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Jun 2021 11:48:22 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101150 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=3D51042&action=3Dedit 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 #ifdef FOO void foo(const std::vector& other) { std::vector v; std::size_t sz =3D other.size(); v.resize(sz); int i =3D 0; for (int o: other) v[i++] =3D o; } #endif #ifdef BAR void bar(const std::vector& other) { std::vector v; unsigned sz =3D other.size(); v.resize(sz); int i =3D 0; for (int o: other) v[i++] =3D 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&)': 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++]=3Do 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 presen= ce 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)=