# [PATCH 1/2] Fix some more memory leaks in http-push.c

3 messages from 2007-12-14 to 2007-12-14. Participants: Mike Hommey, Junio C Hamano.
Thread: https://gitlist.dev/t/11298

## Mike Hommey, 2007-12-14 21:18

Subject: [PATCH 1/2] Fix some more memory leaks in http-push.c
Message-ID: <1197667081-9909-1-git-send-email-mh@glandium.org>
URL: https://gitlist.dev/e/1197667081-9909-1-git-send-email-mh%40glandium.org

```

Signed-off-by: Mike Hommey <mh@glandium.org>
---
 http-push.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/http-push.c b/http-push.c
index fffbe9c..fc60bfd 100644
--- a/http-push.c
+++ b/http-push.c
@@ -925,11 +925,14 @@ static int fetch_index(unsigned char *sha1)
 				     hex);
 		}
 	} else {
+		free(url);
 		return error("Unable to start request");
 	}
 
-	if (has_pack_index(sha1))
+	if (has_pack_index(sha1)) {
+		free(url);
 		return 0;
+	}
 
 	if (push_verbosely)
 		fprintf(stderr, "Getting index for pack %s\n", hex);
@@ -939,9 +942,11 @@ static int fetch_index(unsigned char *sha1)
 	filename = sha1_pack_index_name(sha1);
 	snprintf(tmpfile, sizeof(tmpfile), "%s.temp", filename);
 	indexfile = fopen(tmpfile, "a");
-	if (!indexfile)
+	if (!indexfile) {
+		free(url);
 		return error("Unable to open local file %s for pack index",
 			     tmpfile);
+	}
 
 	slot = get_active_slot();
 	slot->results = &results;
@@ -1135,10 +1140,12 @@ int fetch_ref(char *ref, unsigned char *sha1)
 	curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 	if (start_active_slot(slot)) {
 		run_active_slot(slot);
+		free(url);
 		if (results.curl_result != CURLE_OK)
 			return error("Couldn't get %s for %s\n%s",
 				     url, ref, curl_errorstr);
 	} else {
+		free(url);
 		return error("Unable to start request");
 	}
 
@@ -2107,6 +2114,7 @@ static int remote_exists(const char *path)
 
         if (start_active_slot(slot)) {
 		run_active_slot(slot);
+		free(url);
 		if (results.http_code == 404)
 			return 0;
 		else if (results.curl_result == CURLE_OK)
@@ -2114,6 +2122,7 @@ static int remote_exists(const char *path)
 		else
 			fprintf(stderr, "HEAD HTTP error %ld\n", results.http_code);
 	} else {
+		free(url);
 		fprintf(stderr, "Unable to start HEAD request\n");
 	}
 
-- 
1.5.4.rc0.8.gbf4af-dirty

```

## Mike Hommey, 2007-12-14 21:18

Subject: [PATCH 2/2] Fix random sha1 in error message in http-fetch and http-push
Message-ID: <1197667081-9909-2-git-send-email-mh@glandium.org>
URL: https://gitlist.dev/e/1197667081-9909-2-git-send-email-mh%40glandium.org
In-Reply-To: <1197667081-9909-1-git-send-email-mh@glandium.org>

```
When a downloaded ref doesn't contain a sha1, the error message displays
a random sha1 because of uninitialized memory. This happens when cloning
a repository that is already a clone of another one, in which case
refs/remotes/origin/HEAD is a symref.

Signed-off-by: Mike Hommey <mh@glandium.org>
---

Please note that this is already fixed in my strbuf patch for these files,
which had been applied in pu, but it seems to have disappeared from pu's
history. This also means the strbuf patch conflicts with this one. Please
tell me if you want a new strbuf patch made after this one.

 http-push.c   |    5 +++--
 http-walker.c |    5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/http-push.c b/http-push.c
index fc60bfd..7f1d043 100644
--- a/http-push.c
+++ b/http-push.c
@@ -1149,9 +1149,10 @@ int fetch_ref(char *ref, unsigned char *sha1)
 		return error("Unable to start request");
 	}
 
+	if (buffer.posn != 41)
+		return 1;
         hex[40] = '\0';
-        get_sha1_hex(hex, sha1);
-        return 0;
+        return get_sha1_hex(hex, sha1);
 }
 
 static void one_remote_object(const char *hex)
diff --git a/http-walker.c b/http-walker.c
index a3fb596..68b5108 100644
--- a/http-walker.c
+++ b/http-walker.c
@@ -986,9 +986,10 @@ static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
 		return error("Unable to start request");
 	}
 
+	if (buffer.posn != 41)
+		return 1;
         hex[40] = '\0';
-        get_sha1_hex(hex, sha1);
-        return 0;
+        return get_sha1_hex(hex, sha1);
 }
 
 static void cleanup(struct walker *walker)
-- 
1.5.4.rc0.8.gbf4af-dirty

```

## Junio C Hamano, 2007-12-14 22:17

Subject: Re: [PATCH 2/2] Fix random sha1 in error message in http-fetch and http-push
Message-ID: <7vbq8sq5ta.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbq8sq5ta.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1197667081-9909-2-git-send-email-mh@glandium.org>

```
Mike Hommey <mh@glandium.org> writes:

> Please note that this is already fixed in my strbuf patch for these files,
> which had been applied in pu, but it seems to have disappeared from pu's
> history. This also means the strbuf patch conflicts with this one. Please
> tell me if you want a new strbuf patch made after this one.

The branch 'pu' almost always rewind and gets rebuilt from the tip of
'next'.  Since I am more worried about master and next, even more so
than usual these days, most of the backburnered topics are not even in
'pu' but I still keep tips of them not to lose (they are found in
'offcuts').

Give me a few days to get back to your series.  I think it is a series
of good clean-up patches, (I just read [1/2] which was trivially correct
although I am not sure if it is the optimum fix to sprinkle free(url) to
everywhere we have return), but I just haven't got around to them.

```
