@matheusbsbs / matheus-bagliano / commits / bda959c476
Fork 0

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>

Matheus Tavares committed Sep 28, 2020 at 21:01 UTC bda959c4766d73ab435f26f2cc7c8c67b9443f5a
1 file changed +5 -2
packfile.c
+5 -2
@@ -1463,7 +1463,7 @@ void clear_delta_base_cache(void)
1463 static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1464 void *base, unsigned long base_size, enum object_type type)
1465 {
1466 - struct delta_base_cache_entry *ent = xmalloc(sizeof(*ent));
1466 + struct delta_base_cache_entry *ent;
1467 struct list_head *lru, *tmp;
1468
1469 /*
@@ -1471,8 +1471,10 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1471 * is unpacking the same object, in unpack_entry() (since its phases I
1472 * and III might run concurrently across multiple threads).
1473 */
1474 - if (in_delta_base_cache(p, base_offset))
1474 + if (in_delta_base_cache(p, base_offset)) {
1475 + free(base);
1476 return;
1477 + }
1478
1479 delta_base_cached += base_size;
1480
@@ -1484,6 +1486,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1486 release_delta_base_cache(f);
1487 }
1488
1489 + ent = xmalloc(sizeof(*ent));
1490 ent->key.p = p;
1491 ent->key.base_offset = base_offset;
1492 ent->type = type;