[Bug tree-optimization/84202] missing -Wnonnull on a returns_nonnull function returning null

egallager at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Nov 7 05:16:00 GMT 2019


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

--- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #0)
> GCC silently accepts definitions of functions such as f() below declared
> with attribute returns_nonnull.  Clang issues -Wnonnull on functions that
> trivially violate that guarantee.
> 
> void* __attribute__ ((returns_nonnull)) f ()
> {
>   return 0;   // missing -Wnonnull
> }
> 
> Diagnosing trivial mistakes like the one above is only of very limited
> usefulness.  GCC can do much better by also diagnosing functions that might
> return null such as the two below.  Clang doesn't diagnose these.

Maybe split it into a separate -Wmaybe-nonnull for the tougher cases. 

> 
> void* __attribute__ ((returns_nonnull)) g (unsigned n)
> {
>   return __builtin_malloc (n);   // missing -Wnonnull
> }
> 
> void* __attribute__ ((returns_nonnull)) h (unsigned n)
> {
>   static char buf[256];
> 
>   return n < sizeof buf ? buf : __builtin_malloc (n);   // missing -Wnonnull
> }


More information about the Gcc-bugs mailing list