]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sideband: fix leaks when configuring sideband colors
authorPatrick Steinhardt <ps@pks.im>
Thu, 22 Aug 2024 09:17:49 +0000 (11:17 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 22 Aug 2024 16:18:05 +0000 (09:18 -0700)
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>
sideband.c
t/t5409-colorize-remote-messages.sh

index 5d8907151fec3982e906025fc19c2165ef4572d3..27853736901069eb421c434dcb361037b32b08d9 100644 (file)
@@ -30,28 +30,27 @@ static int use_sideband_colors(void)
 
        const char *key = "color.remote";
        struct strbuf sb = STRBUF_INIT;
-       char *value;
+       const char *value;
        int i;
 
        if (use_sideband_colors_cached >= 0)
                return use_sideband_colors_cached;
 
-       if (!git_config_get_string(key, &value)) {
+       if (!git_config_get_string_tmp(key, &value))
                use_sideband_colors_cached = git_config_colorbool(key, value);
-       } else if (!git_config_get_string("color.ui", &value)) {
+       else if (!git_config_get_string_tmp("color.ui", &value))
                use_sideband_colors_cached = git_config_colorbool("color.ui", value);
-       } else {
+       else
                use_sideband_colors_cached = GIT_COLOR_AUTO;
-       }
 
        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))
+               if (git_config_get_string_tmp(sb.buf, &value))
                        continue;
+               color_parse(value, keywords[i].color);
        }
+
        strbuf_release(&sb);
        return use_sideband_colors_cached;
 }
index fa5de4500a4f50d48067ef6b1c05491b360a11f4..516b22fd9637ea6d7563100167088e8109180c9d 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='remote messages are colorized on the client'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '