]> granicus.if.org Git - graphviz/commitdiff
gcalloc: fix: do not abort on calloc(0, x) or calloc(x, 0) returning NULL
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 26 Jul 2021 04:52:08 +0000 (21:52 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 2 Aug 2021 02:18:33 +0000 (19:18 -0700)
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.

CHANGELOG.md
lib/common/memory.c

index 5a78175c81f3f9ea65531caf48756cfb6d1439ff..2214973707d6f85bcdfe6e09bcdfd88e845a5363 100644 (file)
@@ -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
 
index f32dc21e18a1a0f30be7edfc85596a77437755ca..6071a769ea75f8c5c2af42e49696ae30bb174d2a 100644 (file)
@@ -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);
     }