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>
Mon, 29 Sep 2014 15:01:05 +0000 (16:01 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
secnet.h
util.c

index d9bd400f95f200105f630498470018ce38bd3cbb..c514014a388e0620ca0191e38fea82a88818e734 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 b91497edc8b3bc8606054008898d8bc98a6da230..4029786c7a1303e671b6f7bbe968ff70f739f9b0 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 */