]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/blame: fix leaking blame entries with `--incremental`
authorPatrick Steinhardt <ps@pks.im>
Wed, 20 Nov 2024 13:39:30 +0000 (14:39 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Nov 2024 23:23:40 +0000 (08:23 +0900)
When passing `--incremental` to git-blame(1) we exit early by jumping to
the `cleanup` label. But some of the cleanups we perform are handled
between the `goto` and its label, and thus we leak the data.

Move the cleanups after the `cleanup` label. While at it, move the logic
to free the scoreboard's `final_buf` into `cleanup_scoreboard()` and
drop its `const` declaration.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
blame.c
blame.h
builtin/blame.c
t/t8005-blame-i18n.sh

diff --git a/blame.c b/blame.c
index 90633380cd583b689693e6cfe65c26a79448b00d..bf69768a7de6d2b989b85339a06af0a4763db4ad 100644 (file)
--- a/blame.c
+++ b/blame.c
@@ -2931,6 +2931,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)
 void cleanup_scoreboard(struct blame_scoreboard *sb)
 {
        free(sb->lineno);
+       free(sb->final_buf);
        clear_prio_queue(&sb->commits);
        oidset_clear(&sb->ignore_list);
 
diff --git a/blame.h b/blame.h
index 5b4e47d44c613e31fc4788c12ee9d8dc2a49d571..3b34be0e5c6932666bf5f738eb645da25adc0997 100644 (file)
--- a/blame.h
+++ b/blame.h
@@ -116,7 +116,7 @@ struct blame_scoreboard {
         * Used by many functions to obtain contents of the nth line,
         * indexed with scoreboard.lineno[blame_entry.lno].
         */
-       const char *final_buf;
+       char *final_buf;
        unsigned long final_buf_size;
 
        /* linked list of blames */
index e407a22da3bacf6bd26a6738e0ab0292ffadc216..6a7bb3b07248fbdfeb5ac3a05fed20fff4989a48 100644 (file)
@@ -1216,12 +1216,6 @@ parse_done:
                output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);
 
        output(&sb, output_option);
-       free((void *)sb.final_buf);
-       for (ent = sb.ent; ent; ) {
-               struct blame_entry *e = ent->next;
-               free(ent);
-               ent = e;
-       }
 
        if (show_stats) {
                printf("num read blob: %d\n", sb.num_read_blob);
@@ -1230,6 +1224,12 @@ parse_done:
        }
 
 cleanup:
+       for (ent = sb.ent; ent; ) {
+               struct blame_entry *e = ent->next;
+               free(ent);
+               ent = e;
+       }
+
        free(path);
        cleanup_scoreboard(&sb);
        release_revisions(&revs);
index 7a1f581c240c71106c709a830d3897d84fd8b32d..fa765aff99aa7fbfc4737cc91aab2a6a1bce30bb 100755 (executable)
@@ -1,6 +1,8 @@
 #!/bin/sh
 
 test_description='git blame encoding conversion'
+
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 
 if ! test_have_prereq ICONV