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

[PATCH 2/2] format-patch: fix leak of rev_info in prepare_bases()

From
Jeff King <peff@peff.net>
Date
Jun 30, 2026, 06:43 UTC
Message-ID
<20260630064301.GB3733961@coredump.intra.peff.net>
In-Reply-To
<20260630063944.GA3733670@coredump.intra.peff.net>

In prepare_bases() we do a custom revision walk, separate from the main format-patch walk. After we finish, we fail to call release_revisions(), possibly leaking its contents.

We failed to notice it so far because the revision machinery doesn't always allocate. But at least one case can trigger the leak: if a commit graph is present, then the topo-walk allocates revs.topo_walk_info and some associated data structures. You can see it in the test suite by running:

  make SANITIZE=leak
  cd t
  GIT_TEST_COMMIT_GRAPH=1 ./t4014-format-patch.sh
which yields many entries like:
  ==git==3687620==ERROR: LeakSanitizer: detected memory leaks
  Direct leak of 200 byte(s) in 1 object(s) allocated from:
      #0 0x7f4ccba185cb in malloc ../../../../src/libsanitizer/lsan/lsan_interceptors.cpp:74
      #1 0x55cd452cdd0b in do_xmalloc wrapper.c:55
      #2 0x55cd452cdd9d in xmalloc wrapper.c:76
      #3 0x55cd45255473 in init_topo_walk revision.c:3845
      #4 0x55cd45255bef in prepare_revision_walk revision.c:4017
      #5 0x55cd44ffec40 in prepare_bases builtin/log.c:1872
      #6 0x55cd450010ec in cmd_format_patch builtin/log.c:2439

The un-released rev_info has been there since the code was added in fa2ab86d18 (format-patch: add '--base' option to record base tree info, 2016-04-26), but back then we didn't even have a way to release rev_info resources! The actual leak probably started around f0d9cc4196 (revision.c: begin refactoring --topo-order logic, 2018-11-01), but it's hard to bisect because there were so many other unrelated leaks back then.

So I'm not sure exactly when the leak started beyond "long ago", but it is easy-ish to find now (since we've plugged all those other leaks) and the solution is clear.

I didn't add a new test since we can demonstrate it with the existing ones, but it does require tweaking a test variable. We might consider ways to get more automatic leak-checking coverage there, but I think it should be done outside of this fix.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/log.c | 1 +
 1 file changed, 1 insertion(+)
diff --git a/builtin/log.c b/builtin/log.c
index d027ce1e0b..350b35c556 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1888,6 +1888,7 @@ static void prepare_bases(struct base_tree_info *bases,
 		bases->nr_patch_id++;
 	}
 	clear_commit_base(&commit_base);
+	release_revisions(&revs);
 }
 
 static void print_bases(struct base_tree_info *bases, FILE *file)
-- 
2.55.0.346.g83d0ea82e4
Previous: Jeff KingNext: Patrick Steinhardt
Message 3 of 15 in “small leak fix in format-patch”
  1. 0/2 small leak fix in format-patchJeff King, Jun 30, 2026
  2. 1/2 t: move LSan errors from stdout to stderrJeff King, Jun 30, 2026
  3. 2/2 format-patch: fix leak of rev_info in prepare_bases()Jeff King, Jun 30, 2026
  4. Patrick SteinhardtJun 30, 2026
  5. Jeff KingJul 1, 2026
  6. Patrick SteinhardtJul 1, 2026
  7. Jeff KingJul 1, 2026
  8. Patrick SteinhardtJul 1, 2026
  9. Jeff KingJul 2, 2026
  10. Patrick SteinhardtJul 2, 2026
  11. Junio C HamanoJul 3, 2026
  12. Jeff KingJul 6, 2026
  13. Patrick SteinhardtJul 6, 2026
  14. Karthik NayakJul 4, 2026
  15. Jeff KingJul 6, 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.