Skip to contentSkip to conversation

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

PATCH In review Lidong Yan via GitGitGadget 4 participants 4 messages May 14, 2025 – May 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sequencer: fix memory leak if `todo_list_rearrange_squash()` failed

From: Lidong Yan <[email protected]>

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.

---
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.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1965%2Fbrandb97%2Ffix-sequencer-todo-leak-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1965/brandb97/fix-sequencer-todo-leak-v1
Pull-Request: https://github.com/git/git/pull/1965

 sequencer.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
sequencer.c +5 -3
index b5c4043757e..5fb7b68a7ab 100644
@@ -6596,6 +6596,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 	char **subjects;
 	struct commit_todo_item commit_todo;
 	struct todo_item *items = NULL;
+	int ret = 0;
 
 	init_commit_todo_item(&commit_todo);
 	/*
@@ -6626,8 +6627,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 		}
 
 		if (is_fixup(item->command)) {
-			clear_commit_todo_item(&commit_todo);
-			return error(_("the script was already rearranged."));
+			ret = error(_("the script was already rearranged."));
+			goto cleanup;
 		}
 
 		repo_parse_commit(the_repository, item->commit);
@@ -6729,6 +6730,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 		todo_list->items = items;
 	}
 
+cleanup:
 	free(next);
 	free(tail);
 	for (i = 0; i < todo_list->nr; i++)
@@ -6738,7 +6740,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 
 	clear_commit_todo_item(&commit_todo);
 
-	return 0;
+	return ret;
 }
 
 int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)

base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75

Signature · 1 line
gitgitgadget
#1 lore raw
Quoted 5 lines · Lidong Yan via GitGitGadget

From: Lidong Yan <[email protected]>

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.

You could mention that you're adding the cleanup label.

I suspect reaching this condition is a bug as well because we should only rearrange the todo list when the rebase starts. However I'm not 100% sure that it is impossible to trigger this condition so lets free it as you suggest. The code changes look good.

Out of interest how are you finding these leaks?

Thanks

Phillip

Quoted 57 lines

---
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.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1965%2Fbrandb97%2Ffix-sequencer-todo-leak-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1965/brandb97/fix-sequencer-todo-leak-v1
Pull-Request: https://github.com/git/git/pull/1965

  sequencer.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
sequencer.c +5 -3
index b5c4043757e..5fb7b68a7ab 100644
@@ -6596,6 +6596,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
  	char **subjects;
  	struct commit_todo_item commit_todo;
  	struct todo_item *items = NULL;
+	int ret = 0;
 
  	init_commit_todo_item(&commit_todo);
  	/*
@@ -6626,8 +6627,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
  		}
 
  		if (is_fixup(item->command)) {
-			clear_commit_todo_item(&commit_todo);
-			return error(_("the script was already rearranged."));
+			ret = error(_("the script was already rearranged."));
+			goto cleanup;
  		}
 
  		repo_parse_commit(the_repository, item->commit);
@@ -6729,6 +6730,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
  		todo_list->items = items;
  	}
 
+cleanup:
  	free(next);
  	free(tail);
  	for (i = 0; i < todo_list->nr; i++)
@@ -6738,7 +6740,7 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
 
  	clear_commit_todo_item(&commit_todo);
 
-	return 0;
+	return ret;
  }
 
  int sequencer_determine_whence(struct repository *r, enum commit_whence *whence)

base-commit: 6f84262c44a89851c3ae5a6e4c1a9d06b2068d75

#2 lore raw
Quoted 8 lines · 2025/5/15 18:08,Phillip Wood <[email protected]> 写道:
Quoted 4 lines · Lidong Yan via GitGitGadget

From: Lidong Yan <[email protected]>
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.

You could mention that you're adding the cleanup label.

Understood. I will pay attention to this in future patches.

Quoted 1 line

Out of interest how are you finding these leaks?

I am a graduate student at Nanjing University, working in collaboration with Professor [Qingkai Shi](https://qingkaishi.github.io/). We are currently developing a tool for statically analyzing memory leaks in C programs, and I am testing this tool on Git. To ensure the validity of our testing results, we are submitting patches to Git and seeking confirmation from you developers.

#3 lore raw
In reply to Phillip Wood
Quoted 6 lines · Phillip Wood
Quoted 4 lines · Lidong Yan via GitGitGadget

From: Lidong Yan <[email protected]>
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.

Quoted 6 lines

You could mention that you're adding the cleanup label.

I suspect reaching this condition is a bug as well because we should only rearrange the todo list when the rebase starts. However I'm not 100% sure that it is impossible to trigger this condition so lets free it as you suggest. The code changes look good.

I wonder if there is a way (sort of using BUG("") but not as severe as killing the program) for us to mark "we do not expect this code path to be taken" and ask static analysis or fuzzers to disprove such assertions.

The message being an error(), without a silent return doing nothing, smells like a good enough sign, at least to me, that the original author intended not to call this helper function on a list that was already rearranged, so turning this into BUG("") may be something we would want to consider doing in the longer term.

Quoted 8 lines

sequencer.c int todo_list_rearrange_squash(struct todo_list *todo_list) · line 6631

Quoted 8 lines
sequencer.c +2 -2
@@ -6626,8 +6627,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
  		}
    		if (is_fixup(item->command)) {
-			clear_commit_todo_item(&commit_todo);
-			return error(_("the script was already rearranged."));
+			ret = error(_("the script was already rearranged."));
+			goto cleanup;
  		}

Thanks.