config: do not leak excludes_file

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>

Junio C Hamano committed Apr 6, 2024 at 11:11 UTC 0e0fefb29fde59b6703d3b45987823fe85c00706
2 files changed +4 -1
config.c
+3 -1
@@ -1690,8 +1690,10 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
1690 if (!strcmp(var, "core.askpass"))
1691 return git_config_string(&askpass_program, var, value);
1692
1693 - if (!strcmp(var, "core.excludesfile"))
1693 + if (!strcmp(var, "core.excludesfile")) {
1694 + free((char *)excludes_file);
1695 return git_config_pathname(&excludes_file, var, value);
1696 + }
1697
1698 if (!strcmp(var, "core.whitespace")) {
1699 if (!value)
t/t7300-clean.sh
+1
@@ -5,6 +5,7 @@
5
6 test_description='git clean basic tests'
7
8 +TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10
11 git config clean.requireForce no