Use xmalloc() and friends to catch allocation failures

Some places use the standard malloc/strdup without checking if the
allocation was successful; they should use xmalloc/xstrdup that
check the memory allocation result.

Signed-off-by: Dotan Barak <dotanba@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Dotan Barak 2008-09-09 21:57:10 +03:00 коммит произвёл Junio C Hamano
Родитель 4886b89f8f
Коммит e8eec71d6e
5 изменённых файлов: 6 добавлений и 6 удалений

Просмотреть файл

@ -53,7 +53,7 @@ int cmd_http_fetch(int argc, const char **argv, const char *prefix)
} }
url = argv[arg]; url = argv[arg];
if (url && url[strlen(url)-1] != '/') { if (url && url[strlen(url)-1] != '/') {
rewritten_url = malloc(strlen(url)+2); rewritten_url = xmalloc(strlen(url)+2);
strcpy(rewritten_url, url); strcpy(rewritten_url, url);
strcat(rewritten_url, "/"); strcat(rewritten_url, "/");
url = rewritten_url; url = rewritten_url;

2
git.c
Просмотреть файл

@ -364,7 +364,7 @@ static void handle_internal_command(int argc, const char **argv)
if (sizeof(ext) > 1) { if (sizeof(ext) > 1) {
i = strlen(argv[0]) - strlen(ext); i = strlen(argv[0]) - strlen(ext);
if (i > 0 && !strcmp(argv[0] + i, ext)) { if (i > 0 && !strcmp(argv[0] + i, ext)) {
char *argv0 = strdup(argv[0]); char *argv0 = xstrdup(argv[0]);
argv[0] = cmd = argv0; argv[0] = cmd = argv0;
argv0[i] = '\0'; argv0[i] = '\0';
} }

Просмотреть файл

@ -2237,7 +2237,7 @@ int main(int argc, char **argv)
no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:"); no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
if (remote->url && remote->url[strlen(remote->url)-1] != '/') { if (remote->url && remote->url[strlen(remote->url)-1] != '/') {
rewritten_url = malloc(strlen(remote->url)+2); rewritten_url = xmalloc(strlen(remote->url)+2);
strcpy(rewritten_url, remote->url); strcpy(rewritten_url, remote->url);
strcat(rewritten_url, "/"); strcat(rewritten_url, "/");
remote->url = rewritten_url; remote->url = rewritten_url;

2
http.c
Просмотреть файл

@ -402,7 +402,7 @@ static struct fill_chain *fill_cfg = NULL;
void add_fill_function(void *data, int (*fill)(void *)) void add_fill_function(void *data, int (*fill)(void *))
{ {
struct fill_chain *new = malloc(sizeof(*new)); struct fill_chain *new = xmalloc(sizeof(*new));
struct fill_chain **linkp = &fill_cfg; struct fill_chain **linkp = &fill_cfg;
new->data = data; new->data = data;
new->fill = fill; new->fill = fill;

Просмотреть файл

@ -69,7 +69,7 @@ static const char *alias_url(const char *url)
if (!longest) if (!longest)
return url; return url;
ret = malloc(rewrite[longest_i]->baselen + ret = xmalloc(rewrite[longest_i]->baselen +
(strlen(url) - longest->len) + 1); (strlen(url) - longest->len) + 1);
strcpy(ret, rewrite[longest_i]->base); strcpy(ret, rewrite[longest_i]->base);
strcpy(ret + rewrite[longest_i]->baselen, url + longest->len); strcpy(ret + rewrite[longest_i]->baselen, url + longest->len);
@ -152,7 +152,7 @@ static struct branch *make_branch(const char *name, int len)
ret->name = xstrndup(name, len); ret->name = xstrndup(name, len);
else else
ret->name = xstrdup(name); ret->name = xstrdup(name);
refname = malloc(strlen(name) + strlen("refs/heads/") + 1); refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1);
strcpy(refname, "refs/heads/"); strcpy(refname, "refs/heads/");
strcpy(refname + strlen("refs/heads/"), ret->name); strcpy(refname + strlen("refs/heads/"), ret->name);
ret->refname = refname; ret->refname = refname;