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.

Reply via email to