Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH] mailinfo: do not call handle_body() when headers end at EOF
Date: Sat, 21 Nov 2026 17:57:49 -0800	[thread overview]
Message-ID: <xmqqtsl9k4pu.fsf@gitster.g> (raw)

mailinfo() calls read_one_header_line() in a loop until it returns 0,
assuming a blank or non-header line was just read into 'line', and
then unconditionally passes 'line' to handle_body().

On a header-only input, however, the lookahead fgetc() for header
folding hits EOF on the last header.  In the next iteration,
strbuf_getline_lf() sees feof() and returns EOF early without
resetting 'line', and read_one_header_line() returns 0.  mailinfo()
then feeds that stale last header to handle_body(), leaking headers
like Content-Type into the commit log.

Make read_one_header_line() return -1 on EOF, and call handle_body()
only when it returns 0.  Checking feof() in mailinfo() would be
wrong, as an incomplete non-header line (with no preceding blank
line) also sets EOF while leaving valid data in 'line'.

An additional 'len &&' guard for the trailing LF check in
handle_filter_flowed() is not strictly necessary (the code will
never see an empty line there), but is there for symmetry with the
nearby check for CR, which does need it.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Something I noticed while playing with an unrelated area.

 mailinfo.c          | 18 ++++++++++++------
 t/t5100-mailinfo.sh | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/mailinfo.c b/mailinfo.c
index 13949ff31e..2879761017 100644
--- a/mailinfo.c
+++ b/mailinfo.c
@@ -895,13 +895,18 @@ static int is_rfc2822_header(const struct strbuf *line)
 	return 0;
 }
 
+/*
+ * Read one header line (including continuations) into `line`.
+ * Returns 1 if a header was read, 0 if a blank or non-header line was read
+ * into `line` (with '\n' restored), or -1 on EOF/error.
+ */
 static int read_one_header_line(struct strbuf *line, FILE *in)
 {
 	struct strbuf continuation = STRBUF_INIT;
 
 	/* Get the first part of the line. */
 	if (strbuf_getline_lf(line, in))
-		return 0;
+		return -1;
 
 	/*
 	 * Is it an empty line or not a valid rfc2822 header?
@@ -986,7 +991,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
 	strbuf_reset(&mi->charset);
 
 	/* slurp in this section's info */
-	while (read_one_header_line(line, mi->input))
+	while (read_one_header_line(line, mi->input) > 0)
 		check_header(mi, line, mi->p_hdr_data, 0);
 
 	strbuf_release(&newline);
@@ -1018,7 +1023,7 @@ static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line,
 		return;
 	}
 
-	if (line->buf[len - 1] == '\n') {
+	if (len && line->buf[len - 1] == '\n') {
 		len--;
 		if (len && line->buf[len - 1] == '\r')
 			len--;
@@ -1195,7 +1200,7 @@ static void handle_info(struct mailinfo *mi)
 int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
 {
 	FILE *cmitmsg;
-	int peek;
+	int peek, ret;
 	struct strbuf line = STRBUF_INIT;
 
 	cmitmsg = fopen(msg, "w");
@@ -1223,10 +1228,11 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
 	ungetc(peek, mi->input);
 
 	/* process the email header */
-	while (read_one_header_line(&line, mi->input))
+	while ((ret = read_one_header_line(&line, mi->input)) > 0)
 		check_header(mi, &line, mi->p_hdr_data, 1);
 
-	handle_body(mi, &line);
+	if (!ret)
+		handle_body(mi, &line);
 	fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
 	fclose(cmitmsg);
 	fclose(mi->patchfile);
diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh
index e01078abe7..ecaa39dd1d 100755
--- a/t/t5100-mailinfo.sh
+++ b/t/t5100-mailinfo.sh
@@ -300,4 +300,22 @@ test_expect_success 'from line with unterminated comment' '
 	test_cmp expect actual
 '
 
+test_expect_success 'mailinfo on header-only message does not leak header into body' '
+	cat >in <<-\EOF &&
+	From: A U Thor <author@example.com>
+	Subject: header-only message
+	Content-Type: text/plain; format=flowed
+	EOF
+	cat >expect <<-\EOF &&
+	Author: A U Thor
+	Email: author@example.com
+	Subject: header-only message
+
+	EOF
+	git mailinfo msg patch <in >actual &&
+	test_cmp expect actual &&
+	test_must_be_empty msg &&
+	test_must_be_empty patch
+'
+
 test_done
-- 
2.56.1-766-g80003c9184


                 reply	other threads:[~2026-11-22  1:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqtsl9k4pu.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox