diff mbox series

xmalloc: Fix warnings with gcc analyzer

Message ID 20210728050928.3873663-1-siddhesh@sourceware.org
State New
Headers show
Series xmalloc: Fix warnings with gcc analyzer | expand

Commit Message

Siddhesh Poyarekar July 28, 2021, 5:09 a.m. UTC
Tell the compiler that xmalloc family of allocators always return
non-NULL.
---
 include/programs/xmalloc.h | 12 ++++++++----
 misc/sys/cdefs.h           | 10 ++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

Comments

Florian Weimer July 28, 2021, 10:28 a.m. UTC | #1
* Siddhesh Poyarekar via Libc-alpha:

> diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
> index 30a621ab8f..d3203c73df 100644
> --- a/misc/sys/cdefs.h
> +++ b/misc/sys/cdefs.h
> @@ -330,6 +330,16 @@
>  # define __nonnull(params) _GL_ATTRIBUTE_NONNULL (params)
>  #endif
>  
> +/* The returns_nonnull function attribute marks the return type of the function
> +   as always being non-null.  */
> +#ifndef __returns_nonnull
> +# if __GNUC_PREREQ (4, 9) || __glibc_has_attribute (__returns_nonnull__)
> +# define __returns_nonnull __attribute__ ((returns_nonnull))
> +# else
> +# define __returns_nonnull
> +# endif
> +#endif

This needs to use __returns_nonnull__ in the attribute argument.

Thanks,
Florian
diff mbox series

Patch

diff --git a/include/programs/xmalloc.h b/include/programs/xmalloc.h
index 33871e22ef..574fb41254 100644
--- a/include/programs/xmalloc.h
+++ b/include/programs/xmalloc.h
@@ -23,11 +23,15 @@ 
 
 /* Prototypes for a few program-wide used functions.  */
 extern void *xmalloc (size_t n)
-  __attribute_malloc__ __attribute_alloc_size__ ((1)) __attr_dealloc_free;
+  __attribute_malloc__ __attribute_alloc_size__ ((1)) __attr_dealloc_free
+  __returns_nonnull;
 extern void *xcalloc (size_t n, size_t s)
-  __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __attr_dealloc_free;
+  __attribute_malloc__ __attribute_alloc_size__ ((1, 2)) __attr_dealloc_free
+  __returns_nonnull;
 extern void *xrealloc (void *o, size_t n)
-  __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free;
-extern char *xstrdup (const char *) __attribute_malloc__ __attr_dealloc_free;
+  __attribute_malloc__ __attribute_alloc_size__ ((2)) __attr_dealloc_free
+  __returns_nonnull;
+extern char *xstrdup (const char *) __attribute_malloc__ __attr_dealloc_free
+  __returns_nonnull;
 
 #endif /* xmalloc.h */
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index 30a621ab8f..d3203c73df 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -330,6 +330,16 @@ 
 # define __nonnull(params) _GL_ATTRIBUTE_NONNULL (params)
 #endif
 
+/* The returns_nonnull function attribute marks the return type of the function
+   as always being non-null.  */
+#ifndef __returns_nonnull
+# if __GNUC_PREREQ (4, 9) || __glibc_has_attribute (__returns_nonnull__)
+# define __returns_nonnull __attribute__ ((returns_nonnull))
+# else
+# define __returns_nonnull
+# endif
+#endif
+
 /* If fortification mode, we warn about unused results of certain
    function calls which can lead to problems.  */
 #if __GNUC_PREREQ (3,4) || __glibc_has_attribute (__warn_unused_result__)