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

Re: [PATCH 21/21] list-objects-filter-options: work around reported leak on error

From
Toon Claes <toon@iotcl.com>
Date
Oct 18, 2024, 12:04 UTC
Message-ID
<875xppd3ct.fsf@iotcl.com>
In-Reply-To
<6a2baf0d3e538e5f450c45c22248fbc3fefd77af.1728624670.git.ps@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 82 quoted lines
> This one is a little bit more curious. In t6112, we have a test that
> exercises the `git rev-list --filter` option with invalid filters. We
> execute git-rev-list(1) via `test_must_fail`, which means that we check
> for leaks even though Git exits with an error code. This causes the
> following leak:
>
>     Direct leak of 27 byte(s) in 1 object(s) allocated from:
>         #0 0x5555555e6946 in realloc.part.0 lsan_interceptors.cpp.o
>         #1 0x5555558fb4b6 in xrealloc wrapper.c:137:8
>         #2 0x5555558b6e06 in strbuf_grow strbuf.c:112:2
>         #3 0x5555558b7550 in strbuf_add strbuf.c:311:2
>         #4 0x5555557c1a88 in strbuf_addstr strbuf.h:310:2
>         #5 0x5555557c1d4c in parse_list_objects_filter list-objects-filter-options.c:261:3
>         #6 0x555555885ead in handle_revision_pseudo_opt revision.c:2899:3
>         #7 0x555555884e20 in setup_revisions revision.c:3014:11
>         #8 0x5555556c4b42 in cmd_rev_list builtin/rev-list.c:588:9
>         #9 0x5555555ec5e3 in run_builtin git.c:483:11
>         #10 0x5555555eb1e4 in handle_builtin git.c:749:13
>         #11 0x5555555ec001 in run_argv git.c:819:4
>         #12 0x5555555eaf94 in cmd_main git.c:954:19
>         #13 0x5555556fd569 in main common-main.c:64:11
>         #14 0x7ffff7ca714d in __libc_start_call_main (.../lib/libc.so.6+0x2a14d)
>         #15 0x7ffff7ca7208 in __libc_start_main@GLIBC_2.2.5 (.../libc.so.6+0x2a208)
>         #16 0x5555555ad064 in _start (git+0x59064)
>
> This leak is valid, as we call `die()` and do not clean up the memory at
> all. But what's curious is that this is the only leak reported, because
> we don't clean up any other allocated memory, either, and I have no idea
> why the leak sanitizer treats this buffer specially.
>
> In any case, we can work around the leak by shuffling things around a
> bit. Instead of calling `gently_parse_list_objects_filter()` and dying
> after we have modified the filter spec, we simply do so beforehand. Like
> this we don't allocate the buffer in the error case, which makes the
> reported leak go away.
>
> It's not pretty, but it manages to make t6112 leak free.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  list-objects-filter-options.c       | 17 +++++++----------
>  t/t6112-rev-list-filters-objects.sh |  1 +
>  2 files changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/list-objects-filter-options.c b/list-objects-filter-options.c
> index 00611107d20..fa72e81e4ad 100644
> --- a/list-objects-filter-options.c
> +++ b/list-objects-filter-options.c
> @@ -252,16 +252,14 @@ void parse_list_objects_filter(
>  	const char *arg)
>  {
>  	struct strbuf errbuf = STRBUF_INIT;
> -	int parse_error;
>  
>  	if (!filter_options->filter_spec.buf)
>  		BUG("filter_options not properly initialized");
>  
>  	if (!filter_options->choice) {
> +		if (gently_parse_list_objects_filter(filter_options, arg, &errbuf))
> +			die("%s", errbuf.buf);
>  		strbuf_addstr(&filter_options->filter_spec, arg);
> -
> -		parse_error = gently_parse_list_objects_filter(
> -			filter_options, arg, &errbuf);
>  	} else {
>  		struct list_objects_filter_options *sub;
>  
> @@ -271,18 +269,17 @@ void parse_list_objects_filter(
>  		 */
>  		transform_to_combine_type(filter_options);
>  
> -		strbuf_addch(&filter_options->filter_spec, '+');
> -		filter_spec_append_urlencode(filter_options, arg);
>  		ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
>  			      filter_options->sub_alloc);
>  		sub = &filter_options->sub[filter_options->sub_nr - 1];
>  
>  		list_objects_filter_init(sub);
> -		parse_error = gently_parse_list_objects_filter(sub, arg,
> -							       &errbuf);
> +		if (gently_parse_list_objects_filter(sub, arg, &errbuf))
> +			die("%s", errbuf.buf);

Do we actually have a test hitting this code path? I wanted to figure out why `filter_options->sub` wasn't leaky (I assume that's what you're talking about in your commit message), but I wasn't able to reproduce a scenario where we should die here.

-- Toon

Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 30 of 100 in “Memory leak fixes (pt.9)”
  1. 00/21 Memory leak fixes (pt.9)Patrick Steinhardt, Oct 11, 2024
  2. 01/21 builtin/ls-remote: plug leaking server optionsPatrick Steinhardt, Oct 11, 2024
  3. 02/21 t/helper: fix leaks in "reach" test toolPatrick Steinhardt, Oct 11, 2024
  4. 03/21 grep: fix leak in `grep_splice_or()`Patrick Steinhardt, Oct 11, 2024
  5. 04/21 builtin/grep: fix leak with `--max-count=0`Patrick Steinhardt, Oct 11, 2024
  6. 05/21 revision: fix leaking bloom filtersPatrick Steinhardt, Oct 11, 2024
  7. 06/21 diff-lib: fix leaking diffopts in `do_diff_cache()`Patrick Steinhardt, Oct 11, 2024
  8. Kristoffer HaugsbakkOct 21, 2024
  9. 07/21 pretty: clear signature checkPatrick Steinhardt, Oct 11, 2024
  10. Toon ClaesOct 18, 2024
  11. Patrick SteinhardtOct 21, 2024
  12. 08/21 upload-pack: fix leaking URI protocolsPatrick Steinhardt, Oct 11, 2024
  13. 09/21 builtin/commit: fix leaking change data contentsPatrick Steinhardt, Oct 11, 2024
  14. 10/21 trailer: fix leaking trailer valuesPatrick Steinhardt, Oct 11, 2024
  15. Toon ClaesOct 18, 2024
  16. Patrick SteinhardtOct 21, 2024
  17. 11/21 builtin/commit: fix leaking cleanup configPatrick Steinhardt, Oct 11, 2024
  18. 12/21 transport-helper: fix leaking import/export marksPatrick Steinhardt, Oct 11, 2024
  19. 13/21 builtin/tag: fix leaking key ID on failure to signPatrick Steinhardt, Oct 11, 2024
  20. 14/21 combine-diff: fix leaking lost linesPatrick Steinhardt, Oct 11, 2024
  21. 15/21 dir: release untracked cache dataPatrick Steinhardt, Oct 11, 2024
  22. 16/21 sparse-index: correctly free EWAH contentsPatrick Steinhardt, Oct 11, 2024
  23. 17/21 t/helper: stop re-initialization of `the_repository`Patrick Steinhardt, Oct 11, 2024
  24. 18/21 t/helper: fix leaking buffer in "dump-untracked-cache"Patrick Steinhardt, Oct 11, 2024
  25. 19/21 dir: fix leak when parsing "status.showUntrackedFiles"Patrick Steinhardt, Oct 11, 2024
  26. 20/21 builtin/merge: release outbut buffer after performing mergePatrick Steinhardt, Oct 11, 2024
  27. Toon ClaesOct 18, 2024
  28. Patrick SteinhardtOct 21, 2024
  29. 21/21 list-objects-filter-options: work around reported leak on errorPatrick Steinhardt, Oct 11, 2024
  30. Toon ClaesOct 18, 2024
  31. Patrick SteinhardtOct 21, 2024
  32. Taylor BlauOct 18, 2024
  33. Patrick SteinhardtOct 21, 2024
  34. 00/22 Memory leak fixes (pt.9)Patrick Steinhardt, Oct 21, 2024
  35. 01/22 builtin/ls-remote: plug leaking server optionsPatrick Steinhardt, Oct 21, 2024
  36. Justin ToblerNov 4, 2024
  37. Kristoffer HaugsbakkNov 4, 2024
  38. 02/22 t/helper: fix leaks in "reach" test toolPatrick Steinhardt, Oct 21, 2024
  39. 03/22 grep: fix leak in `grep_splice_or()`Patrick Steinhardt, Oct 21, 2024
  40. Kristoffer HaugsbakkOct 21, 2024
  41. 04/22 builtin/grep: fix leak with `--max-count=0`Patrick Steinhardt, Oct 21, 2024
  42. 05/22 revision: fix leaking bloom filtersPatrick Steinhardt, Oct 21, 2024
  43. 06/22 diff-lib: fix leaking diffopts in `do_diff_cache()`Patrick Steinhardt, Oct 21, 2024
  44. 07/22 pretty: clear signature checkPatrick Steinhardt, Oct 21, 2024
  45. Justin ToblerNov 4, 2024
  46. 08/22 upload-pack: fix leaking URI protocolsPatrick Steinhardt, Oct 21, 2024
  47. 09/22 builtin/commit: fix leaking change data contentsPatrick Steinhardt, Oct 21, 2024
  48. Justin ToblerNov 4, 2024
  49. 10/22 trailer: fix leaking trailer valuesPatrick Steinhardt, Oct 21, 2024
  50. Justin ToblerNov 4, 2024
  51. Patrick SteinhardtNov 5, 2024
  52. 11/22 trailer: fix leaking strbufs when formatting trailersPatrick Steinhardt, Oct 21, 2024
  53. Taylor BlauOct 21, 2024
  54. Justin ToblerNov 4, 2024
  55. Patrick SteinhardtNov 5, 2024
  56. 12/22 builtin/commit: fix leaking cleanup configPatrick Steinhardt, Oct 21, 2024
  57. 13/22 transport-helper: fix leaking import/export marksPatrick Steinhardt, Oct 21, 2024
  58. 14/22 builtin/tag: fix leaking key ID on failure to signPatrick Steinhardt, Oct 21, 2024
  59. 15/22 combine-diff: fix leaking lost linesPatrick Steinhardt, Oct 21, 2024
  60. Justin ToblerNov 4, 2024
  61. Patrick SteinhardtNov 5, 2024
  62. 16/22 dir: release untracked cache dataPatrick Steinhardt, Oct 21, 2024
  63. 17/22 sparse-index: correctly free EWAH contentsPatrick Steinhardt, Oct 21, 2024
  64. 18/22 t/helper: stop re-initialization of `the_repository`Patrick Steinhardt, Oct 21, 2024
  65. 19/22 t/helper: fix leaking buffer in "dump-untracked-cache"Patrick Steinhardt, Oct 21, 2024
  66. 20/22 dir: fix leak when parsing "status.showUntrackedFiles"Patrick Steinhardt, Oct 21, 2024
  67. 21/22 builtin/merge: release outbut buffer after performing mergePatrick Steinhardt, Oct 21, 2024
  68. Kristoffer HaugsbakkOct 21, 2024
  69. 22/22 list-objects-filter-options: work around reported leak on errorPatrick Steinhardt, Oct 21, 2024
  70. Kristoffer HaugsbakkOct 21, 2024
  71. Patrick SteinhardtOct 21, 2024
  72. Toon ClaesOct 25, 2024
  73. Justin ToblerNov 4, 2024
  74. Patrick SteinhardtNov 5, 2024
  75. 00/22 Memory leak fixes (pt.9)Patrick Steinhardt, Nov 5, 2024
  76. 01/22 builtin/ls-remote: plug leaking server optionsPatrick Steinhardt, Nov 5, 2024
  77. 02/22 t/helper: fix leaks in "reach" test toolPatrick Steinhardt, Nov 5, 2024
  78. 03/22 grep: fix leak in `grep_splice_or()`Patrick Steinhardt, Nov 5, 2024
  79. 04/22 builtin/grep: fix leak with `--max-count=0`Patrick Steinhardt, Nov 5, 2024
  80. 05/22 revision: fix leaking bloom filtersPatrick Steinhardt, Nov 5, 2024
  81. 06/22 diff-lib: fix leaking diffopts in `do_diff_cache()`Patrick Steinhardt, Nov 5, 2024
  82. 07/22 pretty: clear signature checkPatrick Steinhardt, Nov 5, 2024
  83. 08/22 upload-pack: fix leaking URI protocolsPatrick Steinhardt, Nov 5, 2024
  84. 09/22 builtin/commit: fix leaking change data contentsPatrick Steinhardt, Nov 5, 2024
  85. 10/22 trailer: fix leaking trailer valuesPatrick Steinhardt, Nov 5, 2024
  86. 11/22 trailer: fix leaking strbufs when formatting trailersPatrick Steinhardt, Nov 5, 2024
  87. 12/22 builtin/commit: fix leaking cleanup configPatrick Steinhardt, Nov 5, 2024
  88. 13/22 transport-helper: fix leaking import/export marksPatrick Steinhardt, Nov 5, 2024
  89. 14/22 builtin/tag: fix leaking key ID on failure to signPatrick Steinhardt, Nov 5, 2024
  90. 15/22 combine-diff: fix leaking lost linesPatrick Steinhardt, Nov 5, 2024
  91. 16/22 dir: release untracked cache dataPatrick Steinhardt, Nov 5, 2024
  92. 17/22 sparse-index: correctly free EWAH contentsPatrick Steinhardt, Nov 5, 2024
  93. 18/22 t/helper: stop re-initialization of `the_repository`Patrick Steinhardt, Nov 5, 2024
  94. 19/22 t/helper: fix leaking buffer in "dump-untracked-cache"Patrick Steinhardt, Nov 5, 2024
  95. 20/22 dir: fix leak when parsing "status.showUntrackedFiles"Patrick Steinhardt, Nov 5, 2024
  96. 21/22 builtin/merge: release output buffer after performing mergePatrick Steinhardt, Nov 5, 2024
  97. 22/22 list-objects-filter-options: work around reported leak on errorPatrick Steinhardt, Nov 5, 2024
  98. Junio C HamanoNov 5, 2024
  99. Patrick SteinhardtNov 5, 2024
  100. Justin ToblerNov 5, 2024

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.