{"thread":{"id":"333","subject":"[PATCH] check for malloc","startedAt":"2005-04-26T15:12:17Z","lastAt":"2005-04-26T18:25:58Z","messageCount":3,"participants":["Christopher Li","Linus Torvalds"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"1761","messageId":"20050426151217.GA5344@64m.dyndns.org","threadId":"333","inReplyTo":null,"subject":"[PATCH] check for malloc","fromName":"Christopher Li","fromEmail":"chrislgit@chrisli.org","sentAt":"2005-04-26T15:12:17Z","receivedAt":"2005-04-26T15:12:17Z","isPatch":true,"sender":{"key":"chrislgit@chrisli.org","avatar":null},"body":"Add check for NULL return from malloc and realloc\n\nSigned-off-by: Christopher Li <chrislgit@chrisli.org>\n\nIndex: git-hack/blob.c\n===================================================================\n--- git-hack.orig/blob.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/blob.c\t2005-04-26 10:55:56.000000000 -0400\n@@ -9,6 +9,8 @@\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n \t\tstruct blob *ret = malloc(sizeof(struct blob));\n+\t\tif (!ret)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tmemset(ret, 0, sizeof(struct blob));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = blob_type;\nIndex: git-hack/checkout-cache.c\n===================================================================\n--- git-hack.orig/checkout-cache.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/checkout-cache.c\t2005-04-26 10:56:32.000000000 -0400\n@@ -42,6 +42,8 @@\n \tchar *buf = malloc(len + 1);\n \tconst char *slash = path;\n \n+\tif (!buf)\n+\t\tdie (\"Out of memory, malloc failed\");\n \twhile ((slash = strchr(slash+1, '/')) != NULL) {\n \t\tlen = slash - path;\n \t\tmemcpy(buf, path, len);\nIndex: git-hack/commit-tree.c\n===================================================================\n--- git-hack.orig/commit-tree.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/commit-tree.c\t2005-04-26 10:54:18.000000000 -0400\n@@ -19,6 +19,8 @@\n static void init_buffer(char **bufp, unsigned int *sizep)\n {\n \tchar *buf = malloc(BLOCKING);\n+\tif (!buf)\n+\t\tdie(\"Out of memory, malloc failed\");\n \t*sizep = 0;\n \t*bufp = buf;\n }\n@@ -41,6 +43,8 @@\n \tif (newsize > alloc) {\n \t\talloc = (newsize + 32767) & ~32767;\n \t\tbuf = realloc(buf, alloc);\n+\t\tif (!buf)\n+\t\t\tdie(\"Out of memory, realloc failed\");\n \t\t*bufp = buf;\n \t}\n \t*sizep = newsize;\nIndex: git-hack/commit.c\n===================================================================\n--- git-hack.orig/commit.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/commit.c\t2005-04-26 10:57:21.000000000 -0400\n@@ -10,6 +10,8 @@\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n \t\tstruct commit *ret = malloc(sizeof(struct commit));\n+\t\tif (!ret)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tmemset(ret, 0, sizeof(struct commit));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = commit_type;\n@@ -79,6 +81,8 @@\n void commit_list_insert(struct commit *item, struct commit_list **list_p)\n {\n \tstruct commit_list *new_list = malloc(sizeof(struct commit_list));\n+\tif (new_list)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tnew_list->item = item;\n \tnew_list->next = *list_p;\n \t*list_p = new_list;\nIndex: git-hack/convert-cache.c\n===================================================================\n--- git-hack.orig/convert-cache.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/convert-cache.c\t2005-04-26 10:58:51.000000000 -0400\n@@ -20,6 +20,8 @@\n {\n \tstruct entry *new = malloc(sizeof(struct entry));\n \n+\tif (!new)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemset(new, 0, sizeof(*new));\n \tmemcpy(new->old_sha1, sha1, 20);\n \tmemmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));\n@@ -72,6 +74,8 @@\n \tunsigned long newlen = 0;\n \tunsigned long used;\n \n+\tif (!new)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tused = 0;\n \twhile (size) {\n \t\tint len = 21 + strlen(buffer);\n@@ -229,6 +233,8 @@\n \tchar *new = malloc(size + 100);\n \tunsigned long newlen = 0;\n \n+\tif (!new)\n+\t\tdie(\"Out of memory, malloc failed\");\n \t// \"tree <sha1>\\n\"\n \tmemcpy(new + newlen, buffer, 46);\n \tnewlen += 46;\n@@ -284,6 +290,8 @@\n \t\tdie(\"unable to read object %s\", sha1_to_hex(sha1));\n \n \tbuffer = malloc(size);\n+\tif (!buffer)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemcpy(buffer, data, size);\n \t\n \tif (!strcmp(type, \"blob\")) {\nIndex: git-hack/diff-tree.c\n===================================================================\n--- git-hack.orig/diff-tree.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/diff-tree.c\t2005-04-26 10:59:51.000000000 -0400\n@@ -38,6 +38,8 @@\n {\n \tint baselen = strlen(base);\n \tchar *newbase = malloc(baselen + pathlen + 2);\n+\tif (!newbase)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemcpy(newbase, base, baselen);\n \tmemcpy(newbase + baselen, path, pathlen);\n \tmemcpy(newbase + baselen + pathlen, \"/\", 2);\n@@ -271,6 +273,8 @@\n \t\tpaths = &argv[3];\n \t\tnr_paths = argc - 3;\n \t\tpathlens = malloc(nr_paths * sizeof(int));\n+\t\tif (!pathlens)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tfor (i=0; i<nr_paths; i++)\n \t\t\tpathlens[i] = strlen(paths[i]);\n \t}\nIndex: git-hack/http-pull.c\n===================================================================\n--- git-hack.orig/http-pull.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/http-pull.c\t2005-04-26 11:00:26.000000000 -0400\n@@ -74,6 +74,8 @@\n \tcurl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);\n \n \turl = malloc(strlen(base) + 50);\n+\tif (!url)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tstrcpy(url, base);\n \tposn = url + strlen(base);\n \tstrcpy(posn, \"objects/\");\nIndex: git-hack/init-db.c\n===================================================================\n--- git-hack.orig/init-db.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/init-db.c\t2005-04-26 11:00:43.000000000 -0400\n@@ -35,6 +35,8 @@\n \t}\n \tlen = strlen(sha1_dir);\n \tpath = malloc(len + 40);\n+\tif (!path)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemcpy(path, sha1_dir, len);\n \n \tsafe_create_dir(sha1_dir);\nIndex: git-hack/object.c\n===================================================================\n--- git-hack.orig/object.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/object.c\t2005-04-26 11:01:14.000000000 -0400\n@@ -53,6 +53,8 @@\n \tif (obj_allocs == nr_objs) {\n \t\tobj_allocs = alloc_nr(obj_allocs);\n \t\tobjs = realloc(objs, obj_allocs * sizeof(struct object *));\n+\t\tif (!objs)\n+\t\t\tdie(\"Out of memory, realloc failed\");\n \t}\n \n \t/* Insert it into the right place */\n@@ -76,6 +78,8 @@\n \n \ttarget->used = 1;\n \tp = malloc(sizeof(*p));\n+\tif (!p)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tp->item = target;\n \tp->next = NULL;\n \t*pp = p;\nIndex: git-hack/read-cache.c\n===================================================================\n--- git-hack.orig/read-cache.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/read-cache.c\t2005-04-26 10:54:23.000000000 -0400\n@@ -144,6 +144,8 @@\n \tif (active_nr == active_alloc) {\n \t\tactive_alloc = alloc_nr(active_alloc);\n \t\tactive_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));\n+\t\tif (active_cache)\n+\t\t\tdie(\"Out of memory, realloc failed\");\n \t}\n \n \t/* Add it in.. */\nIndex: git-hack/sha1_file.c\n===================================================================\n--- git-hack.orig/sha1_file.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/sha1_file.c\t2005-04-26 11:01:40.000000000 -0400\n@@ -74,6 +74,8 @@\n \t\tchar *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;\n \t\tint len = strlen(sha1_file_directory);\n \t\tbase = malloc(len + 60);\n+\t\tif (!base)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tmemcpy(base, sha1_file_directory, len);\n \t\tmemset(base+len, 0, 60);\n \t\tbase[len] = '/';\n@@ -272,6 +274,8 @@\n \tdeflateInit(&stream, Z_BEST_COMPRESSION);\n \tsize = deflateBound(&stream, len+hdrlen);\n \tcompressed = malloc(size);\n+\tif (!compressed)\n+\t\tdie(\"Out of memory, malloc failed\");\n \n \t/* Compress it */\n \tstream.next_out = compressed;\nIndex: git-hack/show-files.c\n===================================================================\n--- git-hack.orig/show-files.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/show-files.c\t2005-04-26 10:54:23.000000000 -0400\n@@ -31,8 +31,12 @@\n \tif (nr_dir == dir_alloc) {\n \t\tdir_alloc = alloc_nr(dir_alloc);\n \t\tdir = realloc(dir, dir_alloc*sizeof(char *));\n+\t\tif (!dir)\n+\t\t\tdie(\"Out of memory, realloc failed\");\n \t}\n \tname = malloc(len + 1);\n+\tif (!name)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemcpy(name, pathname, len + 1);\n \tdir[nr_dir++] = name;\n }\nIndex: git-hack/tree.c\n===================================================================\n--- git-hack.orig/tree.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/tree.c\t2005-04-26 10:54:23.000000000 -0400\n@@ -11,6 +11,8 @@\n \tunsigned int size = cache_entry_size(baselen + len);\n \tstruct cache_entry *ce = malloc(size);\n \n+\tif (!ce)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemset(ce, 0, size);\n \n \tce->ce_mode = create_ce_mode(mode);\n@@ -44,6 +46,8 @@\n \t\t\tchar elttype[20];\n \t\t\tunsigned long eltsize;\n \n+\t\t\tif (!newbase)\n+\t\t\t\tdie(\"Out of memory, malloc failed\");\n \t\t\teltbuf = read_sha1_file(sha1, elttype, &eltsize);\n \t\t\tif (!eltbuf || strcmp(elttype, \"tree\"))\n \t\t\t\treturn -1;\n@@ -75,6 +79,8 @@\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n \t\tstruct tree *ret = malloc(sizeof(struct tree));\n+\t\tif (!ret)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tmemset(ret, 0, sizeof(struct tree));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = tree_type;\n@@ -117,6 +123,8 @@\n \t\t\treturn -1;\n \n \t\tentry = malloc(sizeof(struct tree_entry_list));\n+\t\tif (!entry)\n+\t\t\tdie(\"Out of memory, malloc failed\");\n \t\tentry->name = strdup(path + 1);\n \t\tentry->directory = S_ISDIR(mode);\n \t\tentry->executable = mode & S_IXUSR;\nIndex: git-hack/update-cache.c\n===================================================================\n--- git-hack.orig/update-cache.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/update-cache.c\t2005-04-26 11:02:27.000000000 -0400\n@@ -41,6 +41,8 @@\n \tvoid *in;\n \tSHA_CTX c;\n \n+\tif (!metadata || !out)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tin = \"\";\n \tif (size)\n \t\tin = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);\n@@ -123,6 +125,8 @@\n \tnamelen = strlen(path);\n \tsize = cache_entry_size(namelen);\n \tce = malloc(size);\n+\tif (!ce)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemset(ce, 0, size);\n \tmemcpy(ce->name, path, namelen);\n \tfill_stat_cache_info(ce, &st);\n@@ -206,6 +210,8 @@\n \n \tsize = ce_size(ce);\n \tupdated = malloc(size);\n+\tif (!updated)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemcpy(updated, ce, size);\n \tfill_stat_cache_info(updated, &st);\n \treturn updated;\n@@ -282,6 +288,8 @@\n \tlen = strlen(arg3);\n \tsize = cache_entry_size(len);\n \tce = malloc(size);\n+\tif (!ce)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tmemset(ce, 0, size);\n \n \tmemcpy(ce->sha1, sha1, 20);\nIndex: git-hack/write-tree.c\n===================================================================\n--- git-hack.orig/write-tree.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/write-tree.c\t2005-04-26 10:54:23.000000000 -0400\n@@ -27,6 +27,8 @@\n \t/* Guess at some random initial size */\n \tsize = 8192;\n \tbuffer = malloc(size);\n+\tif (!buffer)\n+\t\tdie(\"Out of memory, malloc failed\");\n \toffset = 0;\n \n \tnr = 0;\n@@ -69,6 +71,8 @@\n \t\tif (offset + entrylen + 100 > size) {\n \t\t\tsize = alloc_nr(offset + entrylen + 100);\n \t\t\tbuffer = realloc(buffer, size);\n+\t\t\tif (!buffer)\n+\t\t\t\tdie(\"Out of memory, realloc failed\");\n \t\t}\n \t\toffset += sprintf(buffer + offset, \"%o %.*s\", mode, entrylen, filename);\n \t\tbuffer[offset++] = 0;\nIndex: git-hack/revision.h\n===================================================================\n--- git-hack.orig/revision.h\t2005-04-26 10:52:38.000000000 -0400\n+++ git-hack/revision.h\t2005-04-26 11:05:24.000000000 -0400\n@@ -69,9 +69,12 @@\n \tif (rev_allocs == nr_revs) {\n \t\trev_allocs = alloc_nr(rev_allocs);\n \t\trevs = realloc(revs, rev_allocs * sizeof(struct revision *));\n+\t\tif (!revs)\n+\t\t\tdie(\"Out of memory, realloc failed\")\n \t}\n \tn = malloc(sizeof(struct revision) + strlen(tag));\n-\n+\tif (!n)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tn->flags = 0;\n \tmemcpy(n->sha1, sha1, 20);\n \tn->parent = NULL;\n@@ -97,6 +100,8 @@\n \t}\n \n \tp = malloc(sizeof(*p));\n+\tif (!p)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tp->parent = parent_rev;\n \tp->next = NULL;\n \t*pp = p;\nIndex: git-hack/diff.c\n===================================================================\n--- git-hack.orig/diff.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/diff.c\t2005-04-26 11:00:06.000000000 -0400\n@@ -103,6 +103,8 @@\n \tchar *cmd = malloc(cmd_size);\n \tint next_at = 0;\n \n+\tif (!cmd)\n+\t\tdie(\"Out of memory, malloc failed\");\n \tnext_at += snprintf(cmd+next_at, cmd_size-next_at,\n \t\t\t    diff_cmd, name_sq, name_sq);\n \tnext_at += snprintf(cmd+next_at, cmd_size-next_at,\nIndex: git-hack/strbuf.c\n===================================================================\n--- git-hack.orig/strbuf.c\t2005-04-26 10:52:47.000000000 -0400\n+++ git-hack/strbuf.c\t2005-04-26 10:54:23.000000000 -0400\n@@ -1,6 +1,7 @@\n #include <stdio.h>\n #include <stdlib.h>\n #include \"strbuf.h\"\n+#include \"cache.h\"\n \n void strbuf_init(struct strbuf *sb) {\n \tsb->buf = 0;\n@@ -16,6 +17,8 @@\n \tif (sb->alloc <= sb->len) {\n \t\tsb->alloc = sb->alloc * 3 / 2 + 16;\n \t\tsb->buf = realloc(sb->buf, sb->alloc);\n+\t\tif (!sb->buf)\n+\t\t\tdie(\"Out of memory, realloc failed\");\n \t}\n \tsb->buf[sb->len++] = ch;\n }\n"},{"id":"1770","messageId":"20050426154212.GC5344@64m.dyndns.org","threadId":"333","inReplyTo":"Pine.LNX.4.58.0504261125220.18901@ppc970.osdl.org","subject":"Re: [PATCH] introduce xmalloc and xrealloc","fromName":"Christopher Li","fromEmail":"git@chrisli.org","sentAt":"2005-04-26T15:42:12Z","receivedAt":"2005-04-26T15:42:12Z","isPatch":true,"sender":{"key":"git@chrisli.org","avatar":null},"body":"On Tue, Apr 26, 2005 at 11:25:58AM -0700, Linus Torvalds wrote:\n> \n> I'd prefer xmalloc()/xrealloc() instead, and just do it in one place.\n\nDone.\n\nChris\n\nIntroduce xmalloc and xrealloc\n \nSigned-off-by: Christopher Li<chrislgit@chrisli.org>\nIndex: git-hack/blob.c\n===================================================================\n--- git-hack.orig/blob.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/blob.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -8,7 +8,7 @@\n {\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n-\t\tstruct blob *ret = malloc(sizeof(struct blob));\n+\t\tstruct blob *ret = xmalloc(sizeof(struct blob));\n \t\tmemset(ret, 0, sizeof(struct blob));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = blob_type;\nIndex: git-hack/checkout-cache.c\n===================================================================\n--- git-hack.orig/checkout-cache.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/checkout-cache.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -39,7 +39,7 @@\n static void create_directories(const char *path)\n {\n \tint len = strlen(path);\n-\tchar *buf = malloc(len + 1);\n+\tchar *buf = xmalloc(len + 1);\n \tconst char *slash = path;\n \n \twhile ((slash = strchr(slash+1, '/')) != NULL) {\nIndex: git-hack/commit-tree.c\n===================================================================\n--- git-hack.orig/commit-tree.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/commit-tree.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -18,7 +18,7 @@\n  */\n static void init_buffer(char **bufp, unsigned int *sizep)\n {\n-\tchar *buf = malloc(BLOCKING);\n+\tchar *buf = xmalloc(BLOCKING);\n \t*sizep = 0;\n \t*bufp = buf;\n }\n@@ -40,7 +40,7 @@\n \tbuf = *bufp;\n \tif (newsize > alloc) {\n \t\talloc = (newsize + 32767) & ~32767;\n-\t\tbuf = realloc(buf, alloc);\n+\t\tbuf = xrealloc(buf, alloc);\n \t\t*bufp = buf;\n \t}\n \t*sizep = newsize;\nIndex: git-hack/commit.c\n===================================================================\n--- git-hack.orig/commit.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/commit.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -9,7 +9,7 @@\n {\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n-\t\tstruct commit *ret = malloc(sizeof(struct commit));\n+\t\tstruct commit *ret = xmalloc(sizeof(struct commit));\n \t\tmemset(ret, 0, sizeof(struct commit));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = commit_type;\n@@ -78,7 +78,7 @@\n \n void commit_list_insert(struct commit *item, struct commit_list **list_p)\n {\n-\tstruct commit_list *new_list = malloc(sizeof(struct commit_list));\n+\tstruct commit_list *new_list = xmalloc(sizeof(struct commit_list));\n \tnew_list->item = item;\n \tnew_list->next = *list_p;\n \t*list_p = new_list;\nIndex: git-hack/convert-cache.c\n===================================================================\n--- git-hack.orig/convert-cache.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/convert-cache.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -18,8 +18,7 @@\n \n static struct entry *insert_new(unsigned char *sha1, int pos)\n {\n-\tstruct entry *new = malloc(sizeof(struct entry));\n-\n+\tstruct entry *new = xmalloc(sizeof(struct entry));\n \tmemset(new, 0, sizeof(*new));\n \tmemcpy(new->old_sha1, sha1, 20);\n \tmemmove(convert + pos + 1, convert + pos, (nr_convert - pos) * sizeof(struct entry *));\n@@ -68,7 +67,7 @@\n \n static int write_subdirectory(void *buffer, unsigned long size, const char *base, int baselen, unsigned char *result_sha1)\n {\n-\tchar *new = malloc(size);\n+\tchar *new = xmalloc(size);\n \tunsigned long newlen = 0;\n \tunsigned long used;\n \n@@ -226,9 +225,9 @@\n \n static void convert_date(void *buffer, unsigned long size, unsigned char *result_sha1)\n {\n-\tchar *new = malloc(size + 100);\n+\tchar *new = xmalloc(size + 100);\n \tunsigned long newlen = 0;\n-\n+\t\n \t// \"tree <sha1>\\n\"\n \tmemcpy(new + newlen, buffer, 46);\n \tnewlen += 46;\n@@ -283,7 +282,7 @@\n \tif (!data)\n \t\tdie(\"unable to read object %s\", sha1_to_hex(sha1));\n \n-\tbuffer = malloc(size);\n+\tbuffer = xmalloc(size);\n \tmemcpy(buffer, data, size);\n \t\n \tif (!strcmp(type, \"blob\")) {\nIndex: git-hack/diff-tree.c\n===================================================================\n--- git-hack.orig/diff-tree.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/diff-tree.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -37,7 +37,7 @@\n static char *malloc_base(const char *base, const char *path, int pathlen)\n {\n \tint baselen = strlen(base);\n-\tchar *newbase = malloc(baselen + pathlen + 2);\n+\tchar *newbase = xmalloc(baselen + pathlen + 2);\n \tmemcpy(newbase, base, baselen);\n \tmemcpy(newbase + baselen, path, pathlen);\n \tmemcpy(newbase + baselen + pathlen, \"/\", 2);\n@@ -270,7 +270,7 @@\n \n \t\tpaths = &argv[3];\n \t\tnr_paths = argc - 3;\n-\t\tpathlens = malloc(nr_paths * sizeof(int));\n+\t\tpathlens = xmalloc(nr_paths * sizeof(int));\n \t\tfor (i=0; i<nr_paths; i++)\n \t\t\tpathlens[i] = strlen(paths[i]);\n \t}\nIndex: git-hack/http-pull.c\n===================================================================\n--- git-hack.orig/http-pull.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/http-pull.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -73,7 +73,7 @@\n \tcurl_easy_setopt(curl, CURLOPT_FILE, NULL);\n \tcurl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);\n \n-\turl = malloc(strlen(base) + 50);\n+\turl = xmalloc(strlen(base) + 50);\n \tstrcpy(url, base);\n \tposn = url + strlen(base);\n \tstrcpy(posn, \"objects/\");\nIndex: git-hack/init-db.c\n===================================================================\n--- git-hack.orig/init-db.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/init-db.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -34,7 +34,7 @@\n \t\tfprintf(stderr, \"defaulting to local storage area\\n\");\n \t}\n \tlen = strlen(sha1_dir);\n-\tpath = malloc(len + 40);\n+\tpath = xmalloc(len + 40);\n \tmemcpy(path, sha1_dir, len);\n \n \tsafe_create_dir(sha1_dir);\nIndex: git-hack/object.c\n===================================================================\n--- git-hack.orig/object.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/object.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -52,7 +52,7 @@\n \n \tif (obj_allocs == nr_objs) {\n \t\tobj_allocs = alloc_nr(obj_allocs);\n-\t\tobjs = realloc(objs, obj_allocs * sizeof(struct object *));\n+\t\tobjs = xrealloc(objs, obj_allocs * sizeof(struct object *));\n \t}\n \n \t/* Insert it into the right place */\n@@ -75,7 +75,7 @@\n \t}\n \n \ttarget->used = 1;\n-\tp = malloc(sizeof(*p));\n+\tp = xmalloc(sizeof(*p));\n \tp->item = target;\n \tp->next = NULL;\n \t*pp = p;\nIndex: git-hack/read-cache.c\n===================================================================\n--- git-hack.orig/read-cache.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/read-cache.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -143,7 +143,7 @@\n \t/* Make sure the array is big enough .. */\n \tif (active_nr == active_alloc) {\n \t\tactive_alloc = alloc_nr(active_alloc);\n-\t\tactive_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));\n+\t\tactive_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));\n \t}\n \n \t/* Add it in.. */\nIndex: git-hack/sha1_file.c\n===================================================================\n--- git-hack.orig/sha1_file.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/sha1_file.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -73,7 +73,7 @@\n \tif (!base) {\n \t\tchar *sha1_file_directory = getenv(DB_ENVIRONMENT) ? : DEFAULT_DB_ENVIRONMENT;\n \t\tint len = strlen(sha1_file_directory);\n-\t\tbase = malloc(len + 60);\n+\t\tbase = xmalloc(len + 60);\n \t\tmemcpy(base, sha1_file_directory, len);\n \t\tmemset(base+len, 0, 60);\n \t\tbase[len] = '/';\n@@ -161,9 +161,7 @@\n \t\treturn NULL;\n \n \tbytes = strlen(buffer) + 1;\n-\tbuf = malloc(*size);\n-\tif (!buf)\n-\t\treturn NULL;\n+\tbuf = xmalloc(*size);\n \n \tmemcpy(buf, buffer + bytes, stream.total_out - bytes);\n \tbytes = stream.total_out - bytes;\n@@ -271,7 +269,7 @@\n \tmemset(&stream, 0, sizeof(stream));\n \tdeflateInit(&stream, Z_BEST_COMPRESSION);\n \tsize = deflateBound(&stream, len+hdrlen);\n-\tcompressed = malloc(size);\n+\tcompressed = xmalloc(size);\n \n \t/* Compress it */\n \tstream.next_out = compressed;\nIndex: git-hack/show-files.c\n===================================================================\n--- git-hack.orig/show-files.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/show-files.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -30,9 +30,9 @@\n \n \tif (nr_dir == dir_alloc) {\n \t\tdir_alloc = alloc_nr(dir_alloc);\n-\t\tdir = realloc(dir, dir_alloc*sizeof(char *));\n+\t\tdir = xrealloc(dir, dir_alloc*sizeof(char *));\n \t}\n-\tname = malloc(len + 1);\n+\tname = xmalloc(len + 1);\n \tmemcpy(name, pathname, len + 1);\n \tdir[nr_dir++] = name;\n }\nIndex: git-hack/tree.c\n===================================================================\n--- git-hack.orig/tree.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/tree.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -9,7 +9,7 @@\n {\n \tint len = strlen(pathname);\n \tunsigned int size = cache_entry_size(baselen + len);\n-\tstruct cache_entry *ce = malloc(size);\n+\tstruct cache_entry *ce = xmalloc(size);\n \n \tmemset(ce, 0, size);\n \n@@ -39,7 +39,7 @@\n \t\tif (S_ISDIR(mode)) {\n \t\t\tint retval;\n \t\t\tint pathlen = strlen(path);\n-\t\t\tchar *newbase = malloc(baselen + 1 + pathlen);\n+\t\t\tchar *newbase = xmalloc(baselen + 1 + pathlen);\n \t\t\tvoid *eltbuf;\n \t\t\tchar elttype[20];\n \t\t\tunsigned long eltsize;\n@@ -74,7 +74,7 @@\n {\n \tstruct object *obj = lookup_object(sha1);\n \tif (!obj) {\n-\t\tstruct tree *ret = malloc(sizeof(struct tree));\n+\t\tstruct tree *ret = xmalloc(sizeof(struct tree));\n \t\tmemset(ret, 0, sizeof(struct tree));\n \t\tcreated_object(sha1, &ret->object);\n \t\tret->object.type = tree_type;\n@@ -116,7 +116,7 @@\n \t\t    sscanf(bufptr, \"%o\", &mode) != 1)\n \t\t\treturn -1;\n \n-\t\tentry = malloc(sizeof(struct tree_entry_list));\n+\t\tentry = xmalloc(sizeof(struct tree_entry_list));\n \t\tentry->name = strdup(path + 1);\n \t\tentry->directory = S_ISDIR(mode);\n \t\tentry->executable = mode & S_IXUSR;\nIndex: git-hack/update-cache.c\n===================================================================\n--- git-hack.orig/update-cache.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/update-cache.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -35,8 +35,8 @@\n \tz_stream stream;\n \tunsigned long size = st->st_size;\n \tint max_out_bytes = size + 200;\n-\tvoid *out = malloc(max_out_bytes);\n-\tvoid *metadata = malloc(200);\n+\tvoid *out = xmalloc(max_out_bytes);\n+\tvoid *metadata = xmalloc(200);\n \tint metadata_size;\n \tvoid *in;\n \tSHA_CTX c;\n@@ -122,7 +122,7 @@\n \t}\n \tnamelen = strlen(path);\n \tsize = cache_entry_size(namelen);\n-\tce = malloc(size);\n+\tce = xmalloc(size);\n \tmemset(ce, 0, size);\n \tmemcpy(ce->name, path, namelen);\n \tfill_stat_cache_info(ce, &st);\n@@ -205,7 +205,7 @@\n \t\treturn ERR_PTR(-EINVAL);\n \n \tsize = ce_size(ce);\n-\tupdated = malloc(size);\n+\tupdated = xmalloc(size);\n \tmemcpy(updated, ce, size);\n \tfill_stat_cache_info(updated, &st);\n \treturn updated;\n@@ -281,7 +281,7 @@\n \n \tlen = strlen(arg3);\n \tsize = cache_entry_size(len);\n-\tce = malloc(size);\n+\tce = xmalloc(size);\n \tmemset(ce, 0, size);\n \n \tmemcpy(ce->sha1, sha1, 20);\nIndex: git-hack/write-tree.c\n===================================================================\n--- git-hack.orig/write-tree.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/write-tree.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -26,7 +26,7 @@\n \n \t/* Guess at some random initial size */\n \tsize = 8192;\n-\tbuffer = malloc(size);\n+\tbuffer = xmalloc(size);\n \toffset = 0;\n \n \tnr = 0;\n@@ -68,7 +68,7 @@\n \t\tentrylen = pathlen - baselen;\n \t\tif (offset + entrylen + 100 > size) {\n \t\t\tsize = alloc_nr(offset + entrylen + 100);\n-\t\t\tbuffer = realloc(buffer, size);\n+\t\t\tbuffer = xrealloc(buffer, size);\n \t\t}\n \t\toffset += sprintf(buffer + offset, \"%o %.*s\", mode, entrylen, filename);\n \t\tbuffer[offset++] = 0;\nIndex: git-hack/revision.h\n===================================================================\n--- git-hack.orig/revision.h\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/revision.h\t2005-04-26 11:38:26.000000000 -0400\n@@ -68,10 +68,9 @@\n \n \tif (rev_allocs == nr_revs) {\n \t\trev_allocs = alloc_nr(rev_allocs);\n-\t\trevs = realloc(revs, rev_allocs * sizeof(struct revision *));\n+\t\trevs = xrealloc(revs, rev_allocs * sizeof(struct revision *));\n \t}\n-\tn = malloc(sizeof(struct revision) + strlen(tag));\n-\n+\tn = xmalloc(sizeof(struct revision) + strlen(tag));\n \tn->flags = 0;\n \tmemcpy(n->sha1, sha1, 20);\n \tn->parent = NULL;\n@@ -96,7 +95,7 @@\n \t\tpp = &p->next;\n \t}\n \n-\tp = malloc(sizeof(*p));\n+\tp = xmalloc(sizeof(*p));\n \tp->parent = parent_rev;\n \tp->next = NULL;\n \t*pp = p;\nIndex: git-hack/diff.c\n===================================================================\n--- git-hack.orig/diff.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/diff.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -59,8 +59,7 @@\n \t\tif (*cp == '\\'')\n \t\t\tcnt += 3;\n \n-\tif (! (buf = malloc(cnt)))\n-\t    return buf;\n+\tbuf = xmalloc(cnt);\n \tbp = buf;\n \twhile ((c = *src++)) {\n \t\tif (c != '\\'')\n@@ -100,7 +99,7 @@\n \t\t\tstrlen(diff_arg) +\n \t\t\tstrlen(name_1_sq) + strlen(name_2_sq)\n \t\t\t- 5);\n-\tchar *cmd = malloc(cmd_size);\n+\tchar *cmd = xmalloc(cmd_size);\n \tint next_at = 0;\n \n \tnext_at += snprintf(cmd+next_at, cmd_size-next_at,\nIndex: git-hack/strbuf.c\n===================================================================\n--- git-hack.orig/strbuf.c\t2005-04-26 11:38:21.000000000 -0400\n+++ git-hack/strbuf.c\t2005-04-26 11:38:26.000000000 -0400\n@@ -1,6 +1,7 @@\n #include <stdio.h>\n #include <stdlib.h>\n #include \"strbuf.h\"\n+#include \"cache.h\"\n \n void strbuf_init(struct strbuf *sb) {\n \tsb->buf = 0;\n@@ -15,7 +16,7 @@\n static void inline strbuf_add(struct strbuf *sb, int ch) {\n \tif (sb->alloc <= sb->len) {\n \t\tsb->alloc = sb->alloc * 3 / 2 + 16;\n-\t\tsb->buf = realloc(sb->buf, sb->alloc);\n+\t\tsb->buf = xrealloc(sb->buf, sb->alloc);\n \t}\n \tsb->buf[sb->len++] = ch;\n }\nIndex: git-hack/cache.h\n===================================================================\n--- git-hack.orig/cache.h\t2005-04-26 11:40:43.000000000 -0400\n+++ git-hack/cache.h\t2005-04-26 11:40:59.000000000 -0400\n@@ -147,4 +147,20 @@\n \t\t\t\t\t\tunsigned long *size,\n \t\t\t\t\t\tunsigned char *tree_sha1_ret);\n \n+static inline void *xmalloc(int size)\n+{\n+\tvoid *ret = malloc(size);\n+\tif (!ret)\n+\t\tdie(\"Out of memory, malloc failed\");\n+\treturn ret;\n+}\n+\n+static inline void *xrealloc(void *ptr, int size)\n+{\n+\tvoid *ret = realloc(ptr, size);\n+\tif (!ret)\n+\t\tdie(\"Out of memory, realloc failed\");\n+\treturn ret;\n+}\n+\n #endif /* CACHE_H */\n\n\n"},{"id":"1767","messageId":"Pine.LNX.4.58.0504261125220.18901@ppc970.osdl.org","threadId":"333","inReplyTo":"20050426151217.GA5344@64m.dyndns.org","subject":"Re: [PATCH] check for malloc","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-04-26T18:25:58Z","receivedAt":"2005-04-26T18:25:58Z","isPatch":true,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\n\nOn Tue, 26 Apr 2005, Christopher Li wrote:\n>\n> Add check for NULL return from malloc and realloc\n\nI'd prefer xmalloc()/xrealloc() instead, and just do it in one place.\n\n\t\tLinus\n"}]}