summaryrefslogtreecommitdiff
path: root/packfile.c
diff options
context:
space:
mode:
authorMatheus Tavares <matheus.bernardino@usp.br>2020-09-29 00:01:53 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-09-29 00:41:53 (GMT)
commitbda959c4766d73ab435f26f2cc7c8c67b9443f5a (patch)
treed7ddc14cf2d77d2a9a467ce6e2c83dece7618fcb /packfile.c
parent74b052f8c269ef0b6f61a5ecf04a8568399345d9 (diff)
downloadgit-bda959c4766d73ab435f26f2cc7c8c67b9443f5a.zip
git-bda959c4766d73ab435f26f2cc7c8c67b9443f5a.tar.gz
git-bda959c4766d73ab435f26f2cc7c8c67b9443f5a.tar.bz2
packfile: fix memory leak in add_delta_base_cache()
When add_delta_base_cache() is called with a base that is already in the cache, no operation is performed. But the check is done after allocating space for a new entry, so we end up leaking memory on the early return. In addition, the caller never free()'s the base as it expects the function to take ownership of it. But the base is not released when we skip insertion, so it also gets leaked. To fix these problems, move the allocation of a new entry further down in add_delta_base_cache(), and free() the base on early return. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'packfile.c')
-rw-r--r--packfile.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/packfile.c b/packfile.c
index bfe76b2..06d51ed 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1463,7 +1463,7 @@ void clear_delta_base_cache(void)
static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
void *base, unsigned long base_size, enum object_type type)
{
- struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
+ struct delta_base_cache_entry *ent;
struct list_head *lru, *tmp;
/*
@@ -1471,8 +1471,10 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
* is unpacking the same object, in unpack_entry() (since its phases I
* and III might run concurrently across multiple threads).
*/
- if (in_delta_base_cache(p, base_offset))
+ if (in_delta_base_cache(p, base_offset)) {
+ free(base);
return;
+ }
delta_base_cached += base_size;
@@ -1484,6 +1486,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
release_delta_base_cache(f);
}
+ ent = xmalloc(sizeof(*ent));
ent->key.p = p;
ent->key.base_offset = base_offset;
ent->type = type;