From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AE460385829C; Mon, 17 Oct 2022 09:59:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE460385829C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666000750; bh=4k/JHmYL4Sd+HwUWXx5p4rQTaj3jEogPLZUovLXuPWw=; h=From:To:Subject:Date:From; b=xwk2LgfQmVpLOAmUKz9g2qkpYmSvqkN/fd0HuJW27yCIqCEyfbL2s7+IowC4oYJs+ 7Mu3CtWjTCGdgI4Miom5YEF/cg7CAkuYlTaw7R4kASOkFq78Ytaen6BGqTCtpqVLVf sRCpitF791NJLeiBeQBQFB0l3S6vhWJeCkKN/6XE= From: "geoffreydgr at icloud dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/107289] New: - -Wanayzer-null-dereference false positive with f = *b Date: Mon, 17 Oct 2022 09:59:10 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: analyzer X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: geoffreydgr at icloud dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: dmalcolm 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 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107289 Bug ID: 107289 Summary: - -Wanayzer-null-dereference false positive with f =3D *b Product: gcc Version: 12.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: analyzer Assignee: dmalcolm at gcc dot gnu.org Reporter: geoffreydgr at icloud dot com Target Milestone: --- I got a false positive error when compiling the following "minimal, complete and verifiable example (MCVE)" program ``` int a=3D1; int *b =3D &a; void c () { int f; f =3D *b; }=20=20=20 void e(){ if (0 =3D=3D b){ int *g =3D 0; } } void d() { e(); c();} int main(){ d(); } ``` Compiling the above code with gcc 12.1 with `-O0 -fanalyzer` in https://godbolt.org/ results in : ``` : In function 'void c()': :5:7: warning: dereference of NULL 'b' [CWE-476] [-Wanalyzer-null-dereference] 5 | f =3D *b; | ~~^~~~ 'void d()': events 1-2 | | 12 | void d() { e(); c();} | | ^ ~~~ | | | | | | | (2) calling 'e' from 'd' | | (1) entry to 'd' | +--> 'void e()': events 3-6 | | 7 | void e(){ | | ^ | | | | | (3) entry to 'e' | 8 | if (0 =3D=3D b){ | | ~~ | | | | | (4) following 'true' branch... | 9 | int *g =3D 0; | | ~ | | | | | (5) ...to here | | (6) 'b' is NULL | <------+ | 'void d()': events 7-8 | | 12 | void d() { e(); c();} | | ~^~ ~~~ | | | | | | | (8) calling 'c' from 'd' | | (7) returning to 'd' from 'e' | +--> 'void c()': events 9-10 | | 3 | void c () { | | ^ | | | | | (9) entry to 'c' | 4 | int f; | 5 | f =3D *b; | | ~~~~~~ | | | | | (10) dereference of NULL 'b' ``` It should not be a null pointer deference, because b is a pointer to a. GCC seems to be fooled by line 8 if (0 =3D=3D b){...}. But if I change the exa= mple program to the following one, the false positive error disappears: ``` int a=3D1; int *b =3D &a; void c () { int f; f =3D *b; }=20=20=20 void d() { c();} int main(){ if (0 =3D=3D b){ int *g =3D 0; } d(); } ```=