Skip to content

Commit f8bb1d9

Browse files
pcloudsgitster
authored andcommitted
wrapper.c: introduce gentle xmallocz that does not die()
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 41ca19b commit f8bb1d9

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ extern try_to_free_t set_try_to_free_routine(try_to_free_t);
593593
extern char *xstrdup(const char *str);
594594
extern void *xmalloc(size_t size);
595595
extern void *xmallocz(size_t size);
596+
extern void *xmallocz_gently(size_t size);
596597
extern void *xmemdupz(const void *data, size_t len);
597598
extern char *xstrndup(const char *str, size_t len);
598599
extern void *xrealloc(void *ptr, size_t size);

wrapper.c

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ static void do_nothing(size_t size)
99

1010
static void (*try_to_free_routine)(size_t size) = do_nothing;
1111

12-
static void memory_limit_check(size_t size)
12+
static int memory_limit_check(size_t size, int gentle)
1313
{
1414
static int limit = -1;
1515
if (limit == -1) {
1616
const char *env = getenv("GIT_ALLOC_LIMIT");
1717
limit = env ? atoi(env) * 1024 : 0;
1818
}
19-
if (limit && size > limit)
20-
die("attempting to allocate %"PRIuMAX" over limit %d",
21-
(intmax_t)size, limit);
19+
if (limit && size > limit) {
20+
if (gentle) {
21+
error("attempting to allocate %"PRIuMAX" over limit %d",
22+
(intmax_t)size, limit);
23+
return -1;
24+
} else
25+
die("attempting to allocate %"PRIuMAX" over limit %d",
26+
(intmax_t)size, limit);
27+
}
28+
return 0;
2229
}
2330

2431
try_to_free_t set_try_to_free_routine(try_to_free_t routine)
@@ -42,11 +49,12 @@ char *xstrdup(const char *str)
4249
return ret;
4350
}
4451

45-
void *xmalloc(size_t size)
52+
static void *do_xmalloc(size_t size, int gentle)
4653
{
4754
void *ret;
4855

49-
memory_limit_check(size);
56+
if (memory_limit_check(size, gentle))
57+
return NULL;
5058
ret = malloc(size);
5159
if (!ret && !size)
5260
ret = malloc(1);
@@ -55,26 +63,54 @@ void *xmalloc(size_t size)
5563
ret = malloc(size);
5664
if (!ret && !size)
5765
ret = malloc(1);
58-
if (!ret)
59-
die("Out of memory, malloc failed (tried to allocate %lu bytes)",
60-
(unsigned long)size);
66+
if (!ret) {
67+
if (!gentle)
68+
die("Out of memory, malloc failed (tried to allocate %lu bytes)",
69+
(unsigned long)size);
70+
else {
71+
error("Out of memory, malloc failed (tried to allocate %lu bytes)",
72+
(unsigned long)size);
73+
return NULL;
74+
}
75+
}
6176
}
6277
#ifdef XMALLOC_POISON
6378
memset(ret, 0xA5, size);
6479
#endif
6580
return ret;
6681
}
6782

68-
void *xmallocz(size_t size)
83+
void *xmalloc(size_t size)
84+
{
85+
return do_xmalloc(size, 0);
86+
}
87+
88+
static void *do_xmallocz(size_t size, int gentle)
6989
{
7090
void *ret;
71-
if (unsigned_add_overflows(size, 1))
72-
die("Data too large to fit into virtual memory space.");
73-
ret = xmalloc(size + 1);
74-
((char*)ret)[size] = 0;
91+
if (unsigned_add_overflows(size, 1)) {
92+
if (gentle) {
93+
error("Data too large to fit into virtual memory space.");
94+
return NULL;
95+
} else
96+
die("Data too large to fit into virtual memory space.");
97+
}
98+
ret = do_xmalloc(size + 1, gentle);
99+
if (ret)
100+
((char*)ret)[size] = 0;
75101
return ret;
76102
}
77103

104+
void *xmallocz(size_t size)
105+
{
106+
return do_xmallocz(size, 0);
107+
}
108+
109+
void *xmallocz_gently(size_t size)
110+
{
111+
return do_xmallocz(size, 1);
112+
}
113+
78114
/*
79115
* xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
80116
* "data" to the allocated memory, zero terminates the allocated memory,
@@ -96,7 +132,7 @@ void *xrealloc(void *ptr, size_t size)
96132
{
97133
void *ret;
98134

99-
memory_limit_check(size);
135+
memory_limit_check(size, 0);
100136
ret = realloc(ptr, size);
101137
if (!ret && !size)
102138
ret = realloc(ptr, 1);
@@ -115,7 +151,7 @@ void *xcalloc(size_t nmemb, size_t size)
115151
{
116152
void *ret;
117153

118-
memory_limit_check(size * nmemb);
154+
memory_limit_check(size * nmemb, 0);
119155
ret = calloc(nmemb, size);
120156
if (!ret && (!nmemb || !size))
121157
ret = calloc(1, 1);

0 commit comments

Comments
 (0)