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

Re: [PATCH] history: streamline message preparation and plug file stream leak

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 29, 2026, 06:33 UTC
Message-ID
<akIRrmD4Tqp-Gi9d@pks.im>
In-Reply-To
<xmqqecht8df1.fsf@gitster.g>
On Fri, Jun 26, 2026 at 09:38:42AM -0700, Junio C Hamano wrote:
Show 21 quoted lines
> diff --git a/builtin/history.c b/builtin/history.c
> index 8dcb9a6046..f17ec049c0 100644
> --- a/builtin/history.c
> +++ b/builtin/history.c
> @@ -41,11 +41,6 @@ static int fill_commit_message(struct repository *repo,
>  		  " empty message aborts the commit.\n");
>  	struct wt_status s;
>  
> -	strbuf_addstr(out, default_message);
> -	strbuf_addch(out, '\n');
> -	strbuf_commented_addf(out, comment_line_str, hint, action, comment_line_str);
> -	write_file_buf(path, out->buf, out->len);
> -
>  	wt_status_prepare(repo, &s);
>  	FREE_AND_NULL(s.branch);
>  	s.ahead_behind_flags = AHEAD_BEHIND_QUICK;
> @@ -57,14 +52,20 @@ static int fill_commit_message(struct repository *repo,
>  	s.whence = FROM_COMMIT;
>  	s.committable = 1;
>  
> -	s.fp = fopen(git_path_commit_editmsg(), "a");

Here we reuse the local `path` variable, which already carries the result of `git_path_commit_editmsg()`.

> +	s.fp = fopen(path, "w");
>  	if (!s.fp)
> -		return error_errno(_("could not open '%s'"), git_path_commit_editmsg());
> +		return error_errno(_("could not open '%s'"), path);
Likewise.
Show 10 quoted lines
> +	strbuf_addstr(out, default_message);
> +	strbuf_addch(out, '\n');
> +	strbuf_commented_addf(out, comment_line_str, hint, action, comment_line_str);
> +	fwrite(out->buf, 1, out->len, s.fp);
>  
>  	wt_status_collect_changes_trees(&s, old_tree, new_tree);
>  	wt_status_print(&s);
>  	wt_status_collect_free_buffers(&s);
>  	string_list_clear_func(&s.change, change_data_free);
> +	fclose(s.fp);
This is fixing the leaked file descriptor.

One thing I wonder though is that we don't perform any error checking on the file in the new version. Previously, we would have died in case `write_file_buf()` failed. But now we just `fwrite()` without error checking. I don't think that "wt-status.c" does error checking either, so we might end up with a partially-written file without us noticing.

Thanks!
Patrick
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 6 in “history: streamline message preparation and plug file stream leak”
  1. history: streamline message preparation and plug file stream leakJunio C Hamano, Jun 26, 2026
  2. Patrick SteinhardtJun 29, 2026
  3. Junio C HamanoJun 29, 2026
  4. Patrick SteinhardtJun 29, 2026
  5. history: streamline message preparation and plug file stream leakJunio C Hamano, Jun 29, 2026
  6. Patrick SteinhardtJun 30, 2026

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.