Commit d40331c7 authored by Rusty Russell's avatar Rusty Russell

compiler, talloc: warn if return from realloc-like functions isn't used.

      
This hit my doc extraction tool, so fix it!
parent 42ecd161
...@@ -136,4 +136,23 @@ ...@@ -136,4 +136,23 @@
/* If we don't know, assume it's not. */ /* If we don't know, assume it's not. */
#define IS_COMPILE_CONSTANT(expr) 0 #define IS_COMPILE_CONSTANT(expr) 0
#endif #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 */ #endif /* CCAN_COMPILER_H */
...@@ -572,7 +572,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3) ...@@ -572,7 +572,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3)
* *
* talloc_set_name_const(ptr, ptr) * 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. * talloc_asprintf_append - sprintf onto the end of a talloc buffer.
...@@ -586,7 +586,8 @@ char *talloc_append_string(char *orig, const char *append); ...@@ -586,7 +586,8 @@ char *talloc_append_string(char *orig, const char *append);
* equivalent to: * equivalent to:
* talloc_set_name_const(ptr, ptr) * 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. * 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 ...@@ -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 * The talloc_vasprintf_append() function is equivalent to
* talloc_asprintf_append(), except it takes a va_list. * 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 * 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 *)); ...@@ -962,7 +964,7 @@ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
size_t talloc_reference_count(const void *ptr); size_t talloc_reference_count(const void *ptr);
void *_talloc_reference(const void *context, 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); void *talloc_parent(const void *ptr);
const char *talloc_parent_name(const void *ptr); const char *talloc_parent_name(const void *ptr);
void *_talloc_steal(const void *new_ctx, 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); ...@@ -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_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_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_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_realloc_fn(const void *context, void *ptr, size_t size);
void talloc_show_parents(const void *context, FILE *file); void talloc_show_parents(const void *context, FILE *file);
int talloc_is_parent(const void *context, const void *ptr); int talloc_is_parent(const void *context, const void *ptr);
......
...@@ -438,14 +438,14 @@ static bool test_realloc(const struct torture_context *ctx) ...@@ -438,14 +438,14 @@ static bool test_realloc(const struct torture_context *ctx)
"failed: talloc_realloc() on a referenced pointer should fail\n"); "failed: talloc_realloc() on a referenced pointer should fail\n");
CHECK_BLOCKS("realloc", p1, 4); CHECK_BLOCKS("realloc", p1, 4);
talloc_realloc_size(NULL, p2, 0); ok1(talloc_realloc_size(NULL, p2, 0) == NULL);
talloc_realloc_size(NULL, p2, 0); ok1(talloc_realloc_size(NULL, p2, 0) == NULL);
CHECK_BLOCKS("realloc", p1, 3); CHECK_BLOCKS("realloc", p1, 3);
torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL, torture_assert("realloc", talloc_realloc_size(NULL, p1, 0x7fffffff) == NULL,
"failed: oversize talloc should fail\n"); "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_BLOCKS("realloc", root, 1);
CHECK_SIZE("realloc", root, 0); CHECK_SIZE("realloc", root, 0);
...@@ -948,7 +948,7 @@ int main(void) ...@@ -948,7 +948,7 @@ int main(void)
{ {
struct torture_context *ctx; struct torture_context *ctx;
plan_tests(284); plan_tests(288);
ctx = talloc_add_external(NULL, normal_realloc, test_lock, test_unlock); ctx = talloc_add_external(NULL, normal_realloc, test_lock, test_unlock);
torture_local_talloc(NULL); torture_local_talloc(NULL);
......
/* Simple config.h for recent gcc. */ /* Simple config.h for recent gcc. */
#define HAVE_ALIGNOF 1 #define HAVE_ALIGNOF 1
#define HAVE_ATTRIBUTE_CONST 1
#define HAVE_ATTRIBUTE_COLD 1 #define HAVE_ATTRIBUTE_COLD 1
#define HAVE_ATTRIBUTE_CONST 1
#define HAVE_ATTRIBUTE_MAY_ALIAS 1 #define HAVE_ATTRIBUTE_MAY_ALIAS 1
#define HAVE_ATTRIBUTE_PRINTF 1 #define HAVE_ATTRIBUTE_PRINTF 1
#define HAVE_ATTRIBUTE_USED 1
#define HAVE_ATTRIBUTE_UNUSED 1 #define HAVE_ATTRIBUTE_UNUSED 1
#define HAVE_ATTRIBUTE_USED 1
#define HAVE_BIG_ENDIAN 0 #define HAVE_BIG_ENDIAN 0
#define HAVE_BSWAP_64 1
#define HAVE_BUILTIN_CHOOSE_EXPR 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_CONSTANT_P 1
#define HAVE_BUILTIN_EXPECT 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_BUILTIN_TYPES_COMPATIBLE_P 1
#define HAVE_BYTESWAP_H 1
#define HAVE_GETPAGESIZE 1 #define HAVE_GETPAGESIZE 1
#define HAVE_LITTLE_ENDIAN 1 #define HAVE_LITTLE_ENDIAN 1
#define HAVE_MMAP 1 #define HAVE_MMAP 1
...@@ -18,10 +25,4 @@ ...@@ -18,10 +25,4 @@
#define HAVE_STATEMENT_EXPR 1 #define HAVE_STATEMENT_EXPR 1
#define HAVE_TYPEOF 1 #define HAVE_TYPEOF 1
#define HAVE_UTIME 1 #define HAVE_UTIME 1
#define HAVE_BUILTIN_CLZ 1 #define HAVE_WARN_UNUSED_RESULT 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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment