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

[PATCH 09/23] http*: copy string returned by sha1_to_hex

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Jun 6, 2009, 08:43 UTC
Message-ID
<20090606164336.09a76a43.rctay89@gmail.com>
In-Reply-To
<cover.1244277116.git.rctay89@gmail.com>

In the fetch_index implementations in http-push.c and http-walker.c, the string returned by sha1_to_hex is assumed to stay immutable.

This patch ensures that hex stays immutable by copying the string returned by sha1_to_hex (via xstrdup) and frees it subsequently. It also refactors free()'s and fclose()'s with labels.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 http-push.c   |   47 ++++++++++++++++++++++++-----------------------
 http-walker.c |   45 +++++++++++++++++++++++----------------------
 2 files changed, 47 insertions(+), 45 deletions(-)
diff --git a/http-push.c b/http-push.c
index 6ac0d63..c8cb6e4 100644
--- a/http-push.c
+++ b/http-push.c
@@ -958,7 +958,8 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)

 static int fetch_index(unsigned char *sha1)
 {
-	char *hex = sha1_to_hex(sha1);
+	int ret = 0;
+	char *hex = xstrdup(sha1_to_hex(sha1));
 	char *filename;
 	char *url;
 	char tmpfile[PATH_MAX];
@@ -980,18 +981,18 @@ static int fetch_index(unsigned char *sha1)
 	if (start_active_slot(slot)) {
 		run_active_slot(slot);
 		if (results.curl_result != CURLE_OK) {
-			free(url);
-			return error("Unable to verify pack %s is available",
-				     hex);
+			ret = error("Unable to verify pack %s is available",
+				    hex);
+			goto cleanup_pack;
 		}
 	} else {
-		free(url);
-		return error("Unable to start request");
+		ret = error("Unable to start request");
+		goto cleanup_pack;
 	}

 	if (has_pack_index(sha1)) {
-		free(url);
-		return 0;
+		ret = 0;
+		goto cleanup_pack;
 	}

 	if (push_verbosely)
@@ -1003,9 +1004,9 @@ static int fetch_index(unsigned char *sha1)
 	snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
 	indexfile = fopen(tmpfile, "a");
 	if (!indexfile) {
-		free(url);
-		return error("Unable to open local file %s for pack index",
-			     tmpfile);
+		ret = error("Unable to open local file %s for pack index",
+			    tmpfile);
+		goto cleanup_pack;
 	}

 	slot = get_active_slot();
@@ -1036,24 +1037,24 @@ static int fetch_index(unsigned char *sha1)
 	if (start_active_slot(slot)) {
 		run_active_slot(slot);
 		if (results.curl_result != CURLE_OK) {
-			free(url);
-			fclose(indexfile);
-			slot->local = NULL;
-			return error("Unable to get pack index %s\n%s", url,
-				     curl_errorstr);
+			ret = error("Unable to get pack index %s\n%s", url,
+				    curl_errorstr);
+			goto cleanup_index;
 		}
 	} else {
-		free(url);
-		fclose(indexfile);
-		slot->local = NULL;
-		return error("Unable to start request");
+		ret = error("Unable to start request");
+		goto cleanup_index;
 	}

-	free(url);
+	ret = move_temp_to_file(tmpfile, filename);
+
+cleanup_index:
 	fclose(indexfile);
 	slot->local = NULL;
-
-	return move_temp_to_file(tmpfile, filename);
+cleanup_pack:
+	free(url);
+	free(hex);
+	return ret;
 }

 static int setup_index(unsigned char *sha1)
