{"thread":{"id":"19372","subject":"[PATCH 04/11] http*: copy string returned by sha1_to_hex","startedAt":"2009-05-16T03:51:37Z","lastAt":"2009-05-16T03:51:37Z","messageCount":1,"participants":["Tay Ray Chuan"],"isPatch":true,"patchVersion":1,"patchTotal":11},"messages":[{"id":"114064","messageId":"20090516115137.ad454ab0.rctay89@gmail.com","threadId":"19372","inReplyTo":null,"subject":"[PATCH 04/11] http*: copy string returned by sha1_to_hex","fromName":"Tay Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-05-16T03:51:37Z","receivedAt":"2009-05-16T03:51:37Z","isPatch":true,"sender":{"key":"rctay89@gmail.com","avatar":"https://avatars.githubusercontent.com/u/61553?v=4"},"body":"In the fetch_index implementations in http-push.c and http-walker.c,\nthe string returned by sha1_to_hex is assumed to stay immutable.\n\nThis patch ensures that hex stays immutable by copying the string\nreturned by sha1_to_hex (via xstrdup) and frees it subsequently. It\nalso refactors free()'s and fclose()'s with labels.\n\nSigned-off-by: Tay Ray Chuan <rctay89@gmail.com>\n---\n http-push.c   |   45 ++++++++++++++++++++++++---------------------\n http-walker.c |   43 +++++++++++++++++++++++--------------------\n 2 files changed, 47 insertions(+), 41 deletions(-)\n\ndiff --git a/http-push.c b/http-push.c\nindex c55bd81..1011ca8 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -954,7 +954,8 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)\n \n static int fetch_index(unsigned char *sha1)\n {\n-\tchar *hex = sha1_to_hex(sha1);\n+\tint ret = 0;\n+\tchar *hex = xstrdup(sha1_to_hex(sha1));\n \tchar *filename;\n \tchar *url;\n \tchar tmpfile[PATH_MAX];\n@@ -976,18 +977,18 @@ static int fetch_index(unsigned char *sha1)\n \tif (start_active_slot(slot)) {\n \t\trun_active_slot(slot);\n \t\tif (results.curl_result != CURLE_OK) {\n-\t\t\tfree(url);\n-\t\t\treturn error(\"Unable to verify pack %s is available\",\n-\t\t\t\t     hex);\n+\t\t\tret = error(\"Unable to verify pack %s is available\",\n+\t\t\t\t    hex);\n+\t\t\tgoto cleanup_pack;\n \t\t}\n \t} else {\n-\t\tfree(url);\n-\t\treturn error(\"Unable to start request\");\n+\t\tret = error(\"Unable to start request\");\n+\t\tgoto cleanup_pack;\n \t}\n \n \tif (has_pack_index(sha1)) {\n-\t\tfree(url);\n-\t\treturn 0;\n+\t\tret = 0;\n+\t\tgoto cleanup_pack;\n \t}\n \n \tif (push_verbosely)\n@@ -999,9 +1000,9 @@ static int fetch_index(unsigned char *sha1)\n \tsnprintf(tmpfile, sizeof(tmpfile), \"%s.temp\", filename);\n \tindexfile = fopen(tmpfile, \"a\");\n \tif (!indexfile) {\n-\t\tfree(url);\n-\t\treturn error(\"Unable to open local file %s for pack index\",\n-\t\t\t     tmpfile);\n+\t\tret = error(\"Unable to open local file %s for pack index\",\n+\t\t\t    tmpfile);\n+\t\tgoto cleanup_pack;\n \t}\n \n \tslot = get_active_slot();\n@@ -1032,21 +1033,23 @@ static int fetch_index(unsigned char *sha1)\n \tif (start_active_slot(slot)) {\n \t\trun_active_slot(slot);\n \t\tif (results.curl_result != CURLE_OK) {\n-\t\t\tfree(url);\n-\t\t\tfclose(indexfile);\n-\t\t\treturn error(\"Unable to get pack index %s\\n%s\", url,\n-\t\t\t\t     curl_errorstr);\n+\t\t\tret = error(\"Unable to get pack index %s\\n%s\", url,\n+\t\t\t\t    curl_errorstr);\n+\t\t\tgoto cleanup_index;\n \t\t}\n \t} else {\n-\t\tfree(url);\n-\t\tfclose(indexfile);\n-\t\treturn error(\"Unable to start request\");\n+\t\tret = error(\"Unable to start request\");\n+\t\tgoto cleanup_index;\n \t}\n \n-\tfree(url);\n-\tfclose(indexfile);\n+\tret = move_temp_to_file(tmpfile, filename);\n \n-\treturn move_temp_to_file(tmpfile, filename);\n+cleanup_index:\n+\tfclose(indexfile);\n+cleanup_pack:\n+\tfree(url);\n+\tfree(hex);\n+\treturn ret;\n }\n \n static int setup_index(unsigned char *sha1)\ndiff --git a/http-walker.c b/http-walker.c\nindex cf46adb..8bf83d0 100644\n--- a/http-walker.c\n+++ b/http-walker.c\n@@ -371,7 +371,8 @@ static void prefetch(struct walker *walker, unsigned char *sha1)\n \n static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)\n {\n-\tchar *hex = sha1_to_hex(sha1);\n+\tint ret = 0;\n+\tchar *hex = xstrdup(sha1_to_hex(sha1));\n \tchar *filename;\n \tchar *url;\n \tchar tmpfile[PATH_MAX];\n@@ -394,18 +395,18 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch\n \tif (start_active_slot(slot)) {\n \t\trun_active_slot(slot);\n \t\tif (results.curl_result != CURLE_OK) {\n-\t\t\tfree(url);\n-\t\t\treturn error(\"Unable to verify pack %s is available\",\n+\t\t\tret = error(\"Unable to verify pack %s is available\",\n \t\t\t\t     hex);\n+\t\t\tgoto cleanup_pack;\n \t\t}\n \t} else {\n-\t\tfree(url);\n-\t\treturn error(\"Unable to start request\");\n+\t\tret = error(\"Unable to start request\");\n+\t\tgoto cleanup_pack;\n \t}\n \n \tif (has_pack_index(sha1)) {\n-\t\tfree(url);\n-\t\treturn 0;\n+\t\tret = 0;\n+\t\tgoto cleanup_pack;\n \t}\n \n \tif (walker->get_verbosely)\n@@ -417,9 +418,9 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch\n \tsnprintf(tmpfile, sizeof(tmpfile), \"%s.temp\", filename);\n \tindexfile = fopen(tmpfile, \"a\");\n \tif (!indexfile) {\n-\t\tfree(url);\n-\t\treturn error(\"Unable to open local file %s for pack index\",\n-\t\t\t     tmpfile);\n+\t\tret = error(\"Unable to open local file %s for pack index\",\n+\t\t\t    tmpfile);\n+\t\tgoto cleanup_pack;\n \t}\n \n \tslot = get_active_slot();\n@@ -450,21 +451,23 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch\n \tif (start_active_slot(slot)) {\n \t\trun_active_slot(slot);\n \t\tif (results.curl_result != CURLE_OK) {\n-\t\t\tfree(url);\n-\t\t\tfclose(indexfile);\n-\t\t\treturn error(\"Unable to get pack index %s\\n%s\", url,\n-\t\t\t\t     curl_errorstr);\n+\t\t\tret = error(\"Unable to get pack index %s\\n%s\", url,\n+\t\t\t\t    curl_errorstr);\n+\t\t\tgoto cleanup_index;\n \t\t}\n \t} else {\n-\t\tfree(url);\n-\t\tfclose(indexfile);\n-\t\treturn error(\"Unable to start request\");\n+\t\tret = error(\"Unable to start request\");\n+\t\tgoto cleanup_index;\n \t}\n \n-\tfree(url);\n-\tfclose(indexfile);\n+\tret = move_temp_to_file(tmpfile, filename);\n \n-\treturn move_temp_to_file(tmpfile, filename);\n+cleanup_index:\n+\tfclose(indexfile);\n+cleanup_pack:\n+\tfree(url);\n+\tfree(hex);\n+\treturn ret;\n }\n \n static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)\n-- \n1.6.3.rc0\n"}]}