{"thread":{"id":"19686","subject":"[PATCH 09/23] http*: copy string returned by sha1_to_hex","startedAt":"2009-06-05T16:04:45Z","lastAt":"2009-06-05T16:04:45Z","messageCount":1,"participants":["Tay Ray Chuan"],"isPatch":true,"patchVersion":1,"patchTotal":23},"messages":[{"id":"115522","messageId":"20090606000445.326d2ef7.rctay89@gmail.com","threadId":"19686","inReplyTo":null,"subject":"[PATCH 09/23] http*: copy string returned by sha1_to_hex","fromName":"Tay Ray Chuan","fromEmail":"rctay89@gmail.com","sentAt":"2009-06-05T16:04:45Z","receivedAt":"2009-06-05T16:04:45Z","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   |   47 ++++++++++++++++++++++++-----------------------\n http-walker.c |   45 +++++++++++++++++++++++----------------------\n 2 files changed, 47 insertions(+), 45 deletions(-)\n\ndiff --git a/http-push.c b/http-push.c\nindex 6ac0d63..c8cb6e4 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -958,7 +958,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@@ -980,18 +981,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@@ -1003,9 +1004,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@@ -1036,24 +1037,24 @@ 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\tslot->local = NULL;\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\tslot->local = NULL;\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+\tret = move_temp_to_file(tmpfile, filename);\n+\n+cleanup_index:\n \tfclose(indexfile);\n \tslot->local = NULL;\n-\n-\treturn move_temp_to_file(tmpfile, filename);\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 bbc3023..d6cc622 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,24 +451,24 @@ 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\tslot->local = NULL;\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\tslot->local = NULL;\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+\tret = move_temp_to_file(tmpfile, filename);\n+\n+cleanup_index:\n \tfclose(indexfile);\n \tslot->local = NULL;\n-\n-\treturn move_temp_to_file(tmpfile, filename);\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.1\n"}]}