* lib/quotearg.h, lib/xalloc.h: Include stdlib.h, for the benefit of _GL_ATTRIBUTE_DALLOC_FREE. * lib/quotearg.h (clone_quoting_options): * lib/xalloc.h (xmalloc, ximalloc, xzalloc, xizalloc, xcalloc) (xicalloc, xrealloc, xirealloc, xireallocarray, x2realloc) (x2nrealloc, xmemdup, ximemdup, xstrdup, xnmalloc, xcharalloc): Add _GL_ATTRIBUTE_DALLOC_FREE and _GL_ATTRIBUTE_RETURNS_NONNULL attributes as appropriate. --- ChangeLog | 10 ++++++++ lib/quotearg.h | 6 +++-- lib/xalloc.h | 65 ++++++++++++++++++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 22 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 33a472f5e..00224486f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2021-07-30 Paul Eggert <egg...@cs.ucla.edu> + xalloc: add malloc-related function attributes + * lib/quotearg.h, lib/xalloc.h: Include stdlib.h, for + the benefit of _GL_ATTRIBUTE_RETURNS_NONNULL. + * lib/quotearg.h (clone_quoting_options): + * lib/xalloc.h (xmalloc, ximalloc, xzalloc, xizalloc, xcalloc) + (xicalloc, xrealloc, xirealloc, xireallocarray, x2realloc) + (x2nrealloc, xmemdup, ximemdup, xstrdup, xnmalloc, xcharalloc): + Add _GL_ATTRIBUTE_DALLOC_FREE and _GL_ATTRIBUTE_RETURNS_NONNULL + attributes as appropriate. + gnulib-common: update for gcc -Wmismatched-dealloc * m4/gnulib-common.m4 (gl_COMMON_BODY): Define the macros _GL_ATTRIBUTE_DEALLOC and _GL_ATTRIBUTE_DEALLOC_FREE. These can diff --git a/lib/quotearg.h b/lib/quotearg.h index bfc264289..ecef74f74 100644 --- a/lib/quotearg.h +++ b/lib/quotearg.h @@ -22,6 +22,7 @@ # define QUOTEARG_H_ 1 # include <stddef.h> +# include <stdlib.h> /* Basic quoting styles. For each style, an example is given on the input strings "simple", "\0 \t\n'\"\033?""?/\\", and "a:b", using @@ -275,8 +276,9 @@ struct quoting_options; /* Allocate a new set of quoting options, with contents initially identical to O if O is not null, or to the default if O is null. It is the caller's responsibility to free the result. */ -struct quoting_options *clone_quoting_options (struct quoting_options *o); - +struct quoting_options *clone_quoting_options (struct quoting_options *o) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_RETURNS_NONNULL; /* Get the value of O's quoting style. If O is null, use the default. */ enum quoting_style get_quoting_style (struct quoting_options const *o); diff --git a/lib/xalloc.h b/lib/xalloc.h index 70ef0971f..05e2daa71 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -19,6 +19,7 @@ #define XALLOC_H_ #include <stddef.h> +#include <stdlib.h> #include <stdint.h> #if GNULIB_XALLOC @@ -53,28 +54,50 @@ extern "C" { #if GNULIB_XALLOC -void *xmalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -void *ximalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -void *xzalloc (size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -void *xizalloc (idx_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); +void *xmalloc (size_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *ximalloc (idx_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *xzalloc (size_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *xizalloc (idx_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; void *xcalloc (size_t n, size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL; void *xicalloc (idx_t n, idx_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); -void *xrealloc (void *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); -void *xirealloc (void *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *xrealloc (void *p, size_t s) + _GL_ATTRIBUTE_ALLOC_SIZE ((2)); +void *xirealloc (void *p, idx_t s) + _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL; void *xreallocarray (void *p, size_t n, size_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); + _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); void *xireallocarray (void *p, idx_t n, idx_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); -void *x2realloc (void *p, size_t *ps); /* superseded by xpalloc */ -void *x2nrealloc (void *p, size_t *pn, size_t s); /* superseded by xpalloc */ -void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s); -void *xmemdup (void const *p, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); -void *ximemdup (void const *p, idx_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2)); -char *ximemdup0 (void const *p, idx_t s) _GL_ATTRIBUTE_MALLOC; + _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *x2realloc (void *p, size_t *ps) /* superseded by xpalloc */ + _GL_ATTRIBUTE_RETURNS_NONNULL; +void *x2nrealloc (void *p, size_t *pn, size_t s) /* superseded by xpalloc */ + _GL_ATTRIBUTE_RETURNS_NONNULL; +void *xpalloc (void *pa, idx_t *pn, idx_t n_incr_min, ptrdiff_t n_max, idx_t s) + _GL_ATTRIBUTE_RETURNS_NONNULL; +void *xmemdup (void const *p, size_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL; +void *ximemdup (void const *p, idx_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((2)) _GL_ATTRIBUTE_RETURNS_NONNULL; +char *ximemdup0 (void const *p, idx_t s) + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_RETURNS_NONNULL; char *xstrdup (char const *str) - _GL_ATTRIBUTE_MALLOC; + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_RETURNS_NONNULL; /* In the following macros, T must be an elementary or structure/union or typedef'ed type, or a pointer to such a type. To apply one of the @@ -106,7 +129,8 @@ char *xstrdup (char const *str) dynamically, with error checking. S must be nonzero. */ XALLOC_INLINE void *xnmalloc (size_t n, size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)) _GL_ATTRIBUTE_RETURNS_NONNULL; XALLOC_INLINE void * xnmalloc (size_t n, size_t s) { @@ -118,7 +142,7 @@ xnmalloc (size_t n, size_t s) objects each of S bytes, with error checking. S must be nonzero. */ XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); + _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); XALLOC_INLINE void * xnrealloc (void *p, size_t n, size_t s) { @@ -129,7 +153,8 @@ xnrealloc (void *p, size_t n, size_t s) except it returns char *. */ XALLOC_INLINE char *xcharalloc (size_t n) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); + _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE + _GL_ATTRIBUTE_ALLOC_SIZE ((1)) _GL_ATTRIBUTE_RETURNS_NONNULL; XALLOC_INLINE char * xcharalloc (size_t n) { -- 2.31.1