{"thread":{"id":"54643","subject":"[PATCH] diff-lib: plug minor memory leaks in do_diff_cache()","startedAt":"2020-11-14T18:37:30Z","lastAt":"2020-11-17T00:35:33Z","messageCount":2,"participants":["René Scharfe","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"409960","messageId":"80fb4a2a-992f-7d3b-9413-5059da3a8f01@web.de","threadId":"54643","inReplyTo":null,"subject":"[PATCH] diff-lib: plug minor memory leaks in do_diff_cache()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2020-11-14T18:37:03Z","receivedAt":"2020-11-14T18:37:30Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"do_diff_cache() builds a struct rev_info to hand to diff_cache() from\nscratch by initializing it using repo_init_revisions() and then\nreplacing its diffopt and prune_data members.\n\nThe diffopt member is initialized to a heap-allocated list of options,\nthough.  Release it using diff_setup_done() before overwriting it.\n\nThe initial value of the prune_data member doesn't need to be released,\nbut the copy created using copy_pathspec() does.  Clear it after use.\n\nSigned-off-by: René Scharfe <l.s.r@web.de>\n---\n diff-lib.c | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git a/diff-lib.c b/diff-lib.c\nindex 082e249fc3..b73cc1859a 100644\n--- a/diff-lib.c\n+++ b/diff-lib.c\n@@ -606,10 +606,12 @@ int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)\n\n \trepo_init_revisions(opt->repo, &revs, NULL);\n \tcopy_pathspec(&revs.prune_data, &opt->pathspec);\n+\tdiff_setup_done(&revs.diffopt);\n \trevs.diffopt = *opt;\n\n \tif (diff_cache(&revs, tree_oid, NULL, 1))\n \t\texit(128);\n+\tclear_pathspec(&revs.prune_data);\n \treturn 0;\n }\n\n--\n2.29.2\n"},{"id":"410055","messageId":"20201117003511.GC13516@coredump.intra.peff.net","threadId":"54643","inReplyTo":"80fb4a2a-992f-7d3b-9413-5059da3a8f01@web.de","subject":"Re: [PATCH] diff-lib: plug minor memory leaks in do_diff_cache()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-11-17T00:35:11Z","receivedAt":"2020-11-17T00:35:33Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Nov 14, 2020 at 07:37:03PM +0100, René Scharfe wrote:\n\n> do_diff_cache() builds a struct rev_info to hand to diff_cache() from\n> scratch by initializing it using repo_init_revisions() and then\n> replacing its diffopt and prune_data members.\n> \n> The diffopt member is initialized to a heap-allocated list of options,\n> though.  Release it using diff_setup_done() before overwriting it.\n\nMakes sense. This whole \"rewrite the options as a heap-allocated list\"\nthing is pretty gross, but is probably the least-bad solution to the\nproblem. I wondered if there might be other unpaired diff_setup() /\ndiff_setup_done() calls. Curiously, there are more of the latter:\n\n  $ git grep 'diff_setup(' | wc -l\n  22\n\n  $ git grep 'diff_setup_done(' | wc -l\n  35\n\nI think because repo_init_revisions() makes an implicit call to\ndiff_setup().\n\n> The initial value of the prune_data member doesn't need to be released,\n> but the copy created using copy_pathspec() does.  Clear it after use.\n\nI suspect there are more elements of rev_info that could be allocated\n(e.g., in a traversal without \"--objects\", I think trees and blobs are\nleft sitting in the pending array). It's a prime candidate for UNLEAK()\nin most cases where we do a single traversal and then exit the program.\nBut for sub-functions like this, we perhaps should bite the bullet and\njust make a rev_info_clear() function that can be used everywhere.\n\n(I'm not opposed to your patch here in the meantime, though).\n\n-Peff\n"}]}