git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] Plug memory leak in write_sha1_to_fd()

From
Sergey Vlasov <vsu@altlinux.ru>
Date
Aug 8, 2005, 18:45 UTC
Message-ID
<20050808184536.GC5789@procyon.home>
In-Reply-To
<20050808184332.GA5789@procyon.home>
[PATCH] Plug memory leak in write_sha1_to_fd()

If the object to write was packed, both its uncompressed and compressed data were leaked. If the object was not packed, its file was not unmapped.

Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
---
 sha1_file.c |   14 ++++++++++++--
 1 files changed, 12 insertions(+), 2 deletions(-)
683b9e4bb090c115242392a1f1dc7b1a7c76c4be
diff --git a/sha1_file.c b/sha1_file.c
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1297,8 +1297,11 @@ int write_sha1_to_fd(int fd, const unsig
 	ssize_t size;
 	unsigned long objsize;
 	int posn = 0;
-	void *buf = map_sha1_file_internal(sha1, &objsize);
+	void *map = map_sha1_file_internal(sha1, &objsize);
+	void *buf = map;
+	void *temp_obj = NULL;
 	z_stream stream;
+
 	if (!buf) {
 		unsigned char *unpacked;
 		unsigned long len;
@@ -1314,7 +1317,7 @@ int write_sha1_to_fd(int fd, const unsig
 		memset(&stream, 0, sizeof(stream));
 		deflateInit(&stream, Z_BEST_COMPRESSION);
 		size = deflateBound(&stream, len + hdrlen);
-		buf = xmalloc(size);
+		temp_obj = buf = xmalloc(size);
 
 		/* Compress it */
 		stream.next_out = buf;
@@ -1332,6 +1335,7 @@ int write_sha1_to_fd(int fd, const unsig
 		while (deflate(&stream, Z_FINISH) == Z_OK)
 			/* nothing */;
 		deflateEnd(&stream);
+		free(unpacked);
 		
 		objsize = stream.total_out;
 	}
@@ -1348,6 +1352,12 @@ int write_sha1_to_fd(int fd, const unsig
 		}
 		posn += size;
 	} while (posn < objsize);
+
+	if (map)
+		munmap(map, objsize);
+	if (temp_obj)
+		free(temp_obj);
+
 	return 0;
 }
 
Previous: Sergey VlasovNext: Sergey Vlasov
Message 3 of 5 in “Plug some more memory leaks in git”
  1. 0/4 Plug some more memory leaks in gitSergey Vlasov, Aug 8, 2005
  2. 1/4 Plug memory leak in read_object_with_reference()Sergey Vlasov, Aug 8, 2005
  3. 2/4 Plug memory leak in write_sha1_to_fd()Sergey Vlasov, Aug 8, 2005
  4. 3/4 Plug memory leak in sha1close()Sergey Vlasov, Aug 8, 2005
  5. 4/4 Plug memory leak in git-pack-objectsSergey Vlasov, Aug 8, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.