From: Matthew Fernandez Date: Mon, 26 Jul 2021 04:52:08 +0000 (-0700) Subject: gcalloc: fix: do not abort on calloc(0, x) or calloc(x, 0) returning NULL X-Git-Tag: 2.49.0~33^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ba32fe4974335a0357d3abcf741b1113332ffd5d;p=graphviz gcalloc: fix: do not abort on calloc(0, x) or calloc(x, 0) returning NULL Commit 43a37bedd934bde50a6441766b85dfc9648abda9 added a `calloc` wrapper but mistakenly did not account for it being valid for `calloc` to return `NULL` when the input arguments are 0. This is unlikely to have caused a problem with Glibc whose allocator typically returns non-null pointers even for 0 allocations. However assuming a 0 allocation may return a null pointer is more portable. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a78175c8..221497370 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 directory #2101 - the generated gdefs.h header is no longer installed - `ccomps` out-of-memory message no longer incorrectly refers to `gc` +- do not abort when `calloc(0, x)` or `calloc(x, 0)` in `gcalloc` return `NULL` ## [2.48.0] - 2021-07-17 diff --git a/lib/common/memory.c b/lib/common/memory.c index f32dc21e1..6071a769e 100644 --- a/lib/common/memory.c +++ b/lib/common/memory.c @@ -38,7 +38,7 @@ void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize) void *gcalloc(size_t nmemb, size_t size) { char *rv = calloc(nmemb, size); - if (UNLIKELY(rv == NULL)) { + if (UNLIKELY(nmemb > 0 && size > 0 && rv == NULL)) { fprintf(stderr, "out of memory\n"); exit(EXIT_FAILURE); }