]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Make x_malloc and x_calloc always return NULL for zero byte allocations
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 9 Nov 2010 21:40:43 +0000 (22:40 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 9 Nov 2010 21:40:43 +0000 (22:40 +0100)
This helps making sure that the code handles NULL return values regardless
of platform.

util.c

diff --git a/util.c b/util.c
index 96fe1f7b12b8dd5836992838ce04d6b4ac6c6269..8105da14fa2a0bdb87478582dff3f99194476603 100644 (file)
--- a/util.c
+++ b/util.c
@@ -542,8 +542,15 @@ void *
 x_malloc(size_t size)
 {
        void *ret;
+       if (size == 0) {
+               /*
+                * malloc() may return NULL if size is zero, so always do this to make sure
+                * that the code handles it regardless of platform.
+                */
+               return NULL;
+       }
        ret = malloc(size);
-       if (!ret && size) {
+       if (!ret) {
                fatal("x_malloc: Could not allocate %lu bytes", (unsigned long)size);
        }
        return ret;
@@ -554,8 +561,15 @@ void *
 x_calloc(size_t nmemb, size_t size)
 {
        void *ret;
+       if (nmemb * size == 0) {
+               /*
+                * calloc() may return NULL if nmemb or size is 0, so always do this to
+                * make sure that the code handles it regardless of platform.
+                */
+               return NULL;
+       }
        ret = calloc(nmemb, size);
-       if (!ret && nmemb && size) {
+       if (!retr) {
                fatal("x_calloc: Could not allocate %lu bytes", (unsigned long)size);
        }
        return ret;