Fix various memory leaks in http-push.c and http-walker.c
Signed-off-by: Mike Hommey <mh@glandium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Mike Hommey committed
Dec 10, 2007 at 22:36 UTC
3a462bc9ba8cc46dcd5e49ae884968e1921c265f
2 files changed
+45
-28
http-push.c
+20
-13
@@ -1119,6 +1119,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
1119
char *base = remote->url;
1120
struct active_request_slot *slot;
1121
struct slot_results results;
1122
+ int ret;
1123
1124
url = quote_ref_url(base, ref);
1125
slot = get_active_slot();
@@ -1129,19 +1130,23 @@ int fetch_ref(char *ref, unsigned char *sha1)
1130
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1131
if (start_active_slot(slot)) {
1132
run_active_slot(slot);
1132
- free(url);
1133
- if (results.curl_result != CURLE_OK)
1134
- return error("Couldn't get %s for %s\n%s",
1135
- url, ref, curl_errorstr);
1133
+ if (results.curl_result == CURLE_OK) {
1134
+ strbuf_rtrim(&buffer);
1135
+ if (buffer.len == 40)
1136
+ ret = get_sha1_hex(buffer.buf, sha1);
1137
+ else
1138
+ ret = 1;
1139
+ } else {
1140
+ ret = error("Couldn't get %s for %s\n%s",
1141
+ url, ref, curl_errorstr);
1142
+ }
1143
} else {
1137
- free(url);
1138
- return error("Unable to start request");
1144
+ ret = error("Unable to start request");
1145
}
1146
1141
- strbuf_rtrim(&buffer);
1142
- if (buffer.len != 40)
1143
- return 1;
1144
- return get_sha1_hex(buffer.buf, sha1);
1147
+ strbuf_release(&buffer);
1148
+ free(url);
1149
+ return ret;
1150
}
1151
1152
static void one_remote_object(const char *hex)
@@ -2043,6 +2048,7 @@ static int remote_exists(const char *path)
2048
char *url = xmalloc(strlen(remote->url) + strlen(path) + 1);
2049
struct active_request_slot *slot;
2050
struct slot_results results;
2051
+ int ret = -1;
2052
2053
sprintf(url, "%s%s", remote->url, path);
2054
@@ -2055,9 +2061,9 @@ static int remote_exists(const char *path)
2061
run_active_slot(slot);
2062
free(url);
2063
if (results.http_code == 404)
2058
- return 0;
2064
+ ret = 0;
2065
else if (results.curl_result == CURLE_OK)
2060
- return 1;
2066
+ ret = 1;
2067
else
2068
fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
2069
} else {
@@ -2065,7 +2071,8 @@ static int remote_exists(const char *path)
2071
fprintf(stderr, "Unable to start HEAD request\n");
2072
}
2073
2068
- return -1;
2074
+ free(url);
2075
+ return ret;
2076
}
2077
2078
static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
http-walker.c
+25
-15
@@ -644,6 +644,7 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
644
struct strbuf buffer = STRBUF_INIT;
645
char *data;
646
int i = 0;
647
+ int ret = 0;
648
649
struct active_request_slot *slot;
650
struct slot_results results;
@@ -666,19 +667,19 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
667
if (start_active_slot(slot)) {
668
run_active_slot(slot);
669
if (results.curl_result != CURLE_OK) {
669
- strbuf_release(&buffer);
670
if (missing_target(&results)) {
671
repo->got_indices = 1;
672
- return 0;
672
+ goto cleanup;
673
} else {
674
repo->got_indices = 0;
675
- return error("%s", curl_errorstr);
675
+ ret = error("%s", curl_errorstr);
676
+ goto cleanup;
677
}
678
}
679
} else {
680
repo->got_indices = 0;
680
- strbuf_release(&buffer);
681
- return error("Unable to start request");
681
+ ret = error("Unable to start request");
682
+ goto cleanup;
683
}
684
685
data = buffer.buf;
@@ -701,9 +702,11 @@ static int fetch_indices(struct walker *walker, struct alt_base *repo)
702
i++;
703
}
704
704
- strbuf_release(&buffer);
705
repo->got_indices = 1;
706
- return 0;
706
+cleanup:
707
+ strbuf_release(&buffer);
708
+ free(url);
709
+ return ret;
710
}
711
712
static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
@@ -939,6 +942,7 @@ static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
942
const char *base = data->alt->base;
943
struct active_request_slot *slot;
944
struct slot_results results;
945
+ int ret;
946
947
url = quote_ref_url(base, ref);
948
slot = get_active_slot();
@@ -949,17 +953,23 @@ static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
953
curl_easy_setopt(slot->curl, CURLOPT_URL, url);
954
if (start_active_slot(slot)) {
955
run_active_slot(slot);
952
- if (results.curl_result != CURLE_OK)
953
- return error("Couldn't get %s for %s\n%s",
954
- url, ref, curl_errorstr);
956
+ if (results.curl_result == CURLE_OK) {
957
+ strbuf_rtrim(&buffer);
958
+ if (buffer.len == 40)
959
+ ret = get_sha1_hex(buffer.buf, sha1);
960
+ else
961
+ ret = 1;
962
+ } else {
963
+ ret = error("Couldn't get %s for %s\n%s",
964
+ url, ref, curl_errorstr);
965
+ }
966
} else {
956
- return error("Unable to start request");
967
+ ret = error("Unable to start request");
968
}
969
959
- strbuf_rtrim(&buffer);
960
- if (buffer.len != 40)
961
- return 1;
962
- return get_sha1_hex(buffer.buf, sha1);
970
+ strbuf_release(&buffer);
971
+ free(url);
972
+ return ret;
973
}
974
975
static void cleanup(struct walker *walker)