cleanup: use internal memory allocation wrapper functions everywhere

The "x"-prefixed versions of strdup, malloc, etc. will check whether the allocation was successful and terminate the process otherwise. A few uses of malloc were left alone since they already implemented a graceful path of failure or were in a quasi external library like xdiff. Additionally, the call to malloc in compat/win32/syslog.c was not modified since the syslog() implemented there is a die handler and a call to the x-wrappers within a die handler could result in recursion should memory allocation fail. This will have to be addressed separately. Signed-off-by: Brandon Casey <drafnel@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Oct 6, 2011 at 13:22 UTC 040a655116c9755bbf30acd22c34eecb2f502c6d
7 files changed +8 -8
attr.c
+1 -1
@@ -533,7 +533,7 @@ static void bootstrap_attr_stack(void)
533
534 if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
535 elem = read_attr(GITATTRIBUTES_FILE, 1);
536 - elem->origin = strdup("");
536 + elem->origin = xstrdup("");
537 elem->prev = attr_stack;
538 attr_stack = elem;
539 debug_push(elem);
builtin/mv.c
+1 -1
@@ -29,7 +29,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
29 to_copy--;
30 if (to_copy != length || base_name) {
31 char *it = xmemdupz(result[i], to_copy);
32 - result[i] = base_name ? strdup(basename(it)) : it;
32 + result[i] = base_name ? xstrdup(basename(it)) : it;
33 }
34 }
35 return get_pathspec(prefix, result);
compat/mingw.c
+1 -1
@@ -1183,7 +1183,7 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
1183 }
1184 ai->ai_addrlen = sizeof(struct sockaddr_in);
1185 if (hints && (hints->ai_flags & AI_CANONNAME))
1186 - ai->ai_canonname = h ? strdup(h->h_name) : NULL;
1186 + ai->ai_canonname = h ? xstrdup(h->h_name) : NULL;
1187 else
1188 ai->ai_canonname = NULL;
1189
compat/qsort.c
+1 -1
@@ -55,7 +55,7 @@ void git_qsort(void *b, size_t n, size_t s,
55 msort_with_tmp(b, n, s, cmp, buf);
56 } else {
57 /* It's somewhat large, so malloc it. */
58 - char *tmp = malloc(size);
58 + char *tmp = xmalloc(size);
59 msort_with_tmp(b, n, s, cmp, tmp);
60 free(tmp);
61 }
remote.c
+1 -1
@@ -840,7 +840,7 @@ char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
840 refspec->dst, &ret))
841 return ret;
842 } else if (!strcmp(refspec->src, name))
843 - return strdup(refspec->dst);
843 + return xstrdup(refspec->dst);
844 }
845 return NULL;
846 }
show-index.c
+1 -1
@@ -48,7 +48,7 @@ int main(int argc, char **argv)
48 unsigned char sha1[20];
49 uint32_t crc;
50 uint32_t off;
51 - } *entries = malloc(nr * sizeof(entries[0]));
51 + } *entries = xmalloc(nr * sizeof(entries[0]));
52 for (i = 0; i < nr; i++)
53 if (fread(entries[i].sha1, 20, 1, stdin) != 1)
54 die("unable to read sha1 %u/%u", i, nr);
transport-helper.c
+2 -2
@@ -183,7 +183,7 @@ static struct child_process *get_helper(struct transport *transport)
183 ALLOC_GROW(refspecs,
184 refspec_nr + 1,
185 refspec_alloc);
186 - refspecs[refspec_nr++] = strdup(capname + strlen("refspec "));
186 + refspecs[refspec_nr++] = xstrdup(capname + strlen("refspec "));
187 } else if (!strcmp(capname, "connect")) {
188 data->connect = 1;
189 } else if (!prefixcmp(capname, "export-marks ")) {
@@ -445,7 +445,7 @@ static int fetch_with_import(struct transport *transport,
445 if (data->refspecs)
446 private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
447 else
448 - private = strdup(posn->name);
448 + private = xstrdup(posn->name);
449 read_ref(private, posn->old_sha1);
450 free(private);
451 }