Skip to content

Commit

Permalink
builtin/blame: fix leaking blame entries with --incremental
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pks-t authored and gitster committed Nov 20, 2024
1 parent 2664f2a commit 65a1b7e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
1 change: 1 addition & 0 deletions blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion blame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 6 additions & 6 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,12 +1216,6 @@ int cmd_blame(int argc,
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);
Expand All @@ -1230,6 +1224,12 @@ int cmd_blame(int argc,
}

cleanup:
for (ent = sb.ent; ent; ) {
struct blame_entry *e = ent->next;
free(ent);
ent = e;
}

free(path);
cleanup_scoreboard(&sb);
release_revisions(&revs);
Expand Down
2 changes: 2 additions & 0 deletions t/t8005-blame-i18n.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 65a1b7e

Please sign in to comment.