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>
maint
Patrick Steinhardt 2024-06-11 11:21:25 +02:00 committed by Junio C Hamano
parent ee6a998583
commit 3332f35577
5 changed files with 9 additions and 2 deletions

View File

@ -687,7 +687,7 @@ static unsigned parse_score(const char *arg)
return score; return score;
} }


static const char *add_prefix(const char *prefix, const char *path) static char *add_prefix(const char *prefix, const char *path)
{ {
return prefix_path(prefix, prefix ? strlen(prefix) : 0, path); return prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
} }
@ -865,7 +865,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,
int cmd_blame(int argc, const char **argv, const char *prefix) int cmd_blame(int argc, const char **argv, const char *prefix)
{ {
struct rev_info revs; struct rev_info revs;
const char *path; char *path = NULL;
struct blame_scoreboard sb; struct blame_scoreboard sb;
struct blame_origin *o; struct blame_origin *o;
struct blame_entry *ent = NULL; struct blame_entry *ent = NULL;
@ -1226,6 +1226,7 @@ parse_done:
} }


cleanup: cleanup:
free(path);
cleanup_scoreboard(&sb); cleanup_scoreboard(&sb);
release_revisions(&revs); release_revisions(&revs);
return 0; return 0;

View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh


test_description='test globbing (and noglob) of pathspec limiting' test_description='test globbing (and noglob) of pathspec limiting'

TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh


test_expect_success 'create commits with glob characters' ' test_expect_success 'create commits with glob characters' '

View File

@ -2,6 +2,7 @@


test_description='setup taking and sanitizing funny paths' test_description='setup taking and sanitizing funny paths'


TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh


test_expect_success setup ' test_expect_success setup '

View File

@ -4,6 +4,7 @@ test_description='git blame corner cases'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME


TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh


pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/' pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'

View File

@ -1,6 +1,8 @@
#!/bin/sh #!/bin/sh


test_description='blame output in various formats on a simple case' test_description='blame output in various formats on a simple case'

TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh


test_expect_success 'setup' ' test_expect_success 'setup' '