]> git.ozlabs.org Git - ccan/commitdiff
compiler, talloc: warn if return from realloc-like functions isn't used.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 7 Oct 2010 02:41:02 +0000 (13:11 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 7 Oct 2010 02:41:02 +0000 (13:11 +1030)
This hit my doc extraction tool, so fix it!

ccan/compiler/compiler.h
ccan/talloc/talloc.h
ccan/talloc/test/run.c
config.h

index 242ef84d133ddb65ea378bdfcc6585a7f46d7bd8..7adcc8ca1a4ea521cb11069d3dce4dff062ecb14 100644 (file)
 /* If we don't know, assume it's not. */
 #define IS_COMPILE_CONSTANT(expr) 0
 #endif
 /* 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 */
 #endif /* CCAN_COMPILER_H */
index 54790055ea8bf1c56f3452585596b744fd03c5f7..e65588cd57d3966b7b9108e35cfde35706d05797 100644 (file)
@@ -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);
  * equivalent to:
  *   talloc_set_name_const(ptr, ptr)
  */
  * 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.
 
 /**
  * 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.
  */
  * 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
 
 /**
  * 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);
 
 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);
 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_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);
 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);
index 9654f625322fae32e44c1654088965e097e357f9..06f9249a152ae71f04cd0d769f7e8a137330437a 100644 (file)
@@ -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);
 
                "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");
 
        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);
 
        CHECK_BLOCKS("realloc", root, 1);
        CHECK_SIZE("realloc", root, 0);
@@ -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);
 
        torture_local_talloc(NULL);
        ctx = talloc_add_external(NULL, normal_realloc, test_lock, test_unlock);
 
        torture_local_talloc(NULL);
index cc9375a06429684f354185613e936b9ace25efd6..82537b28878bfc3e5c7fbf499e69191732c9a9ff 100644 (file)
--- a/config.h
+++ b/config.h
@@ -1,16 +1,23 @@
 /* Simple config.h for recent gcc. */
 #define HAVE_ALIGNOF 1
 /* Simple config.h for recent gcc. */
 #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_PRINTF 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_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_EXPECT 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_BUILTIN_TYPES_COMPATIBLE_P 1
+#define HAVE_BYTESWAP_H 1
 #define HAVE_GETPAGESIZE 1
 #define HAVE_LITTLE_ENDIAN 1
 #define HAVE_MMAP 1
 #define HAVE_GETPAGESIZE 1
 #define HAVE_LITTLE_ENDIAN 1
 #define HAVE_MMAP 1
 #define HAVE_STATEMENT_EXPR 1
 #define HAVE_TYPEOF 1
 #define HAVE_UTIME 1
 #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