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

Re: [PATCH 13/20] sideband: fix leaks when configuring sideband colors

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 20, 2024, 23:52 UTC
Message-ID
<xmqqv7zu3g6c.fsf@gitster.g>
In-Reply-To
<5d09959b6426e53a68e1bce547f9507bdf21bcde.1724159575.git.ps@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 20 quoted lines
> We read a bunch of configs in `use_sideband_colors()` to configure the
> colors that Git should use. We never free the strings read from the
> config though, causing memory leaks. Fix those.
>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  sideband.c                          | 8 +++++---
>  t/t5409-colorize-remote-messages.sh | 1 +
>  2 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/sideband.c b/sideband.c
> index 5d8907151fe..deb6ec0a8b7 100644
> --- a/sideband.c
> +++ b/sideband.c
> @@ -30,7 +30,7 @@ static int use_sideband_colors(void)
>  
>  	const char *key = "color.remote";
>  	struct strbuf sb = STRBUF_INIT;
> -	char *value;
> +	char *value = NULL;

Hmph, this is a bit unfortunate. If there is no configuration variable, on the first call to this function, we come to the end of if/else cascade and we need to free.

Another possibility to convey the intention better may be to assign NULL to value after the "we already know the value, so return early" took place.

Show 5 quoted lines
> @@ -43,15 +43,17 @@ static int use_sideband_colors(void)
>  	} else {
>  		use_sideband_colors_cached = GIT_COLOR_AUTO;
>  	}
> +	FREE_AND_NULL(value);

This can be a simple "free(value)"; I do not see the need to clear the variable at this point.

Show 9 quoted lines
>  	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>  		strbuf_reset(&sb);
>  		strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
>  		if (git_config_get_string(sb.buf, &value))
>  			continue;
> -		if (color_parse(value, keywords[i].color))
> -			continue;
> +		color_parse(value, keywords[i].color);
> +		FREE_AND_NULL(value);

Likewise. I do not see the need to clear. We only come here after we got something from gti_config_get_string(). That value may or may not fail color_parse(), but when we reach this point, value always has something we need to free, not some leftover value from the previous iteration.

