From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 73C4E3858D32; Tue, 18 Oct 2022 02:45:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 73C4E3858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666061140; bh=jb2YJlGMpMAmUF90s7bQh9huJWCoWftfXAMjYyBaAH0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wXKKPHs1lcT1ietmTBvTZGNdQhWWkuG0rEGV6P4wnb0nXCXmybdC9TkXo6K+BEZLX FXgQFZvMBbD5sgLne9SkCjO0aVOkZvNoCExcMed6KOll6fMvwG7nDkfUGqyu7BEJvC QYfuhvuezIc+1m5UlLo+h1vAZrjXOzECZDlFIcnQ= From: "geoffreydgr at icloud dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug analyzer/107289] - -Wanayzer-null-dereference false positive with f = *b Date: Tue, 18 Oct 2022 02:45:39 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed 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: ASSIGNED 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: cc Message-ID: In-Reply-To: References: 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 Geoffrey changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |geoffreydgr at icloud dot = com --- Comment #3 from Geoffrey --- (In reply to David Malcolm from comment #2) > I believe that the analyzer is considering the case where "d" is (somehow) > called from outside of "main", and thus not making the assumption that *b= =3D=3D > &a; seeing the compare with NULL, it considers the case that b could be N= ULL. >=20 > It's not yet clear to me that this case of d being called from outside > "main" is valid, or if it's always the case that d can only ever be called > from main. In addition, i changed the original case to the following one (only add specifier static to `d`) and Compiled it with gcc 12.1 with `-O0 -fanalyzer` ``` 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; } } static void d() { e(); c(); } int main(){ d(); } ``` 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 | static void d() { | | ^ | | | | | (1) entry to 'd' | 13 | e(); | | ~~~=20=20=20=20=20=20=20 | | | | | (2) calling 'e' from '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 | | 13 | e(); | | ~^~ | | | | | (7) returning to 'd' from 'e' | 14 | c(); | | ~~~ | | | | | (8) calling 'c' from 'd' | +--> 'void c()': events 9-10 | | 3 | void c () { | | ^ | | | | | (9) entry to 'c' | 4 | int f; | 5 | f =3D *b; | | ~~~~~~ | | | | | (10) dereference of NULL 'b' | ``` Static analyzer should not give the CWE-476 warning, and it should start analyzing from function main instead of function `d` because function `d` i= s a static function (it is visible only in this compile unit) and only called by function `main`. And i am wondering how gcc analyzer handles global variables?=