{"thread":{"id":"10797","subject":"[PATCH 1/5] Fix memory leak in traverse_commit_list","startedAt":"2007-11-11T07:29:23Z","lastAt":"2007-11-11T07:29:23Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"59279","messageId":"20071111072923.GA19146@spearce.org","threadId":"10797","inReplyTo":null,"subject":"[PATCH 1/5] Fix memory leak in traverse_commit_list","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-11-11T07:29:23Z","receivedAt":"2007-11-11T07:29:23Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"If we were listing objects too then the objects were buffered in an\narray only reachable from a stack allocated structure.  When this\nfunction returns that array would be leaked as nobody would have\na reference to it anymore.\n\nHistorically this hasn't been a problem as the primary user of\ntraverse_commit_list() (the noble git-rev-list) would terminate\nas soon as the function was finished, thus allowing the operating\nsystem to cleanup memory.  However we have been leaking this data\nin git-pack-objects ever since that program learned how to run the\nrevision listing internally, rather than relying on reading object\nnames from git-rev-list.\n\nTo better facilitate reuse of traverse_commit_list during other\nbuiltin tools we shouldn't leak temporary memory like this and\ninstead we need to clean up properly after ourselves.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n list-objects.c |    7 +++++++\n 1 files changed, 7 insertions(+), 0 deletions(-)\n\ndiff --git a/list-objects.c b/list-objects.c\nindex e5c88c2..4ef58e7 100644\n--- a/list-objects.c\n+++ b/list-objects.c\n@@ -170,4 +170,11 @@ void traverse_commit_list(struct rev_info *revs,\n \t}\n \tfor (i = 0; i < objects.nr; i++)\n \t\tshow_object(&objects.objects[i]);\n+\tfree(objects.objects);\n+\tif (revs->pending.nr) {\n+\t\tfree(revs->pending.objects);\n+\t\trevs->pending.nr = 0;\n+\t\trevs->pending.alloc = 0;\n+\t\trevs->pending.objects = NULL;\n+\t}\n }\n-- \n1.5.3.5.1661.gcbf0\n"}]}