@matheusbsbs / matheus-bagliano / commits / 20cfb3aa71
Fork 0

http*: copy string returned by sha1_to_hex

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Jun 6, 2009 at 16:43 UTC 20cfb3aa710d302829a776d7fbad2b89f71f15b6
2 files changed +47 -45
http-push.c
+24 -23
@@ -958,7 +958,8 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
958
959 static int fetch_index(unsigned char *sha1)
960 {
961 - char *hex = sha1_to_hex(sha1);
961 + int ret = 0;
962 + char *hex = xstrdup(sha1_to_hex(sha1));
963 char *filename;
964 char *url;
965 char tmpfile[PATH_MAX];
@@ -980,18 +981,18 @@ static int fetch_index(unsigned char *sha1)
981 if (start_active_slot(slot)) {
982 run_active_slot(slot);
983 if (results.curl_result != CURLE_OK) {
983 - free(url);
984 - return error("Unable to verify pack %s is available",
985 - hex);
984 + ret = error("Unable to verify pack %s is available",
985 + hex);
986 + goto cleanup_pack;
987 }
988 } else {
988 - free(url);
989 - return error("Unable to start request");
989 + ret = error("Unable to start request");
990 + goto cleanup_pack;
991 }
992
993 if (has_pack_index(sha1)) {
993 - free(url);
994 - return 0;
994 + ret = 0;
995 + goto cleanup_pack;
996 }
997
998 if (push_verbosely)
@@ -1003,9 +1004,9 @@ static int fetch_index(unsigned char *sha1)
1004 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
1005 indexfile = fopen(tmpfile, "a");
1006 if (!indexfile) {
1006 - free(url);
1007 - return error("Unable to open local file %s for pack index",
1008 - tmpfile);
1007 + ret = error("Unable to open local file %s for pack index",
1008 + tmpfile);
1009 + goto cleanup_pack;
1010 }
1011
1012 slot = get_active_slot();
@@ -1036,24 +1037,24 @@ static int fetch_index(unsigned char *sha1)
1037 if (start_active_slot(slot)) {
1038 run_active_slot(slot);
1039 if (results.curl_result != CURLE_OK) {
1039 - free(url);
1040 - fclose(indexfile);
1041 - slot->local = NULL;
1042 - return error("Unable to get pack index %s\n%s", url,
1043 - curl_errorstr);
1040 + ret = error("Unable to get pack index %s\n%s", url,
1041 + curl_errorstr);
1042 + goto cleanup_index;
1043 }
1044 } else {
1046 - free(url);
1047 - fclose(indexfile);
1048 - slot->local = NULL;
1049 - return error("Unable to start request");
1045 + ret = error("Unable to start request");
1046 + goto cleanup_index;
1047 }
1048
1052 - free(url);
1049 + ret = move_temp_to_file(tmpfile, filename);
1050 +
1051 +cleanup_index:
1052 fclose(indexfile);
1053 slot->local = NULL;
1055 -
1056 - return move_temp_to_file(tmpfile, filename);
1054 +cleanup_pack:
1055 + free(url);
1056 + free(hex);
1057 + return ret;
1058 }
1059
1060 static int setup_index(unsigned char *sha1)
http-walker.c
+23 -22
@@ -371,7 +371,8 @@ static void prefetch(struct walker *walker, unsigned char *sha1)
371
372 static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
373 {
374 - char *hex = sha1_to_hex(sha1);
374 + int ret = 0;
375 + char *hex = xstrdup(sha1_to_hex(sha1));
376 char *filename;
377 char *url;
378 char tmpfile[PATH_MAX];
@@ -394,18 +395,18 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
395 if (start_active_slot(slot)) {
396 run_active_slot(slot);
397 if (results.curl_result != CURLE_OK) {
397 - free(url);
398 - return error("Unable to verify pack %s is available",
398 + ret = error("Unable to verify pack %s is available",
399 hex);
400 + goto cleanup_pack;
401 }
402 } else {
402 - free(url);
403 - return error("Unable to start request");
403 + ret = error("Unable to start request");
404 + goto cleanup_pack;
405 }
406
407 if (has_pack_index(sha1)) {
407 - free(url);
408 - return 0;
408 + ret = 0;
409 + goto cleanup_pack;
410 }
411
412 if (walker->get_verbosely)
@@ -417,9 +418,9 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
418 snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
419 indexfile = fopen(tmpfile, "a");
420 if (!indexfile) {
420 - free(url);
421 - return error("Unable to open local file %s for pack index",
422 - tmpfile);
421 + ret = error("Unable to open local file %s for pack index",
422 + tmpfile);
423 + goto cleanup_pack;
424 }
425
426 slot = get_active_slot();
@@ -450,24 +451,24 @@ static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned ch
451 if (start_active_slot(slot)) {
452 run_active_slot(slot);
453 if (results.curl_result != CURLE_OK) {
453 - free(url);
454 - fclose(indexfile);
455 - slot->local = NULL;
456 - return error("Unable to get pack index %s\n%s", url,
457 - curl_errorstr);
454 + ret = error("Unable to get pack index %s\n%s", url,
455 + curl_errorstr);
456 + goto cleanup_index;
457 }
458 } else {
460 - free(url);
461 - fclose(indexfile);
462 - slot->local = NULL;
463 - return error("Unable to start request");
459 + ret = error("Unable to start request");
460 + goto cleanup_index;
461 }
462
466 - free(url);
463 + ret = move_temp_to_file(tmpfile, filename);
464 +
465 +cleanup_index:
466 fclose(indexfile);
467 slot->local = NULL;
469 -
470 - return move_temp_to_file(tmpfile, filename);
468 +cleanup_pack:
469 + free(url);
470 + free(hex);
471 + return ret;
472 }
473
474 static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)