builtin/blame: fix leaking prefixed paths

In `cmd_blame()` we compute prefixed paths by calling `add_prefix()`, which itself calls `prefix_path()`. While `prefix_path()` returns an allocated string, `add_prefix()` pretends to return a constant string. Consequently, this path never gets freed. Fix the return type to be `char *` and free the path to plug the memory leak. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 11, 2024 at 11:21 UTC 3332f35577ccbb51a50d88d16caafcceaab23767
5 files changed +9 -2
builtin/blame.c
+3 -2
@@ -687,7 +687,7 @@ static unsigned parse_score(const char *arg)
687 return score;
688 }
689
690 -static const char *add_prefix(const char *prefix, const char *path)
690 +static char *add_prefix(const char *prefix, const char *path)
691 {
692 return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
693 }
@@ -865,7 +865,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,
865 int cmd_blame(int argc, const char **argv, const char *prefix)
866 {
867 struct rev_info revs;
868 - const char *path;
868 + char *path = NULL;
869 struct blame_scoreboard sb;
870 struct blame_origin *o;
871 struct blame_entry *ent = NULL;
@@ -1226,6 +1226,7 @@ parse_done:
1226 }
1227
1228 cleanup:
1229 + free(path);
1230 cleanup_scoreboard(&sb);
1231 release_revisions(&revs);
1232 return 0;
t/t6130-pathspec-noglob.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='test globbing (and noglob) of pathspec limiting'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'create commits with glob characters' '
t/t7010-setup.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='setup taking and sanitizing funny paths'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success setup '
t/t8003-blame-corner-cases.sh
+1
@@ -4,6 +4,7 @@ test_description='git blame corner cases'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 +TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9
10 pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
t/t8008-blame-formats.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='blame output in various formats on a simple case'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 test_expect_success 'setup' '