{"thread":{"id":"66914","subject":"[PATCH 0/6] getwholeline should clear the buffer upon EOF","startedAt":"2026-11-22T14:03:41Z","lastAt":"2026-11-23T15:37:18Z","messageCount":8,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":6},"messages":[{"id":"559095","messageId":"20261122140347.41990-1-gitster@pobox.com","threadId":"66914","inReplyTo":null,"subject":"[PATCH 0/6] getwholeline should clear the buffer upon EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:41Z","receivedAt":"2026-11-22T14:03:41Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This series, which is structured in a somewhat unusual way, addresses\nvarious issues that are caused by a long-standing API bug that the\nstrbuf_getwholeline() family of functions shared for quite some time.\n\nWhen they see feof() on their input stream, they immediately return\nEOF without reading anything (which is good), but they do not clear\nthe output buffer.  It is not strictly *wrong* per se, but it proved\nto be a rather error-prone API contract, as we see in the early part\nof this series.\n\nPatches 2 (shortlog), 3 (clean -i), and 4 (fast-import) explain the\nexisting bugs in various commands and add test(s) to trigger the bugs,\nbut the fix is left for patch 5, and the fix happens in code far away\nfrom where these bugs exist.  The usual \"describe the fix and\ndemonstrate it with new test_expect_success\" pattern is deliberately not\nfollowed.  This is because doing the usual thing would force reviewers\nto understand the details of three unrelated commands illustrated in\npatches 2, 3, and 4, as well as the solution in patch 5 at the same\ntime.  Because the code that caused the existing bugs does not appear in\nany of these steps, the log messages of patches 2-4 instead give\ndetailed walkthroughs and are rather long, which is another reason why\nthe nature of this series discourages following the usual pattern and\ndoing everything in one step.\n\nThen there is a bonus patch 6, which illustrates two additional\nissues in \"clean\" and \"fast-import\" that cannot have\ntest_expect_failure tests that are safe to include before the problem\nis fixed, because these breakages are infinite loops.\n\nPatch 1, by the way, is what I posted elsewhere already.  This is a\ncaller that implicitly relied on strbuf_getwholeline() not clearing\nthe buffer upon feof(), and it is fixed in such a way that it does\nnot matter if strbuf_getwholeline() clears or leaves the buffer upon\nfeof() so \"mailinfo\" keeps working before or after patch 5 of this\nseries.\n\n 1/6: mailinfo: do not call handle_body() when headers end at EOF\n 2/6: t4201: demonstrate shortlog reusing stale buffer on EOF\n 3/6: t7301: demonstrate \"clean -i\" ask mode deleting past EOF\n 4/6: t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF\n 5/6: strbuf_getwholeline(): clear strbuf upon EOF\n 6/6: t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF\n\n mailinfo.c                   | 18 +++++++----\n strbuf.c                     |  6 ++--\n t/t4201-shortlog.sh          | 29 +++++++++++++++++\n t/t5100-mailinfo.sh          | 18 +++++++++++\n t/t7301-clean-interactive.sh | 31 +++++++++++++++++++\n t/t9300-fast-import.sh       | 60 ++++++++++++++++++++++++++++++++++++\n 6 files changed, 154 insertions(+), 8 deletions(-)\n\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559096","messageId":"20261122140347.41990-2-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 1/6] mailinfo: do not call handle_body() when headers end at EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:42Z","receivedAt":"2026-11-22T14:03:42Z","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.\nWhen the function returns 0, it assumes that a blank or non-header line\nwas just read into 'line', and then unconditionally passes it to\nhandle_body().\n\nIf the input had only header lines, however, a call to fgetc() in\nread_one_header_line(), made to look ahead for header-folding, would\nhave hit EOF already.  strbuf_getline_lf(), which is called by\nread_one_header_line(), sees feof(), and returns EOF early without\nresetting 'line', and then read_one_header_line() returns 0.\n\nmailinfo() now feeds that stale last header to handle_body(), leaking\nheaders like Content-Type into the commit log.\n\nMake read_one_header_line() return -1 on EOF, so that we can tell it and\nan empty-line case (i.e., a proper end of header lines) apart, and never\ncall handle_body() if we see an EOF.\n\nBy the way, checking feof() in mailinfo() would not work, as an\nincomplete non-header line (with no preceding blank line) also sets EOF\nwhile 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 never\nsee an empty line there), but is there for symmetry with the nearby\ncheck for CR, which does need it.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\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"},{"id":"559097","messageId":"20261122140347.41990-3-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:43Z","receivedAt":"2026-11-22T14:03:43Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"When builtin/shortlog.c:read_from_stdin() reads from the standard input,\nit skips the header lines until it finds the header (either \"author\" or\n\"committer\") we are interested in, parses what is on the line, skips all\nthe remaining header lines, and then skips the blank lines that follow,\nto find the commit subject line.\n\nBut this assumes that strbuf_getline_lf() clears the buffer when\nreturning EOF.  If the input ends on an incomplete line, that is not\ntrue.  Stale contents in 'oneline' cause the command to produce wrong\noutput.\n\nHere are step-by-step walkthroughs for the two failure modes in this\ncode path.\n\n * If an incomplete \"Author:\" line at EOF follows an earlier commit, the\n   earlier commit leaves its subject in 'oneline'.  The outer loop reads\n   the next \"Author:\" line into 'ident', sets feof(stdin) while scanning\n   for LF, and returns 0.  We successfully read the author of this\n   second commit, and then the two inner loops to skip other headers and\n   blank lines call strbuf_getline_lf() on 'oneline', which sees\n   feof(stdin) and returns EOF without resetting 'oneline'.  The strbuf\n   still has the subject of the previous commit, and that is what is\n   given to insert_one_record().\n\n * If an incomplete header line (such as \"Date:\") appears at EOF, after\n   matching \"Author:\", the first inner loop reads the \"Date:\" line into\n   'oneline', sets feof(stdin), and returns 0.  Because '.len' is\n   non-zero, the loop runs again; both that iteration and the second\n   inner loop see feof(stdin) and return EOF without resetting\n   'oneline'.  insert_one_record() therefore records the discarded\n   \"Date:\" header as the commit's subject.\n\nAdd two test_expect_failure tests to t4201-shortlog.sh illustrating\nthese issues.\n\nOnce strbuf_getwholeline() is changed to reset the buffer before\nreturning on feof(), the first EOF read into 'oneline' empties it in\nboth cases, so insert_one_record() records an empty subject, matching\nthe behavior when the final line is LF-terminated.  Because there are\nother code paths that will be \"fixed\" by such a change to\nstrbuf_getwholeline(), we will not fix it in this commit (yet), and stop\nat documenting the issue in the form of test_expect_failure.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t4201-shortlog.sh | 29 +++++++++++++++++++++++++++++\n 1 file changed, 29 insertions(+)\n\ndiff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh\nindex 10c43e6e75..dc1b175653 100755\n--- a/t/t4201-shortlog.sh\n+++ b/t/t4201-shortlog.sh\n@@ -447,4 +447,33 @@ test_expect_success 'unknown revision options are reported correctly' '\n \ttest_grep \"unknown option .*--no-such-option\" err\n '\n \n+test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '\n+\ttest_write_lines \\\n+\t\t\"Author: A U Thor <author@example.com>\" \\\n+\t\t\"\" \\\n+\t\t\"    First commit\" >in &&\n+\tprintf \"%s\" \"Author: B U Thor <b@example.com>\" >>in &&\n+\ttest_write_lines \\\n+\t\t\"A U Thor (1):\" \\\n+\t\t\"      First commit\" \\\n+\t\t\"\" \\\n+\t\t\"B U Thor (1):\" \\\n+\t\t\"      \" \\\n+\t\t\"\" >expect &&\n+\tgit shortlog <in >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '\n+\tprintf \"%s\\n%s\" \\\n+\t\t\"Author: A U Thor <author@example.com>\" \\\n+\t\t\"Date:   Mon Jan 1 00:00:00 2024 +0000\" >in &&\n+\ttest_write_lines \\\n+\t\t\"A U Thor (1):\" \\\n+\t\t\"      \" \\\n+\t\t\"\" >expect &&\n+\tgit shortlog <in >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559098","messageId":"20261122140347.41990-4-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 3/6] t7301: demonstrate \"clean -i\" ask mode deleting past EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:44Z","receivedAt":"2026-11-22T14:03:44Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"After builtin/clean.c:ask_each_cmd() asks a \"remove this?\" question, it\nreads the interactive answer from the user to decide what to do.  This\nis done in a loop to remove potentially many items.\n\nBut this assumes that strbuf_getline() clears the buffer when returning\nEOF.  A \"y\" answer on an incomplete line causes the command to delete\nevery remaining untracked item instead of stopping on EOF.\n\nHere is a step-by-step walkthrough of this code path.\n\nIn ask_each_cmd(), a loop iterates over each candidate in 'del_list'.\nWhile '!eof', it prompts and calls git_read_line_interactively() (which\ncalls strbuf_getline()) to read into 'confirm'.  On EOF, it prints a\nnewline, sets 'eof = 1', and then falls through to check 'if\n(!confirm.len || strncasecmp(confirm.buf, \"yes\", confirm.len))' to drop\nitems that should not be removed, assuming EOF left 'confirm' empty.\n\nImagine that the user types 'a' to choose 'ask each' mode, and gives 'y'\nwith no trailing LF:\n\n * For the first item, strbuf_getline() reads \"y\" into 'confirm', sets\n   feof(stdin) while scanning for LF, and returns 0.  Because \"y\"\n   matches a prefix of \"yes\", that first item stays in 'del_list' to be\n   removed.\n\n * For the second item, '!eof' is still true, so strbuf_getline() is\n   called on 'confirm'.  strbuf_getwholeline() sees feof(stdin) and\n   returns EOF without resetting 'confirm'.  ask_each_cmd() sets 'eof =\n   1' and falls through with 'confirm' still containing \"y\", keeping the\n   second item in 'del_list' for removal.\n\n * For every remaining item in 'del_list', 'if (!eof)' is skipped while\n   'confirm' remains \"y\", so every remaining untracked file is removed\n   contrary to the \"Ctrl-D should stop removing files\" intent.\n\nAdd a test_expect_failure test to t7301-clean-interactive.sh\nillustrating this issue.\n\nOnce strbuf_getwholeline() is changed to reset the buffer before\nreturning on feof(), the EOF read on the second item empties 'confirm',\nmaking '!confirm.len' true for the second and all subsequent items so\nonly a.out is removed.  Because there are other code paths that will be\n\"fixed\" by such a change to strbuf_getwholeline(), we will not fix it in\nthis commit (yet), and stop at documenting the issue in the form of\ntest_expect_failure.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t7301-clean-interactive.sh | 20 ++++++++++++++++++++\n 1 file changed, 20 insertions(+)\n\ndiff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh\nindex de59ba3724..73e923d212 100755\n--- a/t/t7301-clean-interactive.sh\n+++ b/t/t7301-clean-interactive.sh\n@@ -381,6 +381,26 @@ test_expect_success 'git clean -id (ask)' '\n \n '\n \n+test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '\n+\tmkdir -p build docs &&\n+\ttouch a.out src/part3.c src/part3.h src/part4.c src/part4.h \\\n+\t\tdocs/manual.txt obj.o build/lib.so &&\n+\tprintf \"a\\ny\" |\n+\tgit clean -id &&\n+\ttest_path_is_file Makefile &&\n+\ttest_path_is_file README &&\n+\ttest_path_is_file src/part1.c &&\n+\ttest_path_is_file src/part2.c &&\n+\ttest_path_is_missing a.out &&\n+\ttest_path_is_file docs/manual.txt &&\n+\ttest_path_is_file src/part3.c &&\n+\ttest_path_is_file src/part3.h &&\n+\ttest_path_is_file src/part4.c &&\n+\ttest_path_is_file src/part4.h &&\n+\ttest_path_is_file obj.o &&\n+\ttest_path_is_file build/lib.so\n+'\n+\n test_expect_success 'git clean -id (ask - Ctrl+D)' '\n \n \tmkdir -p build docs &&\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559099","messageId":"20261122140347.41990-5-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:45Z","receivedAt":"2026-11-22T14:03:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"After reading a commit message via builtin/fast-import.c:parse_data(),\nparse_new_commit() reads the next line into 'command_buf' via\nread_next_command() without checking its return value, checks for\noptional \"from\" and \"merge\" lines, and then loops over file-change\ncommands while 'command_buf' is non-empty.\n\nBut this assumes that strbuf_getline_lf() clears the buffer when\nreturning EOF.  If the input ends right after a heredoc commit message\nwith no trailing blank line, that is not true.  The stale heredoc\ndelimiter left in 'command_buf' can then be re-parsed as a file-change\ncommand, corrupting the commit's tree.\n\nHere is a step-by-step walkthrough of this code path.\n\nIn parse_new_commit(), after reading the commit message with\nparse_data(), the function calls read_next_command() without checking\nits return value, runs parse_from() and parse_merge(), and then\ndispatches file-change commands in a 'while (command_buf.len > 0)' loop.\nIt assumes EOF clears 'command_buf' so that the loop is skipped.\n\nWhen a commit message uses a heredoc delimiter matching a file-change\ncommand (e.g., \"data <<deleteall\") and the stream ends right after the\nterminator line (\"deleteall\\n\", with no trailing blank line):\n\n * parse_data() reads into 'command_buf' via strbuf_getline_lf() until\n   'command_buf' matches \"deleteall\" and breaks out of its loop, leaving\n   \"deleteall\" in 'command_buf'.\n\n * At the end of parse_data(), skip_optional_lf() calls fgetc(stdin),\n   which hits EOF and sets feof(stdin).  (Note that the input does not\n   even need an incomplete line.)\n\n * Back in parse_new_commit(), read_next_command() calls\n   strbuf_getline_lf() on 'command_buf'.  strbuf_getwholeline() sees\n   feof(stdin) and returns EOF without resetting 'command_buf'.\n   read_next_command() records 'stdin_eof' and returns EOF, which\n   parse_new_commit() ignores.\n\n * parse_from() and parse_merge() do not match \"deleteall\", so execution\n   enters 'while (command_buf.len > 0)'.  Because 'command_buf' still\n   holds \"deleteall\", parse_new_commit() calls file_change_deleteall(),\n   discarding every file inherited from the parent commit before the\n   loop's trailing read_next_command() check sees EOF and breaks.\n\nAdd a test_expect_failure test to t9300-fast-import.sh illustrating this\nissue.\n\nOnce strbuf_getwholeline() is changed to reset the buffer before\nreturning on feof(), read_next_command() after parse_data() empties\n'command_buf'.  The 'while (command_buf.len > 0)' loop is then skipped,\nand the parent's tree is preserved.  Because there are other code paths\nthat will be \"fixed\" by such a change to strbuf_getwholeline(), we will\nnot fix it in this commit (yet), and stop at documenting the issue in\nthe form of test_expect_failure.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t9300-fast-import.sh | 27 +++++++++++++++++++++++++++\n 1 file changed, 27 insertions(+)\n\ndiff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh\nindex 15f511fb6b..22d697ff9a 100755\n--- a/t/t9300-fast-import.sh\n+++ b/t/t9300-fast-import.sh\n@@ -4087,4 +4087,31 @@ test_expect_success 'Y: rewrite submodules via stdin' '\n \t)\n '\n \n+test_expect_failure 'fast-import: heredoc terminator at EOF is not re-parsed as command' '\n+\ttest_when_finished \"rm -rf eof-deleteall\" &&\n+\tgit init eof-deleteall &&\n+\t(\n+\t\tcd eof-deleteall &&\n+\t\tgit fast-import --quiet <<-\\EOF &&\n+\t\tcommit refs/heads/main\n+\t\tmark :1\n+\t\tcommitter A U Thor <author@example.com> 1000000000 +0000\n+\t\tdata 5\n+\t\tinit\n+\t\tM 100644 inline file\n+\t\tdata 6\n+\t\thello\n+\n+\t\tcommit refs/heads/main\n+\t\tcommitter A U Thor <author@example.com> 1000000001 +0000\n+\t\tdata <<deleteall\n+\t\tsecond commit\n+\t\tdeleteall\n+\t\tEOF\n+\t\techo file >expect &&\n+\t\tgit ls-tree -r --name-only main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_done\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559100","messageId":"20261122140347.41990-6-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:46Z","receivedAt":"2026-11-22T14:03:46Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"strbuf_getwholeline(), and its frontends like strbuf_getline_lf(), has\nan early return upon seeing that the stream has reached EOF; curiously,\nit does not touch the strbuf when it takes this early return code path.\n\nAnd indeed, a few callers of these functions make a false assumption\nthat their buffer will be cleared when the function returns EOF, and due\nto that assumption, they are buggy, as we saw in the previous commits.\n\nTeach strbuf_getwholeline() to clear the buffer when taking the fast EOF\nreturn path.  By doing so, we fix all of these code paths, so we can\nturn the test_expect_failure tests we added into test_expect_success.\n\nThis would break existing callers that expect that strbuf_getwholeline()\nor its frontends do not clear the buffer upon EOF, but such callers are\nunlikely.  If you call strbuf_getline(&buf), the normal thing to expect\nis to lose what you originally had in buf.  Expecting that the original\ncontents can be read back again when the call returns EOF is not really\nsensible.  There is indeed nobody that relies on the fact that the\noriginal contents are still available to read.\n\nIf strbuf_getwholeline() is \"fixed\" to clear the buffer upon EOF, \"git\nmailinfo\" would call handle_body() with an empty 'line' upon feof(), but\nif this were a header-only message that was sent as \"format=flowed\"\ntype, handle_filter_flowed() would get called and access .buf[len - 1]\nfor this empty line whose len == 0, reading out of bounds.  This issue\nhas been addressed in an earlier patch to teach the code to never call\nhandle_body() after EOF with an empty line.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n strbuf.c                     | 6 ++++--\n t/t4201-shortlog.sh          | 4 ++--\n t/t7301-clean-interactive.sh | 2 +-\n t/t9300-fast-import.sh       | 2 +-\n 4 files changed, 8 insertions(+), 6 deletions(-)\n\ndiff --git a/strbuf.c b/strbuf.c\nindex c3baa47b3f..62abf01bfa 100644\n--- a/strbuf.c\n+++ b/strbuf.c\n@@ -643,8 +643,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)\n {\n \tssize_t r;\n \n-\tif (feof(fp))\n+\tif (feof(fp)) {\n+\t\tstrbuf_reset(sb);\n \t\treturn EOF;\n+\t}\n \n \t/* Translate slopbuf to NULL, as we cannot call realloc on it */\n \tif (!sb->alloc)\n@@ -688,10 +690,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)\n {\n \tint ch;\n \n+\tstrbuf_reset(sb);\n \tif (feof(fp))\n \t\treturn EOF;\n \n-\tstrbuf_reset(sb);\n \tflockfile(fp);\n \twhile ((ch = getc_unlocked(fp)) != EOF) {\n \t\tif (!strbuf_avail(sb))\ndiff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh\nindex dc1b175653..fc731da0d3 100755\n--- a/t/t4201-shortlog.sh\n+++ b/t/t4201-shortlog.sh\n@@ -447,7 +447,7 @@ test_expect_success 'unknown revision options are reported correctly' '\n \ttest_grep \"unknown option .*--no-such-option\" err\n '\n \n-test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '\n+test_expect_success 'shortlog from stdin with incomplete Author line at EOF' '\n \ttest_write_lines \\\n \t\t\"Author: A U Thor <author@example.com>\" \\\n \t\t\"\" \\\n@@ -464,7 +464,7 @@ test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '\n \ttest_cmp expect actual\n '\n \n-test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '\n+test_expect_success 'shortlog from stdin with incomplete header line at EOF' '\n \tprintf \"%s\\n%s\" \\\n \t\t\"Author: A U Thor <author@example.com>\" \\\n \t\t\"Date:   Mon Jan 1 00:00:00 2024 +0000\" >in &&\ndiff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh\nindex 73e923d212..c7a32fd7f0 100755\n--- a/t/t7301-clean-interactive.sh\n+++ b/t/t7301-clean-interactive.sh\n@@ -381,7 +381,7 @@ test_expect_success 'git clean -id (ask)' '\n \n '\n \n-test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '\n+test_expect_success 'git clean -id (ask - EOF without trailing LF)' '\n \tmkdir -p build docs &&\n \ttouch a.out src/part3.c src/part3.h src/part4.c src/part4.h \\\n \t\tdocs/manual.txt obj.o build/lib.so &&\ndiff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh\nindex 22d697ff9a..576fb820b3 100755\n--- a/t/t9300-fast-import.sh\n+++ b/t/t9300-fast-import.sh\n@@ -4087,7 +4087,7 @@ test_expect_success 'Y: rewrite submodules via stdin' '\n \t)\n '\n \n-test_expect_failure 'fast-import: heredoc terminator at EOF is not re-parsed as command' '\n+test_expect_success 'fast-import: heredoc terminator at EOF is not re-parsed as command' '\n \ttest_when_finished \"rm -rf eof-deleteall\" &&\n \tgit init eof-deleteall &&\n \t(\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559101","messageId":"20261122140347.41990-7-gitster@pobox.com","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"[PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-22T14:03:47Z","receivedAt":"2026-11-22T14:03:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"There are a few code paths that fell into infinite loops because they\nassumed that strbuf_getwholeline() and friends clear the buffer when\nreturning EOF.  Now that strbuf_getwholeline() resets the buffer before\nreturning on feof(), these bugs are gone.\n\nAdd a few tests that illustrate the situations in which we used to fall\ninto infinite loops.  You can revert the previous step and use these\ntests to observe the fixed bugs.\n\nHere are step-by-step walkthroughs for these two code paths.\n\n * \"git clean -i\", when asked to filter by pattern ('f') in\n   filter_by_patterns_cmd(), enters a 'for (;;)' loop that reads\n   patterns into 'confirm' via git_read_line_interactively() (which\n   calls strbuf_getline()).  On EOF, it prints a newline and relies on\n   'if (!confirm.len) break;' to leave the loop.\n\n   When fed a pattern on an incomplete line (no trailing LF) that leaves\n   at least one candidate in 'del_list' (e.g., 'f' followed by '*.out'\n   without LF), the first call reads '*.out' into 'confirm', sets\n   feof(stdin), and returns 0.  On the next iteration,\n   strbuf_getwholeline() sees feof(stdin) and returns EOF without\n   resetting 'confirm'.  Because 'confirm' still has a non-zero '.len',\n   the loop never breaks and re-applies '*.out' forever.\n\n * \"git fast-import\", when fed a commit whose final line is a 'merge\n   <committish>' without a trailing LF, reads that line into\n   'command_buf' via read_next_command() (which calls\n   strbuf_getline_lf()), setting feof(stdin) while returning 0 (leaving\n   its static 'stdin_eof' flag 0).\n\n   parse_merge() then loops while skip_prefix() matches \"merge \" at the\n   start of 'command_buf', appends the parent to its list, and calls\n   read_next_command() without checking its return value, expecting EOF\n   to clear 'command_buf'.  Inside read_next_command(),\n   strbuf_getwholeline() sees feof(stdin) and returns EOF without\n   resetting 'command_buf', and read_next_command() sets 'stdin_eof' to\n   EOF and returns.  Back in parse_merge(), 'command_buf' still starts\n   with \"merge \", so the loop runs again; on every subsequent iteration,\n   read_next_command() returns early on 'if (stdin_eof)' without\n   touching 'command_buf', looping forever until OOM.\n\nNow strbuf_getwholeline() resets the buffer before returning on\nfeof(),\n\n * In \"git clean -i\", 'confirm' becomes empty on that second iteration,\n   the 'if (!confirm.len) break;' check exits filter_by_patterns_cmd(),\n   and the main menu then sees EOF and exits cleanly.\n\n * In \"git fast-import\", the first EOF in read_next_command() empties\n   'command_buf', causing skip_prefix() in parse_merge() to return 0 and\n   exit the loop cleanly.\n\nHence these infinite loops do not trigger anymore.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t7301-clean-interactive.sh | 11 +++++++++++\n t/t9300-fast-import.sh       | 33 +++++++++++++++++++++++++++++++++\n 2 files changed, 44 insertions(+)\n\ndiff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh\nindex c7a32fd7f0..0095674948 100755\n--- a/t/t7301-clean-interactive.sh\n+++ b/t/t7301-clean-interactive.sh\n@@ -492,6 +492,17 @@ test_expect_success 'git clean -id with prefix and path (ask)' '\n \n '\n \n+test_expect_success 'git clean -id (filter - EOF without trailing LF)' '\n+\tmkdir -p build docs &&\n+\ttouch a.out src/part3.c src/part3.h src/part4.c src/part4.h \\\n+\t\tdocs/manual.txt obj.o build/lib.so &&\n+\tprintf \"f\\n*.out\" |\n+\tgit clean -id &&\n+\ttest_path_is_file a.out &&\n+\ttest_path_is_file docs/manual.txt &&\n+\ttest_path_is_file src/part3.c\n+'\n+\n test_expect_success TTY 'git clean -i paints the header in HEADER color' '\n \t>a.out &&\n \techo q |\ndiff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh\nindex 576fb820b3..1fd3aeaef4 100755\n--- a/t/t9300-fast-import.sh\n+++ b/t/t9300-fast-import.sh\n@@ -4114,4 +4114,37 @@ test_expect_success 'fast-import: heredoc terminator at EOF is not re-parsed as\n \t)\n '\n \n+test_expect_success 'fast-import: merge line without trailing LF at EOF' '\n+\ttest_when_finished \"rm -rf eof-merge\" &&\n+\tgit init eof-merge &&\n+\t(\n+\t\tcd eof-merge &&\n+\t\tcat >input <<-\\EOF &&\n+\t\tcommit refs/heads/a\n+\t\tmark :1\n+\t\tcommitter A U Thor <author@example.com> 1000000000 +0000\n+\t\tdata 2\n+\t\ta\n+\n+\t\tcommit refs/heads/b\n+\t\tmark :2\n+\t\tcommitter A U Thor <author@example.com> 1000000001 +0000\n+\t\tdata 2\n+\t\tb\n+\n+\t\tcommit refs/heads/a\n+\t\tcommitter A U Thor <author@example.com> 1000000002 +0000\n+\t\tdata 6\n+\t\tmerge\n+\t\tfrom :1\n+\t\tEOF\n+\t\tprintf \"merge :2\" >>input &&\n+\t\tgit fast-import --quiet <input &&\n+\t\tgit rev-parse b >expect &&\n+\t\tgit rev-parse a^2 >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\ttest_must_fail git rev-parse --verify a^3\n+\t)\n+'\n+\n test_done\n-- \n2.56.1-766-g80003c9184\n\n\n"},{"id":"559148","messageId":"xmqq4id7d0ep.fsf@gitster.g","threadId":"66914","inReplyTo":"20261122140347.41990-1-gitster@pobox.com","subject":"Re: [PATCH 0/6] getwholeline should clear the buffer upon EOF","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-23T15:37:18Z","receivedAt":"2026-11-23T15:37:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> time.  Because the code that caused the existing bugs does not appear in\n> any of these steps, the log messages of patches 2-4 instead give\n> detailed walkthroughs and are rather long, which is another reason why\n> the nature of this series discourages following the usual pattern and\n> doing everything in one step.\n\nBy the way, I _could_ have instead called strbuf_reset() at strategic\nplaces in patches 2-4 to \"fix\" the bug without touching the root cause,\ni.e., strbuf_getwholeline(), and added test_expect_success tests.  That\nway, the commit log messages for these steps did not have to be such a\ndetailed walkthrough of how the bugs happen, simply because the code\nthat make problematic decisions based on a wrong assumption can be read\nin the context of these changes.  Which requires patch 5 that fixes the\nroot cause to revert these tentative \"fixes\" to call strbuf_reset()\nwhich becomes unnecessary when strbuf_getwholeline() stops leaving the\nstale contents in the buffer upon feof().\n\nI chose not to do so, as a series that adds some code in an early step\nand immediately removes the same code in a later step is awkward and\nwasteful.\n\n"}]}