Plug some more memory leaks in git
cover Plug some more memory leaks in git
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.
Signature · 1 line
Sergey Vlasov
PATCH 1/4 Plug memory leak in read_object_with_reference()
[PATCH] Plug memory leak in read_object_with_reference()
When following a reference, read_object_with_reference() did not free the intermediate object data.
---
sha1_file.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
f7d16d6b83698fd7858ad28340b3e87780322261
sha1_file.c +1 -0
@@ -1165,6 +1165,7 @@ void *read_object_with_reference(const ufree (buffer);return NULL ; } +free (buffer);/* Now we have the ID of the referred-to object in * actual_sha1. Check again. */ }
PATCH 2/4 Plug memory leak in write_sha1_to_fd()
In reply to Sergey Vlasov[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.
---
sha1_file.c | 14 ++++++++++++-- 1 files changed, 12 insertions(+), 2 deletions(-)
683b9e4bb090c115242392a1f1dc7b1a7c76c4be
sha1_file.c +12 -2
@@ -1297,8 +1297,11 @@ int write_sha1_to_fd(int fd, const unsigssize_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 unsigmemset (&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 unsigwhile (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 ; }
PATCH 3/4 Plug memory leak in sha1close()
In reply to Sergey Vlasov[PATCH] Plug memory leak in sha1close()
sha1create() and sha1fd() malloc the returned struct sha1file; sha1close() should free it.
---
csum-file.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
2207ae4977cbaa84636487ca24bad9d1116f54e0
csum-file.c +1 -0
@@ -45,6 +45,7 @@ int sha1close(struct sha1file *f, unsignsha1flush (f,20 );if (close (f->fd))die ("%s: sha1 file error on close (%s)" , f->name,strerror (errno)); +free (f);return 0 ; }
PATCH 4/4 Plug memory leak in git-pack-objects
In reply to Sergey Vlasov[PATCH] Plug memory leak in git-pack-objects
find_deltas() should free its temporary objects before returning.
---
pack-objects.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
8b38f80b97affd0d9808b8f276a9e2e04bf03464
pack-objects.c +4 -0
@@ -382,6 +382,10 @@ static void find_deltas(struct object_enif (idx >= window) idx =0 ; } + +for (i =0 ; i < window; ++i) +free (array[i].data); +free (array); }int main (int argc,char **argv)