From: Paul Eggert <eggert@cs.ucla.edu>
To: bug-gnulib@gnu.org
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: [PATCH 5/6] xalloc: add malloc-related function attributes
Date: Sat, 31 Jul 2021 12:01:22 -0700 [thread overview]
Message-ID: <20210731190124.279622-5-eggert@cs.ucla.edu> (raw)
In-Reply-To: <20210731190124.279622-1-eggert@cs.ucla.edu>
* 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 <eggert@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
next prev parent reply other threads:[~2021-07-31 19:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-31 19:01 [PATCH 1/6] limits-h: add BOOL_MAX, BOOL_WIDTH Paul Eggert
2021-07-31 19:01 ` [PATCH 2/6] manywarnings: document GCC 11 warnings Paul Eggert
2021-07-31 19:01 ` [PATCH 3/6] gnulib-common: update for C2x 2020-12-11 draft Paul Eggert
2021-07-31 19:01 ` [PATCH 4/6] gnulib-common: update for gcc -Wmismatched-dealloc Paul Eggert
2021-07-31 19:01 ` Paul Eggert [this message]
2021-07-31 19:01 ` [PATCH 6/6] doc: C2X -> C2x Paul Eggert
2021-08-01 13:38 ` [PATCH 1/6] limits-h: add BOOL_MAX, BOOL_WIDTH Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210731190124.279622-5-eggert@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=bug-gnulib@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).