Skip to content

Commit 0e0fefb

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0d1bd1d commit 0e0fefb

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,10 @@ static int git_default_core_config(const char *var, const char *value, void *cb)
16901690
if (!strcmp(var, "core.askpass"))
16911691
return git_config_string(&askpass_program, var, value);
16921692

1693-
if (!strcmp(var, "core.excludesfile"))
1693+
if (!strcmp(var, "core.excludesfile")) {
1694+
free((char *)excludes_file);
16941695
return git_config_pathname(&excludes_file, var, value);
1696+
}
16951697

16961698
if (!strcmp(var, "core.whitespace")) {
16971699
if (!value)

t/t7300-clean.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
test_description='git clean basic tests'
77

8+
TEST_PASSES_SANITIZE_LEAK=true
89
. ./test-lib.sh
910

1011
git config clean.requireForce no

0 commit comments

Comments
 (0)