Skip to content

Commit 812666c

Browse files
Christopher LiLinus Torvalds
authored andcommitted
[PATCH] introduce xmalloc and xrealloc
Introduce xmalloc and xrealloc to die gracefully with a descriptive message when out of memory, rather than taking a SIGSEGV. Signed-off-by: Christopher Li<chrislgit@chrisli.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent f2a1934 commit 812666c

18 files changed

+54
-41
lines changed

blob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct blob *lookup_blob(unsigned char *sha1)
88
{
99
struct object *obj = lookup_object(sha1);
1010
if (!obj) {
11-
struct blob *ret = malloc(sizeof(struct blob));
11+
struct blob *ret = xmalloc(sizeof(struct blob));
1212
memset(ret, 0, sizeof(struct blob));
1313
created_object(sha1, &ret->object);
1414
ret->object.type = blob_type;

cache.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,20 @@ extern void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1,
147147
unsigned long *size,
148148
unsigned char *tree_sha1_ret);
149149

150+
static inline void *xmalloc(int size)
151+
{
152+
void *ret = malloc(size);
153+
if (!ret)
154+
die("Out of memory, malloc failed");
155+
return ret;
156+
}
157+
158+
static inline void *xrealloc(void *ptr, int size)
159+
{
160+
void *ret = realloc(ptr, size);
161+
if (!ret)
162+
die("Out of memory, realloc failed");
163+
return ret;
164+
}
165+
150166
#endif /* CACHE_H */

checkout-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int force = 0, quiet = 0, not_new = 0;
3939
static void create_directories(const char *path)
4040
{
4141
int len = strlen(path);
42-
char *buf = malloc(len + 1);
42+
char *buf = xmalloc(len + 1);
4343
const char *slash = path;
4444

4545
while ((slash = strchr(slash+1, '/')) != NULL) {

commit-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
static void init_buffer(char **bufp, unsigned int *sizep)
2020
{
21-
char *buf = malloc(BLOCKING);
21+
char *buf = xmalloc(BLOCKING);
2222
*sizep = 0;
2323
*bufp = buf;
2424
}
@@ -40,7 +40,7 @@ static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
4040
buf = *bufp;
4141
if (newsize > alloc) {
4242
alloc = (newsize + 32767) & ~32767;
43-
buf = realloc(buf, alloc);
43+
buf = xrealloc(buf, alloc);
4444
*bufp = buf;
4545
}
4646
*sizep = newsize;

commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct commit *lookup_commit(unsigned char *sha1)
99
{
1010
struct object *obj = lookup_object(sha1);
1111
if (!obj) {
12-
struct commit *ret = malloc(sizeof(struct commit));
12+
struct commit *ret = xmalloc(sizeof(struct commit));
1313
memset(ret, 0, sizeof(struct commit));
1414
created_object(sha1, &ret->object);
1515
ret->object.type = commit_type;
@@ -78,7 +78,7 @@ int parse_commit(struct commit *item)
7878

7979
void commit_list_insert(struct commit *item, struct commit_list **list_p)
8080
{
81-
struct commit_list *new_list = malloc(sizeof(struct commit_list));
81+
struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
8282
new_list->item = item;
8383
new_list->next = *list_p;
8484
*list_p = new_list;

convert-cache.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ static struct entry * convert_entry(unsigned char *sha1);
1818

1919
static struct entry *insert_new(unsigned char *sha1, int pos)
2020
{
21-
struct entry *new = malloc(sizeof(struct entry));
22-
21+
struct entry *new = xmalloc(sizeof(struct entry));
2322
memset(new, 0, sizeof(*new));
2423
memcpy(new->old_sha1, sha1, 20);
2524
memmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));
@@ -68,7 +67,7 @@ static void convert_ascii_sha1(void *buffer)
6867

6968
static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)
7069
{
71-
char *new = malloc(size);
70+
char *new = xmalloc(size);
7271
unsigned long newlen = 0;
7372
unsigned long used;
7473

@@ -226,9 +225,9 @@ static int convert_date_line(char *dst, void **buf, unsigned long *sp)
226225

227226
static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)
228227
{
229-
char *new = malloc(size + 100);
228+
char *new = xmalloc(size + 100);
230229
unsigned long newlen = 0;
231-
230+
232231
// "tree <sha1>\n"
233232
memcpy(new + newlen, buffer, 46);
234233
newlen += 46;
@@ -283,7 +282,7 @@ static struct entry * convert_entry(unsigned char *sha1)
283282
if (!data)
284283
die("unable to read object %s", sha1_to_hex(sha1));
285284

286-
buffer = malloc(size);
285+
buffer = xmalloc(size);
287286
memcpy(buffer, data, size);
288287

289288
if (!strcmp(type, "blob")) {

diff-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const unsigned char *extract(void *tree, unsigned long size, const char *
3737
static char *malloc_base(const char *base, const char *path, int pathlen)
3838
{
3939
int baselen = strlen(base);
40-
char *newbase = malloc(baselen + pathlen + 2);
40+
char *newbase = xmalloc(baselen + pathlen + 2);
4141
memcpy(newbase, base, baselen);
4242
memcpy(newbase + baselen, path, pathlen);
4343
memcpy(newbase + baselen + pathlen, "/", 2);
@@ -270,7 +270,7 @@ int main(int argc, char **argv)
270270

271271
paths = &argv[3];
272272
nr_paths = argc - 3;
273-
pathlens = malloc(nr_paths * sizeof(int));
273+
pathlens = xmalloc(nr_paths * sizeof(int));
274274
for (i=0; i<nr_paths; i++)
275275
pathlens[i] = strlen(paths[i]);
276276
}

diff.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ static char *sq_expand(const char *src)
5959
if (*cp == '\'')
6060
cnt += 3;
6161

62-
if (! (buf = malloc(cnt)))
63-
return buf;
62+
buf = xmalloc(cnt);
6463
bp = buf;
6564
while ((c = *src++)) {
6665
if (c != '\'')
@@ -100,7 +99,7 @@ static void builtin_diff(const char *name,
10099
strlen(diff_arg) +
101100
strlen(name_1_sq) + strlen(name_2_sq)
102101
- 5);
103-
char *cmd = malloc(cmd_size);
102+
char *cmd = xmalloc(cmd_size);
104103
int next_at = 0;
105104

106105
next_at += snprintf(cmd+next_at, cmd_size-next_at,

http-pull.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static int fetch(unsigned char *sha1)
7373
curl_easy_setopt(curl, CURLOPT_FILE, NULL);
7474
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
7575

76-
url = malloc(strlen(base) + 50);
76+
url = xmalloc(strlen(base) + 50);
7777
strcpy(url, base);
7878
posn = url + strlen(base);
7979
strcpy(posn, "objects/");

init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, char **argv)
3434
fprintf(stderr, "defaulting to local storage area\n");
3535
}
3636
len = strlen(sha1_dir);
37-
path = malloc(len + 40);
37+
path = xmalloc(len + 40);
3838
memcpy(path, sha1_dir, len);
3939

4040
safe_create_dir(sha1_dir);

0 commit comments

Comments
 (0)