{"thread":{"id":"66911","subject":"[PATCH] mailinfo: do not call handle_body() when headers end at EOF","startedAt":"2026-11-22T01:57:49Z","lastAt":"2026-11-22T01:57:49Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559073","messageId":"xmqqtsl9k4pu.fsf@gitster.g","threadId":"66911","inReplyTo":null,"subject":"[PATCH] mailinfo: do not call handle_body() when headers end at EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T01:57:49Z","receivedAt":"2026-11-22T01:57:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"mailinfo() calls read_one_header_line() in a loop until it returns 0,\nassuming a blank or non-header line was just read into 'line', and\nthen unconditionally passes 'line' to handle_body().\n\nOn a header-only input, however, the lookahead fgetc() for header\nfolding hits EOF on the last header.  In the next iteration,\nstrbuf_getline_lf() sees feof() and returns EOF early without\nresetting 'line', and read_one_header_line() returns 0.  mailinfo()\nthen feeds that stale last header to handle_body(), leaking headers\nlike Content-Type into the commit log.\n\nMake read_one_header_line() return -1 on EOF, and call handle_body()\nonly when it returns 0.  Checking feof() in mailinfo() would be\nwrong, as an incomplete non-header line (with no preceding blank\nline) also sets EOF while leaving valid data in 'line'.\n\nAn additional 'len &&' guard for the trailing LF check in\nhandle_filter_flowed() is not strictly necessary (the code will\nnever see an empty line there), but is there for symmetry with the\nnearby check for CR, which does need it.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n * Something I noticed while playing with an unrelated area.\n\n mailinfo.c          | 18 ++++++++++++------\n t/t5100-mailinfo.sh | 18 ++++++++++++++++++\n 2 files changed, 30 insertions(+), 6 deletions(-)\n\ndiff --git a/mailinfo.c b/mailinfo.c\nindex 13949ff31e..2879761017 100644\n--- a/mailinfo.c\n+++ b/mailinfo.c\n@@ -895,13 +895,18 @@ static int is_rfc2822_header(const struct strbuf *line)\n \treturn 0;\n }\n \n+/*\n+ * Read one header line (including continuations) into `line`.\n+ * Returns 1 if a header was read, 0 if a blank or non-header line was read\n+ * into `line` (with '\\n' restored), or -1 on EOF/error.\n+ */\n static int read_one_header_line(struct strbuf *line, FILE *in)\n {\n \tstruct strbuf continuation = STRBUF_INIT;\n \n \t/* Get the first part of the line. */\n \tif (strbuf_getline_lf(line, in))\n-\t\treturn 0;\n+\t\treturn -1;\n \n \t/*\n \t * Is it an empty line or not a valid rfc2822 header?\n@@ -986,7 +991,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)\n \tstrbuf_reset(&mi->charset);\n \n \t/* slurp in this section's info */\n-\twhile (read_one_header_line(line, mi->input))\n+\twhile (read_one_header_line(line, mi->input) > 0)\n \t\tcheck_header(mi, line, mi->p_hdr_data, 0);\n \n \tstrbuf_release(&newline);\n@@ -1018,7 +1023,7 @@ static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line,\n \t\treturn;\n \t}\n \n-\tif (line->buf[len - 1] == '\\n') {\n+\tif (len && line->buf[len - 1] == '\\n') {\n \t\tlen--;\n \t\tif (len && line->buf[len - 1] == '\\r')\n \t\t\tlen--;\n@@ -1195,7 +1200,7 @@ static void handle_info(struct mailinfo *mi)\n int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)\n {\n \tFILE *cmitmsg;\n-\tint peek;\n+\tint peek, ret;\n \tstruct strbuf line = STRBUF_INIT;\n \n \tcmitmsg = fopen(msg, \"w\");\n@@ -1223,10 +1228,11 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)\n \tungetc(peek, mi->input);\n \n \t/* process the email header */\n-\twhile (read_one_header_line(&line, mi->input))\n+\twhile ((ret = read_one_header_line(&line, mi->input)) > 0)\n \t\tcheck_header(mi, &line, mi->p_hdr_data, 1);\n \n-\thandle_body(mi, &line);\n+\tif (!ret)\n+\t\thandle_body(mi, &line);\n \tfwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);\n \tfclose(cmitmsg);\n \tfclose(mi->patchfile);\ndiff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh\nindex e01078abe7..ecaa39dd1d 100755\n--- a/t/t5100-mailinfo.sh\n+++ b/t/t5100-mailinfo.sh\n@@ -300,4 +300,22 @@ test_expect_success 'from line with unterminated comment' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'mailinfo on header-only message does not leak header into body' '\n+\tcat >in <<-\\EOF &&\n+\tFrom: A U Thor <author@example.com>\n+\tSubject: header-only message\n+\tContent-Type: text/plain; format=flowed\n+\tEOF\n+\tcat >expect <<-\\EOF &&\n+\tAuthor: A U Thor\n+\tEmail: author@example.com\n+\tSubject: header-only message\n+\n+\tEOF\n+\tgit mailinfo msg patch <in >actual &&\n+\ttest_cmp expect actual &&\n+\ttest_must_be_empty msg &&\n+\ttest_must_be_empty patch\n+'\n+\n test_done\n-- \n2.56.1-766-g80003c9184\n\n\n"}]}