From 1b514e8e055847fc18f2684c226575d29aafbcf5 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 20 Sep 2014 14:10:28 +0100 Subject: [PATCH] realloc: Provide safe_realloc_ary Signed-off-by: Ian Jackson --- secnet.h | 2 ++ util.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) 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 */ -- 2.30.2