aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2020-03-04 12:55:53 +0000
committerWerner Koch <[email protected]>2020-03-04 12:55:53 +0000
commit6fa1808cb7639f0f3745b78c4b7ce902e42b228c (patch)
treeda32f3560bbd6b050bc338571d6d1fd8b3ebf3e8
parentgpgconf: Always use xmalloc. (diff)
downloadgnupg-6fa1808cb7639f0f3745b78c4b7ce902e42b228c.tar.gz
gnupg-6fa1808cb7639f0f3745b78c4b7ce902e42b228c.zip
common: Add xreallocarray function.
* common/miscellaneous.c (xreallocarray): New func. * common/util.h (xtryreallocarray): New macro. -- Very useful to match calloc behaviour. Signed-off-by: Werner Koch <[email protected]>
-rw-r--r--common/miscellaneous.c11
-rw-r--r--common/util.h8
2 files changed, 19 insertions, 0 deletions
diff --git a/common/miscellaneous.c b/common/miscellaneous.c
index 260552828..5ede00128 100644
--- a/common/miscellaneous.c
+++ b/common/miscellaneous.c
@@ -117,6 +117,17 @@ xoutofcore (void)
}
+/* Wrapper around gpgrt_reallocarray. */
+void *
+xreallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size)
+{
+ void *p = gpgrt_reallocarray (a, oldnmemb, nmemb, size);
+ if (!p)
+ xoutofcore ();
+ return p;
+}
+
+
/* A wrapper around gcry_cipher_algo_name to return the string
"AES-128" instead of "AES". Given that we have an alias in
libgcrypt for it, it does not harm to too much to return this other
diff --git a/common/util.h b/common/util.h
index e1d9fa6fd..daf6d7438 100644
--- a/common/util.h
+++ b/common/util.h
@@ -99,6 +99,7 @@ typedef char **rl_completion_func_t (const char *, int, int);
#define xtrycalloc(a,b) gcry_calloc ((a),(b))
#define xtrycalloc_secure(a,b) gcry_calloc_secure ((a),(b))
#define xtryrealloc(a,b) gcry_realloc ((a),(b))
+#define xtryreallocarray(a,b,c,d) gpgrt_reallocarray ((a),(b),(c),(d))
#define xtrystrdup(a) gcry_strdup ((a))
#define xfree(a) gcry_free ((a))
#define xfree_fnc gcry_free
@@ -109,6 +110,7 @@ typedef char **rl_completion_func_t (const char *, int, int);
#define xcalloc_secure(a,b) gcry_xcalloc_secure ((a),(b))
#define xrealloc(a,b) gcry_xrealloc ((a),(b))
#define xstrdup(a) gcry_xstrdup ((a))
+/* See also the xreallocarray prototype below. */
/* For compatibility with gpg 1.4 we also define these: */
#define xmalloc_clear(a) gcry_xcalloc (1, (a))
@@ -306,6 +308,12 @@ void setup_libgcrypt_logging (void);
/* Print an out of core message and die. */
void xoutofcore (void);
+/* Wrapper aroung gpgrt_reallocarray. Uses the gpgrt alloc function
+ * which are redirect to the Libgcrypt versions via
+ * init_common_subsystems. Thus they can be used interchangeable with
+ * the other alloc functions. */
+void *xreallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size);
+
/* Same as estream_asprintf but die on memory failure. */
char *xasprintf (const char *fmt, ...) GPGRT_ATTR_PRINTF(1,2);
/* This is now an alias to estream_asprintf. */