]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/blame: fix leaking prefixed paths
authorPatrick Steinhardt <ps@pks.im>
Tue, 11 Jun 2024 09:21:25 +0000 (11:21 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 11 Jun 2024 20:15:08 +0000 (13:15 -0700)
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>
builtin/blame.c
t/t6130-pathspec-noglob.sh
t/t7010-setup.sh
t/t8003-blame-corner-cases.sh
t/t8008-blame-formats.sh

index e09ff0155a54e4ed8dbad76e73dfa7a367ae1c73..17694410ed900e035ef89003b48f0d9a80886196 100644 (file)
@@ -687,7 +687,7 @@ static unsigned parse_score(const char *arg)
        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);
 }
@@ -865,7 +865,7 @@ static void build_ignorelist(struct blame_scoreboard *sb,
 int cmd_blame(int argc, const char **argv, const char *prefix)
 {
        struct rev_info revs;
-       const char *path;
+       char *path = NULL;
        struct blame_scoreboard sb;
        struct blame_origin *o;
        struct blame_entry *ent = NULL;
@@ -1226,6 +1226,7 @@ parse_done:
        }
 
 cleanup:
+       free(path);
        cleanup_scoreboard(&sb);
        release_revisions(&revs);
        return 0;
index ba7902c9cdcd0e6664f81b478b24596a4deb06dc..82de25d549a07e0ada1ee858b6ccb6bf43107ce1 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='test globbing (and noglob) of pathspec limiting'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'create commits with glob characters' '
index 520f96d09fb71778704119929012a40aa7c0072a..d9add2162e9c64d8fd5341cf83f123c7c57b7324 100755 (executable)
@@ -2,6 +2,7 @@
 
 test_description='setup taking and sanitizing funny paths'
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success setup '
index 731265541acf9fb0ed4dcc216b662daa897f814d..6288352f5777b3eed84b891c79ded307502f8ed6 100755 (executable)
@@ -4,6 +4,7 @@ test_description='git blame corner cases'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
index ae4b579d245caf568d07d1e452f917581dbfd355..fb5d225a671447cee24906dc51a5a1587e052564 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='blame output in various formats on a simple case'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 test_expect_success 'setup' '