This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
suggestion: c compiler warning for failure to test result
- From: Joe Perches <joe at perches dot com>
- To: gcc <gcc at gcc dot gnu dot org>
- Date: Tue, 25 Apr 2017 13:35:03 -0700
- Subject: suggestion: c compiler warning for failure to test result
- Authentication-results: sourceware.org; auth=none
A possibly useful addition similar to:
__attribute__((warn_unused_result))
might be
__attribute__((warn_untested_result))
for things like allocation failures that
are not verified before use.
For instance:
void *malloc(size_t size);
could become
void * __attribute((warn_untested_result)) malloc(size_t size)
so that
#include <stdlib.h>
struct foo {
int bar;
};
struct foo *alloc_foo(void)
{
struct foo *baz = malloc(sizeof(struct foo));
baz->bar = 1;
return baz;
}
The compiler could emit a warning on the set
of baz->bar as an intermediate test of baz
is not performed before any use of baz.
struct foo *alloc_foo(void)
{
struct foo *baz =
malloc(sizeof(struct foo));
if (baz) baz->bar = 1;
return
baz;
}
Similarly, alloc_foo could use that new attribute.