From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from fhigh-b1-smtp.messagingengine.com (fhigh-b1-smtp.messagingengine.com [202.12.124.152]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3D87310652 for ; Fri, 26 Jun 2026 16:38:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=202.12.124.152 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782491926; cv=none; b=i+VW+/kqL3vvu6Ih5yeqacktDw65hH5bu2iUZdTqbKXpFyDSrb2sWNqMxpg2vuVoc0s++iHOT+p1KG82T2dYXFPTMKZrEIzYk9ixNXxZalPzPEZzM6lR5iGsAnnmWxaMzSVtoKKLBYi/47OCWkR8RadzdBIPX60TPlI06e9hRNM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782491926; c=relaxed/simple; bh=9Z557X5p+QK0ZfEn+DyvWgLVHeNjnRr1MMR/Daho6YY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=cFxkZ5eXixIjQsglYKswwLu2XltH6psucqs7v0+wwQc+jMYZziK901xlv/MBX7XIMrzQrOfXpcZuNyJOmITQ/1xeMga8kKZ7X2HXlDcREOpqBCDyj7bbW7z4HWuahSxCT2SaR23e0OZPQ7O8sOEYZQL6wbTco6cyg/ngAAJt1i8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com; spf=pass smtp.mailfrom=pobox.com; dkim=pass (2048-bit key) header.d=pobox.com header.i=@pobox.com header.b=iDSBG7m4; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Ty446KQV; arc=none smtp.client-ip=202.12.124.152 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=pobox.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pobox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pobox.com header.i=@pobox.com header.b="iDSBG7m4"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Ty446KQV" Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfhigh.stl.internal (Postfix) with ESMTP id 304647A0140; Fri, 26 Jun 2026 12:38:44 -0400 (EDT) Received: from phl-frontend-02 ([10.202.2.161]) by phl-compute-01.internal (MEProxy); Fri, 26 Jun 2026 12:38:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pobox.com; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm3; t=1782491924; x=1782578324; bh=LInLG9YD13dlwZWtZkbrF6zMgVURzp5Y mdaEg6evHWw=; b=iDSBG7m46NvWK8kP1GJ376e0IrUn1GpxUf1EpLiu7swhMQ28 R6jWL79ploLtPECs7dFzatl1Sz0pX90xprsR0icjnKaiBCUlVOJ4uyQew0SSxiMF wBMoX8LSMN5NibyhHQzqFu+5IvcqLLVcs/YnPGsBvqdSwx/dsJfcIJWRqvKYrllF L+Rk9Ya2YPL1boAvbLQoOIy6wj1lHAEzUsrOFxCj58g/zpsOsZR7JoyAvWWsE1kq oqbQNnESYp5pdR0amwX/vD6QSY0yrH6F1NlTUsl9SexUB2AGv5DwTqrv6Kww0HYM hSj1qS5X46L5yJMMPLKlJcK+61BsMfFUhvbKOA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1782491924; x= 1782578324; bh=LInLG9YD13dlwZWtZkbrF6zMgVURzp5YmdaEg6evHWw=; b=T y446KQVazejv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feedback-ID: if26b431b:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 26 Jun 2026 12:38:43 -0400 (EDT) From: Junio C Hamano To: git@vger.kernel.org Cc: Patrick Steinhardt , Johannes Schindelin Subject: [PATCH] history: streamline message preparation and plug file stream leak Date: Fri, 26 Jun 2026 09:38:42 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain An early part of fill_commit_mmessage() function uses write_file_buf() to write out what was prepared in a strbuf, which is primarily meant for use by callers that have their own message prepared fully and called as the last thing to flush it to the destination file. However, the function then opens a file stream in append mode to further write into it. It may have been understandable if this was a later addition, but it seems it came from a single commit, d205234c (builtin/history: implement "reword" subcommand, 2026-01-13), which is somewhat puzzling, but anyway... Just open the file stream upfront for writing, write the message the function has in the strbuf, and then keep writing whatever it wants to write to the same open file stream. And do not forget to close the stream. We are about to pass the resulting file to an external editor, and on some systems, notably Windows, you are not supposed to keep a file open while expecting another program to access it. Diagnosed-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- * As the initial one was written and sent as "how about doing a bit more thorough job while we are at it?" response to a posted patch found in , this is a tested and merge-ready cersion that I consider "v1". builtin/history.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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"); + 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); + + 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); strbuf_reset(out); if (launch_editor(path, out, NULL)) { -- 2.55.0-rc2-177-gc9430f6415