The patch is _not_ wrong per-se, though.
Thanks.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 22 of 60 in “Memory leak fixes (pt.5)”
  1. 00/20 Memory leak fixes (pt.5)Patrick Steinhardt, Aug 20, 2024
  2. 01/20 mailinfo: fix leaking header dataPatrick Steinhardt, Aug 20, 2024
  3. Junio C HamanoAug 20, 2024
  4. 02/20 convert: fix leaks when resetting attributesPatrick Steinhardt, Aug 20, 2024
  5. Junio C HamanoAug 20, 2024
  6. 03/20 pretty: fix memory leaks when parsing pretty formatsPatrick Steinhardt, Aug 20, 2024
  7. Junio C HamanoAug 20, 2024
  8. Patrick SteinhardtAug 22, 2024
  9. 04/20 pretty: fix leaking key/value separator bufferPatrick Steinhardt, Aug 20, 2024
  10. 05/20 builtin/merge-tree: fix leaking `-X` strategy optionsPatrick Steinhardt, Aug 20, 2024
  11. Junio C HamanoAug 20, 2024
  12. 06/20 builtin/upload-archive: fix leaking args passed to `write_archive()`Patrick Steinhardt, Aug 20, 2024
  13. 07/20 builtin/archive: fix leaking `OPT_FILENAME()` valuePatrick Steinhardt, Aug 20, 2024
  14. 08/20 midx-write: fix leaking hashfile on error casesPatrick Steinhardt, Aug 20, 2024
  15. Junio C HamanoAug 20, 2024
  16. Patrick SteinhardtAug 22, 2024
  17. 09/20 builtin/repack: fix leaks when computing packs to repackPatrick Steinhardt, Aug 20, 2024
  18. 10/20 t/helper: fix leaking multi-pack-indices in "read-midx"Patrick Steinhardt, Aug 20, 2024
  19. 11/20 transport: fix leaking OID arrays in git:// transport dataPatrick Steinhardt, Aug 20, 2024
  20. 12/20 builtin/send-pack: fix leaking refspecsPatrick Steinhardt, Aug 20, 2024
  21. 13/20 sideband: fix leaks when configuring sideband colorsPatrick Steinhardt, Aug 20, 2024
  22. Junio C HamanoAug 20, 2024
  23. Patrick SteinhardtAug 22, 2024
  24. 14/20 builtin/fetch-pack: fix leaking refsPatrick Steinhardt, Aug 20, 2024
  25. Junio C HamanoAug 21, 2024
  26. 15/20 remote: fix leaking config stringsPatrick Steinhardt, Aug 20, 2024
  27. Junio C HamanoAug 21, 2024
  28. Patrick SteinhardtAug 22, 2024
  29. Junio C HamanoAug 22, 2024
  30. 16/20 remote: fix leaks when matching refspecsPatrick Steinhardt, Aug 20, 2024
  31. 17/20 remote: fix leaking peer ref when expanding refmapPatrick Steinhardt, Aug 20, 2024
  32. 18/20 builtin/fetch: fix leaking transaction with `--atomic`Patrick Steinhardt, Aug 20, 2024
  33. 19/20 transport: fix leaking arguments when fetching from bundlePatrick Steinhardt, Aug 20, 2024
  34. Junio C HamanoAug 21, 2024
  35. Patrick SteinhardtAug 22, 2024
  36. Junio C HamanoAug 22, 2024
  37. 20/20 transport: fix leaking negotiation tipsPatrick Steinhardt, Aug 20, 2024
  38. 00/20 Memory leak fixes (pt.5)Patrick Steinhardt, Aug 22, 2024
  39. 01/20 mailinfo: fix leaking header dataPatrick Steinhardt, Aug 22, 2024
  40. 02/20 convert: fix leaks when resetting attributesPatrick Steinhardt, Aug 22, 2024
  41. 03/20 pretty: fix memory leaks when parsing pretty formatsPatrick Steinhardt, Aug 22, 2024
  42. 04/20 pretty: fix leaking key/value separator bufferPatrick Steinhardt, Aug 22, 2024
  43. 05/20 builtin/merge-tree: fix leaking `-X` strategy optionsPatrick Steinhardt, Aug 22, 2024
  44. 06/20 builtin/upload-archive: fix leaking args passed to `write_archive()`Patrick Steinhardt, Aug 22, 2024
  45. 07/20 builtin/archive: fix leaking `OPT_FILENAME()` valuePatrick Steinhardt, Aug 22, 2024
  46. 08/20 midx-write: fix leaking hashfile on error casesPatrick Steinhardt, Aug 22, 2024
  47. 09/20 builtin/repack: fix leaks when computing packs to repackPatrick Steinhardt, Aug 22, 2024
  48. 10/20 t/helper: fix leaking multi-pack-indices in "read-midx"Patrick Steinhardt, Aug 22, 2024
  49. 11/20 transport: fix leaking OID arrays in git:// transport dataPatrick Steinhardt, Aug 22, 2024
  50. 12/20 builtin/send-pack: fix leaking refspecsPatrick Steinhardt, Aug 22, 2024
  51. 13/20 sideband: fix leaks when configuring sideband colorsPatrick Steinhardt, Aug 22, 2024
  52. 14/20 builtin/fetch-pack: fix leaking refsPatrick Steinhardt, Aug 22, 2024
  53. 15/20 remote: fix leaking config stringsPatrick Steinhardt, Aug 22, 2024
  54. 16/20 remote: fix leaks when matching refspecsPatrick Steinhardt, Aug 22, 2024
  55. 17/20 remote: fix leaking peer ref when expanding refmapPatrick Steinhardt, Aug 22, 2024
  56. 18/20 builtin/fetch: fix leaking transaction with `--atomic`Patrick Steinhardt, Aug 22, 2024
  57. 19/20 transport: fix leaking arguments when fetching from bundlePatrick Steinhardt, Aug 22, 2024
  58. Junio C HamanoAug 22, 2024
  59. 20/20 transport: fix leaking negotiation tipsPatrick Steinhardt, Aug 22, 2024
  60. Junio C HamanoAug 22, 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.