Skip to contentSkip to conversation

diff-lib: plug minor memory leaks in do_diff_cache()

PATCH In review René Scharfe 2 participants 2 messages Nov 14, 2020 – Nov 17, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff-lib: plug minor memory leaks in do_diff_cache()

do_diff_cache() builds a struct rev_info to hand to diff_cache() from scratch by initializing it using repo_init_revisions() and then replacing its diffopt and prune_data members.

The diffopt member is initialized to a heap-allocated list of options, though. Release it using diff_setup_done() before overwriting it.

The initial value of the prune_data member doesn't need to be released, but the copy created using copy_pathspec() does. Clear it after use.

---
 diff-lib.c | 2 ++
 1 file changed, 2 insertions(+)
diff-lib.c +2 -0
index 082e249fc3..b73cc1859a 100644
@@ -606,10 +606,12 @@ int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
 
 	repo_init_revisions(opt->repo, &revs, NULL);
 	copy_pathspec(&revs.prune_data, &opt->pathspec);
+	diff_setup_done(&revs.diffopt);
 	revs.diffopt = *opt;
 
 	if (diff_cache(&revs, tree_oid, NULL, 1))
 		exit(128);
+	clear_pathspec(&revs.prune_data);
 	return 0;
 }
 

--
2.29.2

#1 lore raw
Quoted 6 lines · René Scharfe

do_diff_cache() builds a struct rev_info to hand to diff_cache() from scratch by initializing it using repo_init_revisions() and then replacing its diffopt and prune_data members.

The diffopt member is initialized to a heap-allocated list of options, though. Release it using diff_setup_done() before overwriting it.

Makes sense. This whole "rewrite the options as a heap-allocated list" thing is pretty gross, but is probably the least-bad solution to the problem. I wondered if there might be other unpaired diff_setup() / diff_setup_done() calls. Curiously, there are more of the latter:

  $ git grep 'diff_setup(' | wc -l
  22
  $ git grep 'diff_setup_done(' | wc -l
  35

I think because repo_init_revisions() makes an implicit call to diff_setup().

Quoted 2 lines

The initial value of the prune_data member doesn't need to be released, but the copy created using copy_pathspec() does. Clear it after use.

I suspect there are more elements of rev_info that could be allocated (e.g., in a traversal without "--objects", I think trees and blobs are left sitting in the pending array). It's a prime candidate for UNLEAK() in most cases where we do a single traversal and then exit the program. But for sub-functions like this, we perhaps should bite the bullet and just make a rev_info_clear() function that can be used everywhere.

(I'm not opposed to your patch here in the meantime, though).

-Peff