chiark / gitweb /
realloc: Provide safe_realloc_ary
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 20 Sep 2014 13:10:28 +0000 (14:10 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 2 Oct 2014 15:31:13 +0000 (16:31 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
secnet.h
util.c

index f64382c35ff3fb038a94eab53c142b8358cde3aa..3b2ca93ef19e818ca423503b4425c63c8624c735 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -165,6 +165,8 @@ extern uint32_t string_list_to_word(list_t *l, struct flagstr *f,
 extern char *safe_strdup(const char *string, const char *message);
 extern void *safe_malloc(size_t size, const char *message);
 extern void *safe_malloc_ary(size_t size, size_t count, const char *message);
+extern void *safe_realloc_ary(void *p, size_t size, size_t count,
+                             const char *message);
 
 void setcloexec(int fd); /* cannot fail */
 void pipe_cloexec(int fd[2]); /* pipe(), setcloexec() twice; cannot fail */
diff --git a/util.c b/util.c
index 091473279f430e201b0bf92c686d05e7f78f0abf..8525c63222cdf98c73f2ca1c334b78445e0b2e21 100644 (file)
--- a/util.c
+++ b/util.c
@@ -77,11 +77,22 @@ void *safe_malloc(size_t size, const char *message)
     }
     return r;
 }
-void *safe_malloc_ary(size_t size, size_t count, const char *message) {
+void *safe_realloc_ary(void *p, size_t size, size_t count,
+                      const char *message) {
     if (count >= INT_MAX/size) {
        fatal("array allocation overflow: %s", message);
     }
-    return safe_malloc(size*count, message);
+    assert(size && count);
+    p = realloc(p, size*count);
+    if (!p)
+       fatal_perror("%s", message);
+    return p;
+}
+
+void *safe_malloc_ary(size_t size, size_t count, const char *message) {
+    if (!size && !count)
+       return 0;
+    return safe_realloc_ary(0,size,count,message);
 }
 
 /* Convert a buffer into its MP_INT representation */