From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 81A7A3858C98; Tue, 6 Feb 2024 10:07:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81A7A3858C98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707214039; bh=mK7OmEp/M7+lCR8B9fE7SuSLF7lYAq0vLS1LxvKgXtw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=MI9DBQsIIHpHYNZBFC/zJ+XG6L/Z5wLRJCq8GLU6vDb2/4H5bZOfmVJR3Y8fnugfu 0N+LlSpG9Yvmjd3+7wiZQ4eBGMFOvVAyiwsEneh311YYxX5DB76szNdwtOpUvRpAqY lWOqAwH+lE9vX83eE/KTXmUziHFcDxE2akoIej74= From: "paul at crapouillou dot net" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/86879] G++ should warn about redundant tests for null pointers returned from functions with __attribute__((returns_nonnull)) Date: Tue, 06 Feb 2024 10:07:18 +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: 9.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: enhancement X-Bugzilla-Who: paul at crapouillou dot net X-Bugzilla-Status: NEW 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: 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=3D86879 Paul Cercueil changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |paul at crapouillou dot net --- Comment #3 from Paul Cercueil --- I would also like to see a warning, and also in C code, for a different rea= son. I have functions that return pointers to opaque structures. In case of an error, instead of returning NULL and setting errno, it encodes the error co= de into the pointer value. The error code can then be retrieved with the following inline function: static inline int is_err(const void *ptr) { return (uintptr_t) ptr >=3D (uintptr_t) -4095 ? (int)(intptr_t) ptr= : 0; } if is_err(ptr) returns 0, then the pointer is valid - otherwise it returns = the error code. Note that this was inspired on the Linux kernel, which has the exact same mechanism. What I want to prevent (and warn on), is incorrect error-checking of the functions using this mechanism. Most often than not, callers will do this: struct foo *ptr =3D maybe_return_errptr(arg); if (!ptr) { printf("Error!\n"); return NULL; } To avoid this mistake, I could tag my "maybe_return_errptr()" function with __attribute__((returns_nonnull)). However, even with that, GCC does not warn about the NULL-check; and it'd be great if it would.=