https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84202
Bug ID: 84202 Summary: missing -Wnonnull on a returns_nonnull function returning null Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- 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. 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 }