?alloc: do not return NULL when asked for zero bytes
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 29, 2005 at 01:31 UTC
4e7a2eccc21c9029180585e43b3b941d0bdf8b3b
1 file changed
+6
git-compat-util.h
+6
@@ -63,6 +63,8 @@ extern char *gitstrcasestr(const char *haystack, const char *needle);
63
static inline void *xmalloc(size_t size)
64
{
65
void *ret = malloc(size);
66
+ if (!ret && !size)
67
+ ret = malloc(1);
68
if (!ret)
69
die("Out of memory, malloc failed");
70
return ret;
@@ -71,6 +73,8 @@ static inline void *xmalloc(size_t size)
73
static inline void *xrealloc(void *ptr, size_t size)
74
{
75
void *ret = realloc(ptr, size);
76
+ if (!ret && !size)
77
+ ret = realloc(ptr, 1);
78
if (!ret)
79
die("Out of memory, realloc failed");
80
return ret;
@@ -79,6 +83,8 @@ static inline void *xrealloc(void *ptr, size_t size)
83
static inline void *xcalloc(size_t nmemb, size_t size)
84
{
85
void *ret = calloc(nmemb, size);
86
+ if (!ret && (!nmemb || !size))
87
+ ret = calloc(1, 1);
88
if (!ret)
89
die("Out of memory, calloc failed");
90
return ret;