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

[PATCH 2/4] check_connected(): fix leak of pack-index mmap

From
Jeff King <peff@peff.net>
Date
Mar 5, 2026, 23:09 UTC
Message-ID
<20260305230956.GB2901305@coredump.intra.peff.net>
In-Reply-To
<20260305230315.GA2354983@coredump.intra.peff.net>

Since c6807a40dc (clone: open a shortcut for connectivity check, 2013-05-26), we may open a one-off packed_git struct to check what's in the pack we just received. At the end of the function we throw away the struct (rather than linking it into the repository struct as usual).

We used to leak the struct until dd4143e7bf (connected.c: free the "struct packed_git", 2022-11-08), which calls free(). But that's not sufficient; inside the struct we'll have mmap'd the pack idx data from disk, which needs an munmap() call.

Building with SANITIZE=leak doesn't detect this, because we are leaking our own mmap(), and it only finds heap allocations from malloc(). But if we use our compat mmap implementation like this:

  make NO_MMAP=MapsBecomeMallocs SANITIZE=leak

then LSan will notice the leak, because now it's a regular heap buffer allocated by malloc().

We can fix it by calling close_pack(), which will free any associated memory. Note that we need to check for NULL ourselves; unlike free(), it is not safe to pass a NULL pointer to close_pack().

Signed-off-by: Jeff King <peff@peff.net>
---
 connected.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/connected.c b/connected.c
index 530357de54..6718503649 100644
--- a/connected.c
+++ b/connected.c
@@ -159,6 +159,9 @@ int check_connected(oid_iterate_fn fn, void *cb_data,
 		err = error_errno(_("failed to close rev-list's stdin"));
 
 	sigchain_pop(SIGPIPE);
-	free(new_pack);
+	if (new_pack) {
+		close_pack(new_pack);
+		free(new_pack);
+	}
 	return finish_command(&rev_list) || err;
 }
-- 
2.53.0.786.g466665faa3
Previous: Jacob KellerNext: Jacob Keller
Message 6 of 25 in “memory leak when cloning a repository”
  1. Jacob KellerMar 5, 2026
  2. Jeff KingMar 5, 2026
  3. 0/4 plugging some mmap() leaksJeff King, Mar 5, 2026
  4. 1/4 check_connected(): delay opening new_packJeff King, Mar 5, 2026
  5. Jacob KellerMar 5, 2026
  6. 2/4 check_connected(): fix leak of pack-index mmapJeff King, Mar 5, 2026
  7. Jacob KellerMar 5, 2026
  8. 3/4 pack-revindex: avoid double-loading .rev filesJeff King, Mar 5, 2026
  9. 4/4 Makefile: turn on NO_MMAP when building with LSanJeff King, Mar 5, 2026
  10. Jacob KellerMar 6, 2026
  11. 5/4 meson: turn on NO_MMAP when building with LSanJeff King, Mar 6, 2026
  12. Ramsay JonesMar 6, 2026
  13. Junio C HamanoMar 7, 2026
  14. 5/4 object-file: fix mmap() leak in odb_source_loose_read_object_stream()Jeff King, Mar 7, 2026
  15. Junio C HamanoMar 7, 2026
  16. Patrick SteinhardtMar 10, 2026
  17. Ramsay JonesMar 6, 2026
  18. Jeff KingMar 6, 2026
  19. Ramsay JonesMar 6, 2026
  20. Junio C HamanoMar 6, 2026
  21. Ramsay JonesMar 6, 2026
  22. Junio C HamanoMar 6, 2026
  23. Ramsay JonesMar 6, 2026
  24. Junio C HamanoMar 7, 2026
  25. Jacob KellerMar 5, 2026

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.