merge-recursive: fix leaking rename conflict info

When computing rename conflicts in our recursive merge algorithm we set up `struct rename_conflict_info`s to track that information. We never free those data structures though and thus leak memory. We need to be a bit more careful here though because the same rename conflict info can be assigned to multiple structures. Accommodate for this by introducing a `rename_conflict_info_owned` bit that we can use to steer whether or not the rename conflict info shall be free'd. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 11, 2024 at 11:19 UTC 3d31d382557527bf945a088931e3e28a717cc547
4 files changed +10 -1
merge-recursive.c
+7 -1
@@ -239,7 +239,8 @@ enum rename_type {
239 struct stage_data {
240 struct diff_filespec stages[4]; /* mostly for oid & mode; maybe path */
241 struct rename_conflict_info *rename_conflict_info;
242 - unsigned processed:1;
242 + unsigned processed:1,
243 + rename_conflict_info_owned:1;
244 };
245
246 struct rename {
@@ -308,6 +309,7 @@ static inline void setup_rename_conflict_info(enum rename_type rename_type,
309
310 ci->ren1->dst_entry->processed = 0;
311 ci->ren1->dst_entry->rename_conflict_info = ci;
312 + ci->ren1->dst_entry->rename_conflict_info_owned = 1;
313 if (ren2) {
314 ci->ren2->dst_entry->rename_conflict_info = ci;
315 }
@@ -3055,6 +3057,10 @@ static void final_cleanup_rename(struct string_list *rename)
3057 for (i = 0; i < rename->nr; i++) {
3058 re = rename->items[i].util;
3059 diff_free_filepair(re->pair);
3060 + if (re->src_entry->rename_conflict_info_owned)
3061 + FREE_AND_NULL(re->src_entry->rename_conflict_info);
3062 + if (re->dst_entry->rename_conflict_info_owned)
3063 + FREE_AND_NULL(re->dst_entry->rename_conflict_info);
3064 }
3065 string_list_clear(rename, 1);
3066 free(rename);
t/t3401-rebase-and-am-rename.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git rebase + directory rename tests'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-rebase.sh
8
t/t4153-am-resume-override-opts.sh
+1
@@ -2,6 +2,7 @@
2
3 test_description='git-am command-line options override saved options'
4
5 +TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
8
t/t7201-co.sh
+1
@@ -23,6 +23,7 @@ Test switching across them.
23 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
24 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
25
26 +TEST_PASSES_SANITIZE_LEAK=true
27 . ./test-lib.sh
28
29 test_tick