]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
mem: add _gnutls_reallocarray and _gnutls_reallocarray_fast
authorDaiki Ueno <ueno@gnu.org>
Sun, 21 Feb 2021 07:42:23 +0000 (08:42 +0100)
committerDaiki Ueno <ueno@gnu.org>
Mon, 29 Mar 2021 08:27:38 +0000 (10:27 +0200)
Signed-off-by: Daiki Ueno <ueno@gnu.org>
lib/mem.c
lib/mem.h

index 32cab5a47c9489aa4513edd92173f353873ec01a..9a74456dbb160f0ad736cd711dffe3a76f202cdd 100644 (file)
--- a/lib/mem.c
+++ b/lib/mem.c
@@ -24,6 +24,7 @@
 #include "errors.h"
 #include <num.h>
 #include <xsize.h>
+#include "xalloc-oversized.h"
 
 gnutls_alloc_function gnutls_secure_malloc = malloc;
 gnutls_alloc_function gnutls_malloc = malloc;
@@ -61,6 +62,23 @@ void *gnutls_realloc_fast(void *ptr, size_t size)
        return ret;
 }
 
+/* This will free ptr in case reallocarray fails.
+ */
+void *_gnutls_reallocarray_fast(void *ptr, size_t nmemb, size_t size)
+{
+       void *ret;
+
+       if (size == 0)
+               return ptr;
+
+       ret = _gnutls_reallocarray(ptr, nmemb, size);
+       if (ret == NULL) {
+               gnutls_free(ptr);
+       }
+
+       return ret;
+}
+
 char *_gnutls_strdup(const char *str)
 {
        size_t siz;
@@ -77,6 +95,12 @@ char *_gnutls_strdup(const char *str)
        return ret;
 }
 
+void *_gnutls_reallocarray(void *ptr, size_t nmemb, size_t size)
+{
+       return xalloc_oversized(nmemb, size) ? NULL :
+               gnutls_realloc(ptr, nmemb * size);
+}
+
 #if 0
 /* don't use them. They are included for documentation.
  */
index d3eea97a408dbaeb121572c77c493eec48f3eb9b..13f47f17b477e49f9fc7586450d6275f7f0cc346 100644 (file)
--- a/lib/mem.h
+++ b/lib/mem.h
 
 #include <config.h>
 
-/* this realloc function will return ptr if size==0, and
- * will free the ptr if the new allocation failed.
+/* These realloc functions will return ptr if size==0, and will free
+ * the ptr if the new allocation failed.
  */
 void *gnutls_realloc_fast(void *ptr, size_t size);
+void *_gnutls_reallocarray_fast(void *ptr, size_t nmemb, size_t size);
 
 void *_gnutls_calloc(size_t nmemb, size_t size);
 char *_gnutls_strdup(const char *);
 
+void *_gnutls_reallocarray(void *, size_t, size_t);
+
 unsigned _gnutls_mem_is_zero(const uint8_t *ptr, unsigned size);
 
 #define zrelease_mpi_key(mpi) if (*mpi!=NULL) { \