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

[PATCH 01/11] Fix memory leak in traverse_commit_list

From
Shawn O. Pearce <spearce@spearce.org>
Date
Nov 9, 2007, 11:06 UTC
Message-ID
<20071109110610.GA19368@spearce.org>

If we were listing objects too then the objects were buffered in an array only reachable from a stack allocated structure. When this function returns that array would be leaked as nobody would have a reference to it anymore.

Historically this hasn't been a problem as the primary user of traverse_commit_list() (the noble git-rev-list) would terminate as soon as the function was finished, thus allowing the operating system to cleanup memory. However we have been leaking this data in git-pack-objects ever since that program learned how to run the revision listing internally, rather than relying on reading object names from git-rev-list.

To better facilitate reuse of traverse_commit_list during other builtin tools (such as git-fetch) we shouldn't leak temporary memory like this and instead we need to clean up properly after ourselves.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 list-objects.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/list-objects.c b/list-objects.c
index e5c88c2..713bef9 100644
--- a/list-objects.c
+++ b/list-objects.c
@@ -170,4 +170,11 @@ void traverse_commit_list(struct rev_info *revs,
 	}
 	for (i = 0; i < objects.nr; i++)
 		show_object(&objects.objects[i]);
+	free(objects.objects);
+	if (revs->pending.nr) {
+		revs->pending.nr = 0;
+		revs->pending.alloc = 0;
+		revs->pending.objects = NULL;
+		free(revs->pending.objects);
+	}
 }
-- 
1.5.3.5.1622.g41d10
Next: Junio C Hamano
Message 1 of 4 in “Fix memory leak in traverse_commit_list”
  1. 01/11 Fix memory leak in traverse_commit_listShawn O. Pearce, Nov 9, 2007
  2. Junio C HamanoNov 9, 2007
  3. Johannes SchindelinNov 9, 2007
  4. Shawn O. PearceNov 11, 2007

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.