Skip to content

Commit 4e7a2ec

Browse files
author
Junio C Hamano
committed
?alloc: do not return NULL when asked for zero bytes
Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 82f9d58 commit 4e7a2ec

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

git-compat-util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
6363
static inline void *xmalloc(size_t size)
6464
{
6565
void *ret = malloc(size);
66+
if (!ret && !size)
67+
ret = malloc(1);
6668
if (!ret)
6769
die("Out of memory, malloc failed");
6870
return ret;
@@ -71,6 +73,8 @@ static inline void *xmalloc(size_t size)
7173
static inline void *xrealloc(void *ptr, size_t size)
7274
{
7375
void *ret = realloc(ptr, size);
76+
if (!ret && !size)
77+
ret = realloc(ptr, 1);
7478
if (!ret)
7579
die("Out of memory, realloc failed");
7680
return ret;
@@ -79,6 +83,8 @@ static inline void *xrealloc(void *ptr, size_t size)
7983
static inline void *xcalloc(size_t nmemb, size_t size)
8084
{
8185
void *ret = calloc(nmemb, size);
86+
if (!ret && (!nmemb || !size))
87+
ret = calloc(1, 1);
8288
if (!ret)
8389
die("Out of memory, calloc failed");
8490
return ret;

0 commit comments

Comments
 (0)