public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "paul at crapouillou dot net" <gcc-bugzilla@gcc.gnu.org>
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	[thread overview]
Message-ID: <bug-86879-4-HAn04Rc2h1@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-86879-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86879

Paul Cercueil <paul at crapouillou dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |paul at crapouillou dot net

--- Comment #3 from Paul Cercueil <paul at crapouillou dot net> ---
I would also like to see a warning, and also in C code, for a different reason.

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 code
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 >= (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 = 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.

      parent reply	other threads:[~2024-02-06 10:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-86879-4@http.gcc.gnu.org/bugzilla/>
2021-04-08 21:28 ` msebor at gcc dot gnu.org
2024-02-06 10:07 ` paul at crapouillou dot net [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-86879-4-HAn04Rc2h1@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).