threads / patch / 20463

patchCheck return value of ftruncate call in http.c. Remove possible mem leak

Subject: [PATCH] Check return value of ftruncate call in http.c. Remove possible mem leak

## tl;dr

2 messages between Aug 8, 2009 and Aug 10, 2009. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Jeff Lasslett· Aug 8, 2009, 05:38 UTC · lore

In new_http_object_request(), check ftruncate() call return value and handle possible errors. Remove possible leak of mem pointed to by url.

Signed-off-by: Jeff Lasslett <jeff.lasslett@gmail.com>
---
 http.c |   12 +++++++++---
 1 files changed, 9 insertions(+), 3 deletions(-)
Show changes to http.c +9 −3
diff --git a/http.c b/http.c
index a2720d5..34a3a35 100644
--- a/http.c
+++ b/http.c
@@ -1098,7 +1098,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
 	char *hex = sha1_to_hex(sha1);
 	char *filename;
 	char prevfile[PATH_MAX];
-	char *url;
+	char *url = NULL;
 	int prevlocal;
 	unsigned char prev_buf[PREV_BUF_SIZE];
 	ssize_t prev_read = 0;
@@ -1189,7 +1189,11 @@ struct http_object_request *new_http_object_request(const char *base_url,
 		if (prev_posn>0) {
 			prev_posn = 0;
 			lseek(freq->localfile, 0, SEEK_SET);
-			ftruncate(freq->localfile, 0);
+			if (ftruncate(freq->localfile, 0) < 0) {
+				error("Couldn't truncate temporary file %s for %s: %s",
+					  freq->tmpfile, freq->filename, strerror(errno));
+				goto abort;
+			}
 		}
 	}
 
@@ -1216,10 +1220,12 @@ struct http_object_request *new_http_object_request(const char *base_url,
 				 CURLOPT_HTTPHEADER, range_header);
 	}
 
+	free(url);
 	return freq;
 
-	free(url);
 abort:
+	free(url);
+	free(freq->url);
 	free(filename);
 	free(freq);
 	return NULL;
-- 
1.6.4.59.g4d590.dirty
Junio C Hamano· Aug 10, 2009, 01:54 UTC · re: Jeff Lasslett · lore

Re: [PATCH] Check return value of ftruncate call in http.c. Remove possible mem leak

Jeff Lasslett <jeff.lasslett@gmail.com> writes:
> In new_http_object_request(), check ftruncate() call return value and
> handle possible errors.  Remove possible leak of mem pointed to by url.

Are you assuming that everybody is running libcurl 7.17.0 or later? Before that version strings given to easy_setopt() were not copied and you needed to keep them around.

I think freeing url is fine but you would probably need to then give freq->url to easy_setopt(CURLOPT_URL) if you wanted to do so.

← back to recent threads