builtin/blame: fix leaking blame entries with `--incremental`

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>

Patrick Steinhardt committed Nov 20, 2024 at 14:39 UTC 65a1b7e2bdbdff523f8d55e0036bfad72467bfc8
4 files changed +10 -7
blame.c
+1
@@ -2931,6 +2931,7 @@ void setup_blame_bloom_data(struct blame_scoreboard *sb)
2931 void cleanup_scoreboard(struct blame_scoreboard *sb)
2932 {
2933 free(sb->lineno);
2934 + free(sb->final_buf);
2935 clear_prio_queue(&sb->commits);
2936 oidset_clear(&sb->ignore_list);
2937
blame.h
+1 -1
@@ -116,7 +116,7 @@ struct blame_scoreboard {
116 * Used by many functions to obtain contents of the nth line,
117 * indexed with scoreboard.lineno[blame_entry.lno].
118 */
119 - const char *final_buf;
119 + char *final_buf;
120 unsigned long final_buf_size;
121
122 /* linked list of blames */
builtin/blame.c
+6 -6
@@ -1216,12 +1216,6 @@ parse_done:
1216 output_option &= ~(OUTPUT_COLOR_LINE | OUTPUT_SHOW_AGE_WITH_COLOR);
1217
1218 output(&sb, output_option);
1219 - free((void *)sb.final_buf);
1220 - for (ent = sb.ent; ent; ) {
1221 - struct blame_entry *e = ent->next;
1222 - free(ent);
1223 - ent = e;
1224 - }
1219
1220 if (show_stats) {
1221 printf("num read blob: %d\n", sb.num_read_blob);
@@ -1230,6 +1224,12 @@ parse_done:
1224 }
1225
1226 cleanup:
1227 + for (ent = sb.ent; ent; ) {
1228 + struct blame_entry *e = ent->next;
1229 + free(ent);
1230 + ent = e;
1231 + }
1232 +
1233 free(path);
1234 cleanup_scoreboard(&sb);
1235 release_revisions(&revs);
t/t8005-blame-i18n.sh
+2
@@ -1,6 +1,8 @@
1 #!/bin/sh
2
3 test_description='git blame encoding conversion'
4 +
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7
8 if ! test_have_prereq ICONV