diff --git a/http-walker.c b/http-walker.c
index bbc3023..d6cc622 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -371,7 +371,8 @@ static void prefetch(struct walker *walker, unsigned char *sha1)

 static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
 {
-	char *hex = sha1_to_hex(sha1);
+	int ret = 0;
+	char *hex = xstrdup(sha1_to_hex(sha1));
 	char *filename;
 	char *url;
 	char tmpfile[PATH_MAX];
@@ -394,18 +395,18 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
 	if (start_active_slot(slot)) {
 		run_active_slot(slot);
 		if (results.curl_result != CURLE_OK) {
-			free(url);
-			return error("Unable to verify pack %s is available",
+			ret = error("Unable to verify pack %s is available",
 				     hex);
+			goto cleanup_pack;
 		}
 	} else {
-		free(url);
-		return error("Unable to start request");
+		ret = error("Unable to start request");
+		goto cleanup_pack;
 	}

 	if (has_pack_index(sha1)) {
-		free(url);
-		return 0;
+		ret = 0;
+		goto cleanup_pack;
 	}

 	if (walker->get_verbosely)
@@ -417,9 +418,9 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
 	snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
 	indexfile = fopen(tmpfile, "a");
 	if (!indexfile) {
-		free(url);
-		return error("Unable to open local file %s for pack index",
-			     tmpfile);
+		ret = error("Unable to open local file %s for pack index",
+			    tmpfile);
+		goto cleanup_pack;
 	}

 	slot = get_active_slot();
@@ -450,24 +451,24 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
 	if (start_active_slot(slot)) {
 		run_active_slot(slot);
 		if (results.curl_result != CURLE_OK) {
-			free(url);
-			fclose(indexfile);
-			slot->local = NULL;
-			return error("Unable to get pack index %s\n%s", url,
-				     curl_errorstr);
+			ret = error("Unable to get pack index %s\n%s", url,
+				    curl_errorstr);
+			goto cleanup_index;
 		}
 	} else {
-		free(url);
-		fclose(indexfile);
-		slot->local = NULL;
-		return error("Unable to start request");
+		ret = error("Unable to start request");
+		goto cleanup_index;
 	}

-	free(url);
+	ret = move_temp_to_file(tmpfile, filename);
+
+cleanup_index:
 	fclose(indexfile);
 	slot->local = NULL;
-
-	return move_temp_to_file(tmpfile, filename);
+cleanup_pack:
+	free(url);
+	free(hex);
+	return ret;
 }

 static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
--
1.6.3.1
Previous: Tay Ray ChuanNext: Tay Ray Chuan
Message 9 of 23 in “t5540-http-push: test fetching of loose objects”
  1. 01/23 t5540-http-push: test fetching of loose objectsTay Ray Chuan, Jun 6, 2009
  2. 02/23 t5540-http-push: test fetching of packed objectsTay Ray Chuan, Jun 6, 2009
  3. 03/23 http*: cleanup slot->local after fcloseTay Ray Chuan, Jun 6, 2009
  4. 04/23 http-push: send out fetch requests on queueTay Ray Chuan, Jun 6, 2009
  5. 05/23 http-push: fix missing "#ifdef USE_CURL_MULTI" around "is_running_queue"Tay Ray Chuan, Jun 6, 2009
  6. 06/23 t5550-http-fetch: test fetching of packed objectsTay Ray Chuan, Jun 6, 2009
  7. 07/23 http-push, http-walker: style fixesTay Ray Chuan, Jun 6, 2009
  8. 08/23 http-walker: verify remote packsTay Ray Chuan, Jun 6, 2009
  9. 09/23 http*: copy string returned by sha1_to_hexTay Ray Chuan, Jun 6, 2009
  10. 10/23 http-push: do not SEGV after fetching a bad pack idx fileTay Ray Chuan, Jun 6, 2009
  11. 11/23 Don't expect verify_pack() callers to set pack_sizeTay Ray Chuan, Jun 6, 2009
  12. 12/23 transport.c::get_refs_via_curl(): do not leak refs_urlTay Ray Chuan, Jun 6, 2009
  13. 13/23 http*: move common variables and macros to http.[ch]Tay Ray Chuan, Jun 6, 2009
  14. 14/23 http: create function end_url_with_slashTay Ray Chuan, Jun 6, 2009
  15. 15/23 http.c: new functions for the http APITay Ray Chuan, Jun 6, 2009
  16. 16/23 transport.c::get_refs_via_curl(): use the new http APITay Ray Chuan, Jun 6, 2009
  17. 17/23 http.c::http_fetch_ref(): use the new http APITay Ray Chuan, Jun 6, 2009
  18. 18/23 http-push.c::remote_exists(): use the new http APITay Ray Chuan, Jun 6, 2009
  19. 19/23 http-push.c::fetch_symref(): use the new http APITay Ray Chuan, Jun 6, 2009
  20. 20/23 http*: add http_get_info_packsTay Ray Chuan, Jun 6, 2009
  21. 21/23 http: use new http API in fetch_index()Tay Ray Chuan, Jun 6, 2009
  22. 22/23 http*: add helper methods for fetching packsTay Ray Chuan, Jun 6, 2009
  23. 23/23 http*: add helper methods for fetching objects (loose)Tay Ray Chuan, Jun 6, 2009

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.