From: Ralph Corderoy Date: Sun, 16 Oct 2016 17:21:15 +0000 (+0100) Subject: Add mh_xfree(), guarding free(3) from NULLs. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/74eecc059657dc9d93ef1a4da6372c7c15b91892?hp=3910a511d4d9ad19e5eeff769881f4e52f498947 Add mh_xfree(), guarding free(3) from NULLs. Perhaps all nmh's platforms cope with free(3) these days, but it gives symmetry with the other mh_x* memory functions and if there is code testing the pointer before calling free then it can use this instead. --- diff --git a/h/utils.h b/h/utils.h index 6a99466c..95cae88c 100644 --- a/h/utils.h +++ b/h/utils.h @@ -12,6 +12,9 @@ void *mh_xrealloc(void *, size_t); /* Call calloc(3), exiting on NULL return. */ void *mh_xcalloc(size_t, size_t); +/* Call free(3), if ptr isn't NULL. */ +void mh_xfree(void *ptr); + char *pwd(void); char *add(const char *, char *); char *addlist(char *, const char *); diff --git a/sbr/utils.c b/sbr/utils.c index 0766809d..5b7b19f1 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -76,6 +76,13 @@ void *mh_xcalloc(size_t nelem, size_t elsize) return p; } +/* Call free(3), if ptr isn't NULL. */ +void mh_xfree(void *ptr) +{ + if (ptr) + free(ptr); /* Some very old platforms can't cope with NULL. */ +} + /* * Return the present working directory, if the current directory does not * exist, or is too long, make / the pwd.