line-log: fix leak when rewriting commit parents

In `process_ranges_merge_commit()` we try to figure out which of the parents can be blamed for the given line changes. When we figure out that none of the files in the line-log have changed we assign the complete blame to that commit and rewrite the parents of the current commit to only use that single parent. This is done via `commit_list_append()`, which is misleadingly _not_ appending to the list of parents. Instead, we overwrite the parents with the blamed parent. This makes us lose track of the old pointers, creating a memory leak. Fix this issue by freeing the parents before we overwrite them. 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 141766d1bb801ae2a9c7358920ce8dc9697f53c4
2 files changed +2
line-log.c
+1
@@ -1237,6 +1237,7 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *comm
1237 * don't follow any other path in history
1238 */
1239 add_line_range(rev, parents[i], cand[i]);
1240 + free_commit_list(commit->parents);
1241 commit_list_append(parents[i], &commit->parents);
1242
1243 ret = 0;
t/t4211-line-log.sh
+1
@@ -4,6 +4,7 @@ test_description='test log -L'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 +TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
9
10 test_expect_success 'setup (import history)' '