From 33a63bb8d9e56424140d4b546bad5d10a3da97aa Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 30 Jul 2021 14:54:28 -0700 Subject: Improved error checking --- utils.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 3a950c8..578b59c 100644 --- a/utils.c +++ b/utils.c @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -124,12 +125,17 @@ char unescapechar(const char *escaped, const char **end) } // -// Fail and exit if a memory value is NULL +// If the given argument is NULL, print the error message and exit with +// failure. Otherwise return the given argument. // -void *memcheck(void *p) +void *check_nonnull(void *p, const char *err_msg, ...) { - if (p == NULL) - err(EXIT_FAILURE, "memory allocation failure"); + if (p == NULL) { + va_list args; + va_start(args, err_msg); + verr(EXIT_FAILURE, err_msg, args); + va_end(args); + } return p; } -- cgit v1.2.3