From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7E7D13858D20; Fri, 3 Feb 2023 12:08:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E7D13858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675426127; bh=LI56ZFY+BAJKgzp5Qu+ptNWeccQ1kYuaTwOXNc4OWFo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ck1uU3Y8w11vP3lF0p4g0yelmgwtcPl/zrkgYwIL+wSVk6Hq7YQA53G0jB++w/6e8 rB7TtSn3ar9KJ111o3IMR72625cvu60XCbJycEsXFGIqOOWfddDomhX2htpjQhYa92 KFBcUF5sTgh62U+t6xQzjY9rTtQYsfCJnA8k92BY= From: "jg at jguk dot org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/108646] nonnull attribute does not detect variables that are NULL being passed Date: Fri, 03 Feb 2023 12:08:47 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jg at jguk dot org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: DUPLICATE 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: 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=3D108646 --- Comment #3 from Jonny Grant --- (In reply to Jonathan Wakely from comment #2) > It already does detect it: >=20 > n.c: In function =E2=80=98test=E2=80=99: > n.c:6:2: warning: argument 1 null where non-null expected [-Wnonnull] > 6 | mem2(dest); > | ^~~~~~~~~~ > n.c:2:8: note: in a call to function =E2=80=98mem2=E2=80=99 declared =E2= =80=98nonnull=E2=80=99 > 2 | void * mem2(void *dest) __attribute__((nonnull)); > | ^~~~ >=20 > You need to enable optimization, otherwise there is no data flow analysis. >=20 > Without optimization, it detects it with -fanalyzer >=20 > n.c: In function =E2=80=98void test()=E2=80=99: > n.c:6:6: warning: use of NULL =E2=80=98dest=E2=80=99 where non-null expec= ted [CWE-476] > [-Wanalyzer-null-argument] > 6 | mem2(dest); > | ~~~~^~~~~~ > =E2=80=98void test()=E2=80=99: events 1-2 > | > | 5 | char *dest =3D NULL; > | | ^~~~ > | | | > | | (1) =E2=80=98dest=E2=80=99 is NULL > | 6 | mem2(dest); > | | ~~~~~~~~~~ > | | | > | | (2) argument 1 (=E2=80=98dest=E2=80=99) NULL where non-= null expected > | > n.c:2:8: note: argument 1 of =E2=80=98void* mem2(void*)=E2=80=99 must be = non-null > 2 | void * mem2(void *dest) __attribute__((nonnull)); > | ^~~~ >=20 >=20 > And it's also detected at runtime using -fsanitize=3Dundefined: >=20 > n.c:6:6: runtime error: null pointer passed as argument 1, which is decla= red > to never be null That's great it works with optimization, I should remember to always do that.(In reply to Jonathan Wakely from comment #2) > It already does detect it: >=20 > n.c: In function =E2=80=98test=E2=80=99: > n.c:6:2: warning: argument 1 null where non-null expected [-Wnonnull] > 6 | mem2(dest); > | ^~~~~~~~~~ > n.c:2:8: note: in a call to function =E2=80=98mem2=E2=80=99 declared =E2= =80=98nonnull=E2=80=99 > 2 | void * mem2(void *dest) __attribute__((nonnull)); > | ^~~~ >=20 > You need to enable optimization, otherwise there is no data flow analysis. >=20 > Without optimization, it detects it with -fanalyzer >=20 > n.c: In function =E2=80=98void test()=E2=80=99: > n.c:6:6: warning: use of NULL =E2=80=98dest=E2=80=99 where non-null expec= ted [CWE-476] > [-Wanalyzer-null-argument] > 6 | mem2(dest); > | ~~~~^~~~~~ > =E2=80=98void test()=E2=80=99: events 1-2 > | > | 5 | char *dest =3D NULL; > | | ^~~~ > | | | > | | (1) =E2=80=98dest=E2=80=99 is NULL > | 6 | mem2(dest); > | | ~~~~~~~~~~ > | | | > | | (2) argument 1 (=E2=80=98dest=E2=80=99) NULL where non-= null expected > | > n.c:2:8: note: argument 1 of =E2=80=98void* mem2(void*)=E2=80=99 must be = non-null > 2 | void * mem2(void *dest) __attribute__((nonnull)); > | ^~~~ >=20 >=20 > And it's also detected at runtime using -fsanitize=3Dundefined: >=20 > n.c:6:6: runtime error: null pointer passed as argument 1, which is decla= red > to never be null That's great. I should have remembered to add -O2 Is it worth -Wnonnull emitting a warning message that it needs optimization= to get the needed data flow analysis?=