From: Ralph Corderoy Date: Sun, 16 Oct 2016 16:43:56 +0000 (+0100) Subject: Tweak mh_xmalloc(); print size in error, allocate zero bytes. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/4d78228c5d21c6320ecca02f587e167d0c6f024d?hp=8890670d4c1e8e3f9608c2850c1fe299fb256776 Tweak mh_xmalloc(); print size in error, allocate zero bytes. 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 rather than exit; it's sometimes useful to allocate zero, but some older platforms might not like it. --- diff --git a/h/utils.h b/h/utils.h index 9e515989..3a428193 100644 --- a/h/utils.h +++ b/h/utils.h @@ -3,7 +3,9 @@ * utils.h -- utility prototypes */ +/* Call malloc(3), exiting on NULL return. */ void *mh_xmalloc(size_t); + void *mh_xrealloc(void *, size_t); void *mh_xcalloc(size_t, size_t); char *pwd(void); diff --git a/sbr/utils.c b/sbr/utils.c index 7ebb4186..d3228909 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -25,22 +25,18 @@ extern char *mhdocdir; */ #define MAXMSGS 256 -/* - * Safely call malloc - */ -void * -mh_xmalloc(size_t size) +/* Call malloc(3), exiting on NULL return. */ +void *mh_xmalloc(size_t size) { - void *memory; + void *p; if (size == 0) - adios(NULL, "Tried to malloc 0 bytes"); + size = 1; /* Some mallocs don't like 0. */ + p = malloc(size); + if (!p) + adios(NULL, "malloc failed, size wanted: %zu", size); - memory = malloc(size); - if (!memory) - adios(NULL, "Malloc failed"); - - return memory; + return p; } /*