sequencer: fix memory leak if `todo_list_rearrange_squash()` failed

In sequencer.c:todo_list_rearrange_squash, if it fails, memory allocated in `next`, `tail`, `subjects` and `subject2item` will leak. Jump to cleanup label before return could fix this leak problem. Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lidong Yan committed May 14, 2025 at 13:53 UTC 044511f889b1989840339a322f84e50dfa3bf6e0
1 file changed +5 -3
sequencer.c
+5 -3
@@ -6596,6 +6596,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6596 char **subjects;
6597 struct commit_todo_item commit_todo;
6598 struct todo_item *items = NULL;
6599 + int ret = 0;
6600
6601 init_commit_todo_item(&commit_todo);
6602 /*
@@ -6626,8 +6627,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6627 }
6628
6629 if (is_fixup(item->command)) {
6629 - clear_commit_todo_item(&commit_todo);
6630 - return error(_("the script was already rearranged."));
6630 + ret = error(_("the script was already rearranged."));
6631 + goto cleanup;
6632 }
6633
6634 repo_parse_commit(the_repository, item->commit);
@@ -6729,6 +6730,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6730 todo_list->items = items;
6731 }
6732
6733 +cleanup:
6734 free(next);
6735 free(tail);
6736 for (i = 0; i < todo_list->nr; i++)
@@ -6738,7 +6740,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
6740
6741 clear_commit_todo_item(&commit_todo);
6742
6741 - return 0;
6743 + return ret;
6744 }
6745
6746 int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)