Skip to content

Commit 040a655

Browse files
drafnelgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 97410b2 commit 040a655

File tree

7 files changed

+8
-8
lines changed

7 files changed

+8
-8
lines changed

attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ static void bootstrap_attr_stack(void)
533533

534534
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
535535
elem = read_attr(GITATTRIBUTES_FILE, 1);
536-
elem->origin = strdup("");
536+
elem->origin = xstrdup("");
537537
elem->prev = attr_stack;
538538
attr_stack = elem;
539539
debug_push(elem);

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
2929
to_copy--;
3030
if (to_copy != length || base_name) {
3131
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;
3333
}
3434
}
3535
return get_pathspec(prefix, result);

compat/mingw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
11831183
}
11841184
ai->ai_addrlen = sizeof(struct sockaddr_in);
11851185
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;
11871187
else
11881188
ai->ai_canonname = NULL;
11891189

compat/qsort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void git_qsort(void *b, size_t n, size_t s,
5555
msort_with_tmp(b, n, s, cmp, buf);
5656
} else {
5757
/* It's somewhat large, so malloc it. */
58-
char *tmp = malloc(size);
58+
char *tmp = xmalloc(size);
5959
msort_with_tmp(b, n, s, cmp, tmp);
6060
free(tmp);
6161
}

remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
840840
refspec->dst, &ret))
841841
return ret;
842842
} else if (!strcmp(refspec->src, name))
843-
return strdup(refspec->dst);
843+
return xstrdup(refspec->dst);
844844
}
845845
return NULL;
846846
}

show-index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main(int argc, char **argv)
4848
unsigned char sha1[20];
4949
uint32_t crc;
5050
uint32_t off;
51-
} *entries = malloc(nr * sizeof(entries[0]));
51+
} *entries = xmalloc(nr * sizeof(entries[0]));
5252
for (i = 0; i < nr; i++)
5353
if (fread(entries[i].sha1, 20, 1, stdin) != 1)
5454
die("unable to read sha1 %u/%u", i, nr);

transport-helper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static struct child_process *get_helper(struct transport *transport)
183183
ALLOC_GROW(refspecs,
184184
refspec_nr + 1,
185185
refspec_alloc);
186-
refspecs[refspec_nr++] = strdup(capname + strlen("refspec "));
186+
refspecs[refspec_nr++] = xstrdup(capname + strlen("refspec "));
187187
} else if (!strcmp(capname, "connect")) {
188188
data->connect = 1;
189189
} else if (!prefixcmp(capname, "export-marks ")) {
@@ -445,7 +445,7 @@ static int fetch_with_import(struct transport *transport,
445445
if (data->refspecs)
446446
private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
447447
else
448-
private = strdup(posn->name);
448+
private = xstrdup(posn->name);
449449
read_ref(private, posn->old_sha1);
450450
free(private);
451451
}

0 commit comments

Comments
 (0)