From: Ralph Corderoy Date: Sun, 16 Oct 2016 17:17:31 +0000 (+0100) Subject: Tweak mh_xcalloc(); print size on error, follow POSIX. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/3910a511d4d9ad19e5eeff769881f4e52f498947?hp=1bd5129468527b24516a9efed711630a33e41a59 Tweak mh_xcalloc(); print size on error, follow POSIX. Using `%zu' for the size_t value. That might be a problem on older platforms, but we'll see. If asked for zero bytes then allocate one to get a unique pointer. --- diff --git a/h/utils.h b/h/utils.h index 20ebd2cf..6a99466c 100644 --- a/h/utils.h +++ b/h/utils.h @@ -9,7 +9,9 @@ void *mh_xmalloc(size_t); /* Call realloc(3), exiting on NULL return. */ void *mh_xrealloc(void *, size_t); +/* Call calloc(3), exiting on NULL return. */ void *mh_xcalloc(size_t, size_t); + char *pwd(void); char *add(const char *, char *); char *addlist(char *, const char *); diff --git a/sbr/utils.c b/sbr/utils.c index 83c32170..0766809d 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -61,22 +61,19 @@ void *mh_xrealloc(void *ptr, size_t size) return new; } -/* - * Safely call calloc - */ -void * -mh_xcalloc(size_t nmemb, size_t size) +/* Call calloc(3), exiting on NULL return. */ +void *mh_xcalloc(size_t nelem, size_t elsize) { - void *memory; + void *p; - if (nmemb == 0 || size == 0) - adios(NULL, "Tried to calloc 0 bytes"); + if (!nelem || !elsize) + return mh_xmalloc(1); /* Get a unique pointer. */ - if ((memory = calloc(nmemb, size))) { - return memory; - } else { - adios(NULL, "calloc failed"); - } + p = calloc(nelem, elsize); + if (!p) + adios(NULL, "calloc failed, size wanted: %zu * %zu", nelem, elsize); + + return p; } /*