X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/4d78228c5d21c6320ecca02f587e167d0c6f024d..1bd5129468527b24516a9efed711630a33e41a59:/sbr/utils.c?ds=inline diff --git a/sbr/utils.c b/sbr/utils.c index d3228909..83c32170 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -39,26 +39,26 @@ void *mh_xmalloc(size_t size) return p; } -/* - * Safely call realloc - */ -void * -mh_xrealloc(void *ptr, size_t size) +/* Call realloc(3), exiting on NULL return. */ +void *mh_xrealloc(void *ptr, size_t size) { - void *memory; + void *new; - /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */ + /* Copy POSIX behaviour, coping with non-POSIX systems. */ + if (size == 0) { + if (ptr) { + free(ptr); + } + return mh_xmalloc(1); /* Get a unique pointer. */ + } if (!ptr) return mh_xmalloc(size); - if (size == 0) - adios(NULL, "Tried to realloc 0 bytes"); - - memory = realloc(ptr, size); - if (!memory) - adios(NULL, "Realloc failed"); + new = realloc(ptr, size); + if (!new) + adios(NULL, "realloc failed, size wanted: %zu", size); - return memory; + return new; } /*