From: Rusty Russell Date: Thu, 7 Oct 2010 02:41:02 +0000 (+1030) Subject: compiler, talloc: warn if return from realloc-like functions isn't used. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=d40331c745a6e4a56a3ea210ef9e1e264f7e6e5a compiler, talloc: warn if return from realloc-like functions isn't used. This hit my doc extraction tool, so fix it! --- diff --git a/ccan/compiler/compiler.h b/ccan/compiler/compiler.h index 242ef84d..7adcc8ca 100644 --- a/ccan/compiler/compiler.h +++ b/ccan/compiler/compiler.h @@ -136,4 +136,23 @@ /* If we don't know, assume it's not. */ #define IS_COMPILE_CONSTANT(expr) 0 #endif + +#if HAVE_WARN_UNUSED_RESULT +/** + * WARN_UNUSED_RESULT - warn if a function return value is unused. + * + * Used to mark a function where it is extremely unlikely that the caller + * can ignore the result, eg realloc(). + * + * Example: + * // buf param may be freed by this; need return value! + * static char *WARN_UNUSED_RESULT enlarge(const char *buf, unsigned *size) + * { + * return realloc(buf, (*size) *= 2); + * } + */ +#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#else +#define WARN_UNUSED_RESULT +#endif #endif /* CCAN_COMPILER_H */ diff --git a/ccan/talloc/talloc.h b/ccan/talloc/talloc.h index 54790055..e65588cd 100644 --- a/ccan/talloc/talloc.h +++ b/ccan/talloc/talloc.h @@ -572,7 +572,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3) * * talloc_set_name_const(ptr, ptr) */ -char *talloc_append_string(char *orig, const char *append); +char *WARN_UNUSED_RESULT talloc_append_string(char *orig, const char *append); /** * talloc_asprintf_append - sprintf onto the end of a talloc buffer. @@ -586,7 +586,8 @@ char *talloc_append_string(char *orig, const char *append); * equivalent to: * talloc_set_name_const(ptr, ptr) */ -char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +char *WARN_UNUSED_RESULT talloc_asprintf_append(char *s, const char *fmt, ...) + PRINTF_ATTRIBUTE(2,3); /** * talloc_vasprintf - vsprintf into a talloc buffer. @@ -613,7 +614,8 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIB * The talloc_vasprintf_append() function is equivalent to * talloc_asprintf_append(), except it takes a va_list. */ -char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); +char *WARN_UNUSED_RESULT talloc_vasprintf_append(char *s, const char *fmt, va_list ap) + PRINTF_ATTRIBUTE(2,0); /** * talloc_set_type - force the name of a pointer to a particular type @@ -962,7 +964,7 @@ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *)); size_t talloc_reference_count(const void *ptr); void *_talloc_reference(const void *context, const void *ptr); -void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name); +void *WARN_UNUSED_RESULT _talloc_realloc(const void *context, void *ptr, size_t size, const char *name); void *talloc_parent(const void *ptr); const char *talloc_parent_name(const void *ptr); void *_talloc_steal(const void *new_ctx, const void *ptr); @@ -971,7 +973,7 @@ void *_talloc_zero(const void *ctx, size_t size, const char *name); void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name); void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); -void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); +void *WARN_UNUSED_RESULT _talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); void *talloc_realloc_fn(const void *context, void *ptr, size_t size); void talloc_show_parents(const void *context, FILE *file); int talloc_is_parent(const void *context, const void *ptr); diff --git a/ccan/talloc/test/run.c b/ccan/talloc/test/run.c index 9654f625..06f9249a 100644 --- a/ccan/talloc/test/run.c +++ b/ccan/talloc/test/run.c @@ -438,14 +438,14 @@ static bool test_realloc(const struct torture_context *ctx) "failed: talloc_realloc() on a referenced pointer should fail\n"); CHECK_BLOCKS("realloc", p1, 4); - talloc_realloc_size(NULL, p2, 0); - talloc_realloc_size(NULL, p2, 0); + ok1(talloc_realloc_size(NULL, p2, 0) == NULL); + ok1(talloc_realloc_size(NULL, p2, 0) == NULL); CHECK_BLOCKS("realloc", p1, 3); torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL, "failed: oversize talloc should fail\n"); - talloc_realloc_size(NULL, p1, 0); + p1 = talloc_realloc_size(NULL, p1, 0); CHECK_BLOCKS("realloc", root, 1); CHECK_SIZE("realloc", root, 0); @@ -948,7 +948,7 @@ int main(void) { struct torture_context *ctx; - plan_tests(284); + plan_tests(288); ctx = talloc_add_external(NULL, normal_realloc, test_lock, test_unlock); torture_local_talloc(NULL); diff --git a/config.h b/config.h index cc9375a0..82537b28 100644 --- a/config.h +++ b/config.h @@ -1,16 +1,23 @@ /* Simple config.h for recent gcc. */ #define HAVE_ALIGNOF 1 -#define HAVE_ATTRIBUTE_CONST 1 #define HAVE_ATTRIBUTE_COLD 1 +#define HAVE_ATTRIBUTE_CONST 1 #define HAVE_ATTRIBUTE_MAY_ALIAS 1 #define HAVE_ATTRIBUTE_PRINTF 1 -#define HAVE_ATTRIBUTE_USED 1 #define HAVE_ATTRIBUTE_UNUSED 1 +#define HAVE_ATTRIBUTE_USED 1 #define HAVE_BIG_ENDIAN 0 +#define HAVE_BSWAP_64 1 #define HAVE_BUILTIN_CHOOSE_EXPR 1 +#define HAVE_BUILTIN_CLZ 1 +#define HAVE_BUILTIN_CLZL 1 +#define HAVE_BUILTIN_CLZLL 1 #define HAVE_BUILTIN_CONSTANT_P 1 #define HAVE_BUILTIN_EXPECT 1 +#define HAVE_BUILTIN_FFSL 1 +#define HAVE_BUILTIN_POPCOUNTL 1 #define HAVE_BUILTIN_TYPES_COMPATIBLE_P 1 +#define HAVE_BYTESWAP_H 1 #define HAVE_GETPAGESIZE 1 #define HAVE_LITTLE_ENDIAN 1 #define HAVE_MMAP 1 @@ -18,10 +25,4 @@ #define HAVE_STATEMENT_EXPR 1 #define HAVE_TYPEOF 1 #define HAVE_UTIME 1 -#define HAVE_BUILTIN_CLZ 1 -#define HAVE_BUILTIN_CLZL 1 -#define HAVE_BUILTIN_CLZLL 1 -#define HAVE_BUILTIN_FFSL 1 -#define HAVE_BUILTIN_POPCOUNTL 1 -#define HAVE_BYTESWAP_H 1 -#define HAVE_BSWAP_64 1 +#define HAVE_WARN_UNUSED_RESULT 1