git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] http.c: fix an invalid free()

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Aug 3, 2011, 11:54 UTC
Message-ID
<1312372443-11508-1-git-send-email-rctay89@gmail.com>
In-Reply-To
<20110802033344.GA17494@sigill.intra.peff.net>
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 <avarab@gmail.com>
Helped-by: Jeff King peff@peff.net
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 http.c |    8 ++------
 1 files changed, 2 insertions(+), 6 deletions(-)
diff --git a/http.c b/http.c
index a1ea3db..a59cac4 100644
--- a/http.c
+++ b/http.c
@@ -1121,9 +1121,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",
@@ -1215,7 +1214,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;

@@ -1253,8 +1252,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);
@@ -1329,7 +1326,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;
--
1.7.6.11.g49037.dirty
Previous: Jeff King
Message 7 of 7 in “Possibly solved invalid free() in git-remote-http from Git 1.7.2.1”
  1. Ævar Arnfjörð BjarmasonAug 1, 2011
  2. Ævar Arnfjörð BjarmasonAug 1, 2011
  3. Ævar Arnfjörð BjarmasonAug 1, 2011
  4. Jeff KingAug 1, 2011
  5. Tay Ray ChuanAug 2, 2011
  6. Jeff KingAug 2, 2011
  7. http.c: fix an invalid free()Tay Ray Chuan, Aug 3, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.