From 04be5b1456b381303709fdc90385fe74dad1bd10 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 22 Dec 2016 14:18:38 +0100 Subject: eprintf: add ecalloc Add ecalloc as an error handling calloc wrapper. --- eprintf.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'eprintf.c') diff --git a/eprintf.c b/eprintf.c index 7a885b3..41c043f 100644 --- a/eprintf.c +++ b/eprintf.c @@ -76,6 +76,18 @@ void *emalloc(size_t n) return p; } +/* ecalloc: attempt to allocate memory or exit on error */ +void *ecalloc(size_t nmemb, size_t size) +{ + void *p; + + p = calloc(nmemb, size); + if (p == NULL) + eprintf("ecalloc(%zu, %zu) failed:", nmemb, size); + + return p; +} + /* erealloc: attempt to reallocate memory or exit on error */ void *erealloc(void *p, size_t n) { -- cgit v1.2.3-54-g00ecf From 0fc0c19e4dc2b4ec2f175744e1ac468643b6da71 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 29 Dec 2016 14:46:34 +0100 Subject: eprintf: Add asserts --- eprintf.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'eprintf.c') diff --git a/eprintf.c b/eprintf.c index 41c043f..2aea355 100644 --- a/eprintf.c +++ b/eprintf.c @@ -1,9 +1,10 @@ +#include #include #include +#include #include #include #include -#include #include "eprintf.h" @@ -15,6 +16,8 @@ void eprintf(const char *fmt, ...) int errnum = errno; va_list va; + assert(fmt); + if (progname() != NULL) fprintf(stderr, "%s: ", progname()); @@ -35,6 +38,8 @@ void weprintf(const char *fmt, ...) int errnum = errno; va_list va; + assert(fmt); + if (progname() != NULL) fprintf(stderr, "%s: ", progname()); @@ -53,6 +58,8 @@ char *estrdup(const char *s) size_t len; char *dup; + assert(s); + len = strlen(s); if (len == SIZE_MAX) eprintf("estrdup(\"%.20s\") failed: Too long", s); -- cgit v1.2.3-54-g00ecf