]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: do not leak excludes_file
authorJunio C Hamano <gitster@pobox.com>
Sat, 6 Apr 2024 18:11:12 +0000 (11:11 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Apr 2024 20:20:24 +0000 (13:20 -0700)
The excludes_file variable is marked "const char *", but all the
assignments to it are made with a piece of memory allocated just
for it, and the variable is responsible for owning it.

When "core.excludesfile" is read, the code just lost the previous
value, leaking memory.  Plug it.

The real problem is that the variable is mistyped; our convention
is to never make a variable that owns the piece of memory pointed
by it as "const".  Fixing that would reduce the chance of this kind
of bug happening, and also would make it unnecessary to cast the
constness away while free()ing it, but that would be a much larger
follow-up effort.

Reported-by: Rubén Justo <rjusto@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
t/t7300-clean.sh

index aa2888d301a35f9ccd6d92aaae4ee177926f0424..146856567a6e4b1a9b740c814b92640d831d8268 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1690,8 +1690,10 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
        if (!strcmp(var, "core.askpass"))
                return git_config_string(&askpass_program, var, value);
 
-       if (!strcmp(var, "core.excludesfile"))
+       if (!strcmp(var, "core.excludesfile")) {
+               free((char *)excludes_file);
                return git_config_pathname(&excludes_file, var, value);
+       }
 
        if (!strcmp(var, "core.whitespace")) {
                if (!value)
index c975eb54d234d77b2f13df2a96acd4cbefb93865..529fc53339cfe3f612e45ac202859001ed707e76 100755 (executable)
@@ -5,6 +5,7 @@
 
 test_description='git clean basic tests'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 git config clean.requireForce no