From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C5F093858C52; Fri, 3 Feb 2023 09:23:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5F093858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675416233; bh=Ox34vd0zRhUlqxZBGKu00brYsby90kLcosM+3+IaQjo=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wey6Y3tRRSBKfG0Kza3vOpNq7HVT9YwbKFUdK8FUIPtS2Mb9eS5sTL0WMwUSZfyv1 ZbL+HW8DPN/B4U3VqQ6mj0hKXfR52Oa/7SS3jh3rMjVGziDbGoWU2Mms6mo2GVZzwu tYG+b1iRZUu+V8Ng54q1AT6FFeOPNLET5xzs8+gI= From: "redi at gcc dot gnu.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 09:23:53 +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: redi at gcc dot gnu.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 #2 from Jonathan Wakely --- It already does detect it: 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)); | ^~~~ You need to enable optimization, otherwise there is no data flow analysis. Without optimization, it detects it with -fanalyzer 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 expecte= d [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-nu= ll expected | n.c:2:8: note: argument 1 of =E2=80=98void* mem2(void*)=E2=80=99 must be no= n-null 2 | void * mem2(void *dest) __attribute__((nonnull)); | ^~~~ And it's also detected at runtime using -fsanitize=3Dundefined: n.c:6:6: runtime error: null pointer passed as argument 1, which is declare= d to never be null=