git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] sequencer: fix memory leak if `todo_list_rearrange_squash()` failed

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
May 15, 2025, 10:08 UTC
Message-ID
<52e0a717-f7f0-401d-b621-f1f6e028e595@gmail.com>
In-Reply-To
<pull.1965.git.git.1747230808770.gitgitgadget@gmail.com>
On 14/05/2025 14:53, Lidong Yan via GitGitGadget wrote:
Show 5 quoted lines
> From: Lidong Yan <502024330056@smail.nju.edu.cn>
> 
> 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
Show 57 quoted lines
> Signed-off-by: Lidong Yan <502024330056@smail.nju.edu.cn>
> ---
>      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(-)
> 
> diff --git a/sequencer.c b/sequencer.c
> index b5c4043757e..5fb7b68a7ab 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -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
Previous: Lidong Yan via GitGitGadgetNext: lidongyan
Message 2 of 4 in “sequencer: fix memory leak if `todo_list_rearrange_squash()` failed”
  1. sequencer: fix memory leak if `todo_list_rearrange_squash()` failedLidong Yan via GitGitGadget, May 14, 2025
  2. Phillip WoodMay 15, 2025
  3. lidongyanMay 15, 2025
  4. Junio C HamanoMay 15, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.