Skip to content

Commit bda959c

Browse files
matheustavaresgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74b052f commit bda959c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

packfile.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,16 +1463,18 @@ void clear_delta_base_cache(void)
14631463
static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
14641464
void *base, unsigned long base_size, enum object_type type)
14651465
{
1466-
struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
1466+
struct delta_base_cache_entry *ent;
14671467
struct list_head *lru, *tmp;
14681468

14691469
/*
14701470
* Check required to avoid redundant entries when more than one thread
14711471
* is unpacking the same object, in unpack_entry() (since its phases I
14721472
* and III might run concurrently across multiple threads).
14731473
*/
1474-
if (in_delta_base_cache(p, base_offset))
1474+
if (in_delta_base_cache(p, base_offset)) {
1475+
free(base);
14751476
return;
1477+
}
14761478

14771479
delta_base_cached += base_size;
14781480

@@ -1484,6 +1486,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
14841486
release_delta_base_cache(f);
14851487
}
14861488

1489+
ent = xmalloc(sizeof(*ent));
14871490
ent->key.p = p;
14881491
ent->key.base_offset = base_offset;
14891492
ent->type = type;

0 commit comments

Comments
 (0)