From 74eecc059657dc9d93ef1a4da6372c7c15b91892 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Sun, 16 Oct 2016 18:21:15 +0100 Subject: [PATCH] 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. --- h/utils.h | 3 +++ sbr/utils.c | 7 +++++++ 2 files changed, 10 insertions(+) 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. -- 2.56.0