# [PATCH 0/4] Plug some more memory leaks in git

5 messages from 2005-08-08 to 2005-08-08. Participants: Sergey Vlasov.
Thread: https://gitlist.dev/t/1453

## Sergey Vlasov, 2005-08-08 18:43

Subject: [PATCH 0/4] Plug some more memory leaks in git
Message-ID: <20050808184332.GA5789@procyon.home>
URL: https://gitlist.dev/e/20050808184332.GA5789%40procyon.home

```
Hello!

This set of patches fixes some more memory leaks which I have found in
git.  Especially the write_sha1_to_fd() leak was noticeable when
running git-ssh-push.

-- 
Sergey Vlasov

```

## Sergey Vlasov, 2005-08-08 18:44

Subject: [PATCH 1/4] Plug memory leak in read_object_with_reference()
Message-ID: <20050808184443.GB5789@procyon.home>
URL: https://gitlist.dev/e/20050808184443.GB5789%40procyon.home
In-Reply-To: <20050808184332.GA5789@procyon.home>

```
[PATCH] Plug memory leak in read_object_with_reference()

When following a reference, read_object_with_reference() did not free the
intermediate object data.

Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
---

 sha1_file.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

f7d16d6b83698fd7858ad28340b3e87780322261
diff --git a/sha1_file.c b/sha1_file.c
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1165,6 +1165,7 @@ void *read_object_with_reference(const u
 			free(buffer);
 			return NULL;
 		}
+		free(buffer);
 		/* Now we have the ID of the referred-to object in
 		 * actual_sha1.  Check again. */
 	}

```

## Sergey Vlasov, 2005-08-08 18:45

Subject: [PATCH 2/4] Plug memory leak in write_sha1_to_fd()
Message-ID: <20050808184536.GC5789@procyon.home>
URL: https://gitlist.dev/e/20050808184536.GC5789%40procyon.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;
 }
 

```

## Sergey Vlasov, 2005-08-08 18:46

Subject: [PATCH 3/4] Plug memory leak in sha1close()
Message-ID: <20050808184613.GD5789@procyon.home>
URL: https://gitlist.dev/e/20050808184613.GD5789%40procyon.home
In-Reply-To: <20050808184332.GA5789@procyon.home>

```
[PATCH] Plug memory leak in sha1close()

sha1create() and sha1fd() malloc the returned struct sha1file;
sha1close() should free it.

Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
---

 csum-file.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

2207ae4977cbaa84636487ca24bad9d1116f54e0
diff --git a/csum-file.c b/csum-file.c
--- a/csum-file.c
+++ b/csum-file.c
@@ -45,6 +45,7 @@ int sha1close(struct sha1file *f, unsign
 		sha1flush(f, 20);
 	if (close(f->fd))
 		die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
+	free(f);
 	return 0;
 }
 

```

## Sergey Vlasov, 2005-08-08 18:46

Subject: [PATCH 4/4] Plug memory leak in git-pack-objects
Message-ID: <20050808184658.GE5789@procyon.home>
URL: https://gitlist.dev/e/20050808184658.GE5789%40procyon.home
In-Reply-To: <20050808184332.GA5789@procyon.home>

```
[PATCH] Plug memory leak in git-pack-objects

find_deltas() should free its temporary objects before returning.

Signed-off-by: Sergey Vlasov <vsu@altlinux.ru>
---

 pack-objects.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

8b38f80b97affd0d9808b8f276a9e2e04bf03464
diff --git a/pack-objects.c b/pack-objects.c
--- a/pack-objects.c
+++ b/pack-objects.c
@@ -382,6 +382,10 @@ static void find_deltas(struct object_en
 		if (idx >= window)
 			idx = 0;
 	}
+
+	for (i = 0; i < window; ++i)
+		free(array[i].data);
+	free(array);
 }
 
 int main(int argc, char **argv)

```
