Skip to content

Commit

Permalink
http.c: fix an invalid free()
Browse files Browse the repository at this point in the history
Remove a free() on the static buffer returned by sha1_file_name().

While we're at it, replace xmalloc() calls on the structs
http_(object|pack)_request with xcalloc() so that pointers in the
structs get initialized to NULL. That way, free()'s are safe - for
example, a free() on the url string member when aborting.

This fixes an invalid free().

Reported-by: Ævar Arnfjörð Bjarmason <[email protected]>
Helped-by: Jeff King [email protected]
Signed-off-by: Tay Ray Chuan <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
rctay authored and gitster committed Aug 3, 2011
1 parent b586744 commit ec99c9a
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,9 +1116,8 @@ struct http_pack_request *new_http_pack_request(
struct strbuf buf = STRBUF_INIT;
struct http_pack_request *preq;

preq = xmalloc(sizeof(*preq));
preq = xcalloc(1, sizeof(*preq));
preq->target = target;
preq->range_header = NULL;

end_url_with_slash(&buf, base_url);
strbuf_addf(&buf, "objects/pack/pack-%s.pack",
Expand Down Expand Up @@ -1210,7 +1209,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
struct curl_slist *range_header = NULL;
struct http_object_request *freq;

freq = xmalloc(sizeof(*freq));
freq = xcalloc(1, sizeof(*freq));
hashcpy(freq->sha1, sha1);
freq->localfile = -1;

Expand Down Expand Up @@ -1248,8 +1247,6 @@ struct http_object_request *new_http_object_request(const char *base_url,
goto abort;
}

memset(&freq->stream, 0, sizeof(freq->stream));

git_inflate_init(&freq->stream);

git_SHA1_Init(&freq->c);
Expand Down Expand Up @@ -1324,7 +1321,6 @@ struct http_object_request *new_http_object_request(const char *base_url,
return freq;

abort:
free(filename);
free(freq->url);
free(freq);
return NULL;
Expand Down

0 comments on commit ec99c9a

Please sign in to comment.