From: Ian Jackson Date: Sat, 20 Sep 2014 13:10:28 +0000 (+0100) Subject: realloc: Provide safe_realloc_ary X-Git-Tag: proposed.polypath.v3~47 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=secnet.git;a=commitdiff_plain;h=1b514e8e055847fc18f2684c226575d29aafbcf5 realloc: Provide safe_realloc_ary Signed-off-by: Ian Jackson --- diff --git a/secnet.h b/secnet.h index f64382c..3b2ca93 100644 --- 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 0914732..8525c63 100644 --- 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 */