git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] diff-lib: plug minor memory leaks in do_diff_cache()
@ 2020-11-14 18:37 René Scharfe
  2020-11-17  0:35 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: René Scharfe @ 2020-11-14 18:37 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano

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.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 diff-lib.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/diff-lib.c b/diff-lib.c
index 082e249fc3..b73cc1859a 100644
--- a/diff-lib.c
+++ b/diff-lib.c
@@ -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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] diff-lib: plug minor memory leaks in do_diff_cache()
  2020-11-14 18:37 [PATCH] diff-lib: plug minor memory leaks in do_diff_cache() René Scharfe
@ 2020-11-17  0:35 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2020-11-17  0:35 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git Mailing List, Junio C Hamano

On Sat, Nov 14, 2020 at 07:37:03PM +0100, René Scharfe wrote:

> 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().

> 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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-11-17  0:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-14 18:37 [PATCH] diff-lib: plug minor memory leaks in do_diff_cache() René Scharfe
2020-11-17  0:35 ` Jeff King

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git