sideband: fix leaks when configuring sideband colors

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. Refactor the code to use `git_config_get_string_tmp()` instead, which does not allocate memory. As we throw the strings away after parsing them anyway there is no need to use allocated strings. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 22, 2024 at 11:17 UTC 2a2d5da1f22f91242c482bd7800b3dd03915870a
2 files changed +8 -8
sideband.c
+7 -8
@@ -30,28 +30,27 @@ static int use_sideband_colors(void)
30
31 const char *key = "color.remote";
32 struct strbuf sb = STRBUF_INIT;
33 - char *value;
33 + const char *value;
34 int i;
35
36 if (use_sideband_colors_cached >= 0)
37 return use_sideband_colors_cached;
38
39 - if (!git_config_get_string(key, &value)) {
39 + if (!git_config_get_string_tmp(key, &value))
40 use_sideband_colors_cached = git_config_colorbool(key, value);
41 - } else if (!git_config_get_string("color.ui", &value)) {
41 + else if (!git_config_get_string_tmp("color.ui", &value))
42 use_sideband_colors_cached = git_config_colorbool("color.ui", value);
43 - } else {
43 + else
44 use_sideband_colors_cached = GIT_COLOR_AUTO;
45 - }
45
46 for (i = 0; i < ARRAY_SIZE(keywords); i++) {
47 strbuf_reset(&sb);
48 strbuf_addf(&sb, "%s.%s", key, keywords[i].keyword);
50 - if (git_config_get_string(sb.buf, &value))
51 - continue;
52 - if (color_parse(value, keywords[i].color))
49 + if (git_config_get_string_tmp(sb.buf, &value))
50 continue;
51 + color_parse(value, keywords[i].color);
52 }
53 +
54 strbuf_release(&sb);
55 return use_sideband_colors_cached;
56 }
t/t5409-colorize-remote-messages.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='remote messages are colorized on the client'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '