git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] packfile: fix memory leak in add_delta_base_cache()

From
Matheus Tavares <matheus.bernardino@usp.br>
Date
Sep 29, 2020, 00:01 UTC
Message-ID
<f15f0c82fd779656dc03ce196d7c29acd63fb6be.1601337543.git.matheus.bernardino@usp.br>
In-Reply-To
<cover.1601337543.git.matheus.bernardino@usp.br>

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>
---
 packfile.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/packfile.c b/packfile.c
index 0319418d88..d31aaaeeaa 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1474,7 +1474,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;
 
 	/*
@@ -1482,8 +1482,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;
 
@@ -1495,6 +1497,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;
-- 
2.28.0
Previous: Phil Hord
Message 12 of 12 in “RFC - concurrency causes segfault in git grep since 2.26.0”
  1. Phil HordSep 25, 2020
  2. Matheus TavaresSep 25, 2020
  3. Phil HordSep 25, 2020
  4. 0/2 Fix race condition and memory leak in delta base cacheMatheus Tavares, Sep 28, 2020
  5. 1/2 packfile: fix race condition on unpack_entry()Matheus Tavares, Sep 28, 2020
  6. Junio C HamanoSep 28, 2020
  7. 2/2 packfile: fix memory leak in add_delta_base_cache()Matheus Tavares, Sep 28, 2020
  8. Junio C HamanoSep 28, 2020
  9. 0/2 Fix race condition and memory leak in delta base cacheMatheus Tavares, Sep 29, 2020
  10. 1/2 packfile: fix race condition on unpack_entry()Matheus Tavares, Sep 29, 2020
  11. Phil HordOct 2, 2020
  12. 2/2 packfile: fix memory leak in add_delta_base_cache()Matheus Tavares, Sep 29, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.