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

Re: [PATCH] for-each-ref: delay parsing of --sort=<atom> options

From
Jeff King <peff@peff.net>
Date
Oct 20, 2021, 02:20 UTC
Message-ID
<YW98+Lj9xVsR9u9Q@coredump.intra.peff.net>
In-Reply-To
<YW9EP5UNX0f+eOke@coredump.intra.peff.net>
On Tue, Oct 19, 2021 at 06:18:40PM -0400, Jeff King wrote:
Show 28 quoted lines
> > @@ -86,8 +86,6 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
> >  
> >  	packet_trace_identity("ls-remote");
> >  
> > -	UNLEAK(sorting);
> > -
> >  	if (argc > 1) {
> >  		int i;
> >  		CALLOC_ARRAY(pattern, argc);
> > @@ -139,8 +137,13 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
> >  		item->symref = xstrdup_or_null(ref->symref);
> >  	}
> >  
> > -	if (sorting)
> > +	if (sorting_options.nr) {
> > +		struct ref_sorting *sorting;
> > +		UNLEAK(sorting);
> > +
> > +		sorting = ref_sorting_options(&sorting_options);
> >  		ref_array_sort(sorting, &ref_array);
> > +	}
> 
> I wondered at first about pulling this UNLEAK() down, but it's because
> you move the "sorting" variable itself into the smaller scope. So this
> makes sense (and calling UNLEAK() before the pointer is set is perfectly
> fine, since it takes the address of the auto variable). It is a shame
> you can't just ref_sorting_free() afterwards, but we don't have that
> function yet. And adding it is way out of scope here. :)

Actually, I think I was wrong here. UNLEAK() will look at &sorting, but it will snapshot its data at the time of the call. So it won't do anything when the variable doesn't yet have a value.

You can demonstrate with:
  $ make SANITIZE=leak
  $ ./git ls-remote --sort=refname .
which will complain. Bumping it down like this:
diff --git a/builtin/ls-remote.c b/builtin/ls-remote.c
index 1e6017cdaa..a94a220256 100644
--- a/builtin/ls-remote.c
+++ b/builtin/ls-remote.c
@@ -139,10 +139,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix)
 
 	if (sorting_options.nr) {
 		struct ref_sorting *sorting;
-		UNLEAK(sorting);
 
 		sorting = ref_sorting_options(&sorting_options);
 		ref_array_sort(sorting, &ref_array);
+		UNLEAK(sorting);
 	}
 
 	for (i = 0; i < ref_array.nr; i++) {

clears it up. Note that there are other similar "leaks" (e.g., if you
give a pattern in argv[1]) which should be punted to another topic, but
I think you'd want to deal with this one since you're moving the
UNLEAK() around.

-Peff
Previous: Jeff KingNext: Ævar Arnfjörð Bjarmason
Message 3 of 20 in “for-each-ref: delay parsing of --sort=<atom> options”
  1. for-each-ref: delay parsing of --sort=<atom> optionsJunio C Hamano, Oct 18, 2021
  2. Jeff KingOct 19, 2021
  3. Jeff KingOct 20, 2021
  4. Ævar Arnfjörð BjarmasonOct 20, 2021
  5. 0/4 for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixesÆvar Arnfjörð Bjarmason, Oct 20, 2021
  6. 1/4 tag: use a "goto cleanup" pattern, leak less memoryÆvar Arnfjörð Bjarmason, Oct 20, 2021
  7. Junio C HamanoOct 20, 2021
  8. 2/4 ref-filter API user: add and use a ref_sorting_release()Ævar Arnfjörð Bjarmason, Oct 20, 2021
  9. Junio C HamanoOct 20, 2021
  10. 3/4 for-each-ref: delay parsing of --sort=<atom> optionsÆvar Arnfjörð Bjarmason, Oct 20, 2021
  11. 4/4 branch: use ref_sorting_release()Ævar Arnfjörð Bjarmason, Oct 20, 2021
  12. Junio C HamanoOct 20, 2021
  13. 0/3 ref-filter: add a ref_sorting_release()Ævar Arnfjörð Bjarmason, Oct 20, 2021
  14. 1/3 tag: use a "goto cleanup" pattern, leak less memoryÆvar Arnfjörð Bjarmason, Oct 20, 2021
  15. 2/3 ref-filter API user: add and use a ref_sorting_release()Ævar Arnfjörð Bjarmason, Oct 20, 2021
  16. 3/3 branch: use ref_sorting_release()Ævar Arnfjörð Bjarmason, Oct 20, 2021
  17. Junio C HamanoOct 20, 2021
  18. Jeff KingOct 20, 2021
  19. Junio C HamanoOct 20, 2021
  20. Jeff KingOct 21, 2021

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.