summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2010-11-09 22:45:13 +0100
committerJoel Rosdahl <joel@rosdahl.net>2010-11-09 22:45:13 +0100
commita8e23f24db6e6da750bbed194ad6e21a0683ce18 (patch)
tree85482803836224f8cc49a8e069d06edb00e6fe70 /util.c
parent9cdd115480513cd47f9039da3751f457aef50ef6 (diff)
parent5912bba9876d5a85f89ea96253037968becda489 (diff)
downloadccache-a8e23f24db6e6da750bbed194ad6e21a0683ce18.tar.gz
Merge branch 'maint'
* maint: Make x_malloc and x_calloc always return NULL for zero byte allocations Fix calloc success check Fix malloc success check
Diffstat (limited to 'util.c')
-rw-r--r--util.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/util.c b/util.c
index 27452a23..774d1bbb 100644
--- a/util.c
+++ b/util.c
@@ -596,6 +596,13 @@ 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) {
fatal("x_malloc: Could not allocate %lu bytes", (unsigned long)size);
@@ -608,8 +615,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) {
+ if (!retr) {
fatal("x_calloc: Could not allocate %lu bytes", (unsigned long)size);
}
return ret;