# [PATCH 0/6] getwholeline should clear the buffer upon EOF

8 messages from 2026-11-22 to 2026-11-23. Participants: Junio C Hamano.
Thread: https://gitlist.dev/t/66914

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 0/6] getwholeline should clear the buffer upon EOF
Message-ID: <20261122140347.41990-1-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-1-gitster%40pobox.com

```
This series, which is structured in a somewhat unusual way, addresses
various issues that are caused by a long-standing API bug that the
strbuf_getwholeline() family of functions shared for quite some time.

When they see feof() on their input stream, they immediately return
EOF without reading anything (which is good), but they do not clear
the output buffer.  It is not strictly *wrong* per se, but it proved
to be a rather error-prone API contract, as we see in the early part
of this series.

Patches 2 (shortlog), 3 (clean -i), and 4 (fast-import) explain the
existing bugs in various commands and add test(s) to trigger the bugs,
but the fix is left for patch 5, and the fix happens in code far away
from where these bugs exist.  The usual "describe the fix and
demonstrate it with new test_expect_success" pattern is deliberately not
followed.  This is because doing the usual thing would force reviewers
to understand the details of three unrelated commands illustrated in
patches 2, 3, and 4, as well as the solution in patch 5 at the same
time.  Because the code that caused the existing bugs does not appear in
any of these steps, the log messages of patches 2-4 instead give
detailed walkthroughs and are rather long, which is another reason why
the nature of this series discourages following the usual pattern and
doing everything in one step.

Then there is a bonus patch 6, which illustrates two additional
issues in "clean" and "fast-import" that cannot have
test_expect_failure tests that are safe to include before the problem
is fixed, because these breakages are infinite loops.

Patch 1, by the way, is what I posted elsewhere already.  This is a
caller that implicitly relied on strbuf_getwholeline() not clearing
the buffer upon feof(), and it is fixed in such a way that it does
not matter if strbuf_getwholeline() clears or leaves the buffer upon
feof() so "mailinfo" keeps working before or after patch 5 of this
series.

 1/6: mailinfo: do not call handle_body() when headers end at EOF
 2/6: t4201: demonstrate shortlog reusing stale buffer on EOF
 3/6: t7301: demonstrate "clean -i" ask mode deleting past EOF
 4/6: t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF
 5/6: strbuf_getwholeline(): clear strbuf upon EOF
 6/6: t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF

 mailinfo.c                   | 18 +++++++----
 strbuf.c                     |  6 ++--
 t/t4201-shortlog.sh          | 29 +++++++++++++++++
 t/t5100-mailinfo.sh          | 18 +++++++++++
 t/t7301-clean-interactive.sh | 31 +++++++++++++++++++
 t/t9300-fast-import.sh       | 60 ++++++++++++++++++++++++++++++++++++
 6 files changed, 154 insertions(+), 8 deletions(-)

-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 1/6] mailinfo: do not call handle_body() when headers end at EOF
Message-ID: <20261122140347.41990-2-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-2-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

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

If the input had only header lines, however, a call to fgetc() in
read_one_header_line(), made to look ahead for header-folding, would
have hit EOF already.  strbuf_getline_lf(), which is called by
read_one_header_line(), sees feof(), and returns EOF early without
resetting 'line', and then read_one_header_line() returns 0.

mailinfo() now 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, so that we can tell it and
an empty-line case (i.e., a proper end of header lines) apart, and never
call handle_body() if we see an EOF.

By the way, checking feof() in mailinfo() would not work, 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>
---
 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



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF
Message-ID: <20261122140347.41990-3-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-3-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
When builtin/shortlog.c:read_from_stdin() reads from the standard input,
it skips the header lines until it finds the header (either "author" or
"committer") we are interested in, parses what is on the line, skips all
the remaining header lines, and then skips the blank lines that follow,
to find the commit subject line.

But this assumes that strbuf_getline_lf() clears the buffer when
returning EOF.  If the input ends on an incomplete line, that is not
true.  Stale contents in 'oneline' cause the command to produce wrong
output.

Here are step-by-step walkthroughs for the two failure modes in this
code path.

 * If an incomplete "Author:" line at EOF follows an earlier commit, the
   earlier commit leaves its subject in 'oneline'.  The outer loop reads
   the next "Author:" line into 'ident', sets feof(stdin) while scanning
   for LF, and returns 0.  We successfully read the author of this
   second commit, and then the two inner loops to skip other headers and
   blank lines call strbuf_getline_lf() on 'oneline', which sees
   feof(stdin) and returns EOF without resetting 'oneline'.  The strbuf
   still has the subject of the previous commit, and that is what is
   given to insert_one_record().

 * If an incomplete header line (such as "Date:") appears at EOF, after
   matching "Author:", the first inner loop reads the "Date:" line into
   'oneline', sets feof(stdin), and returns 0.  Because '.len' is
   non-zero, the loop runs again; both that iteration and the second
   inner loop see feof(stdin) and return EOF without resetting
   'oneline'.  insert_one_record() therefore records the discarded
   "Date:" header as the commit's subject.

Add two test_expect_failure tests to t4201-shortlog.sh illustrating
these issues.

Once strbuf_getwholeline() is changed to reset the buffer before
returning on feof(), the first EOF read into 'oneline' empties it in
both cases, so insert_one_record() records an empty subject, matching
the behavior when the final line is LF-terminated.  Because there are
other code paths that will be "fixed" by such a change to
strbuf_getwholeline(), we will not fix it in this commit (yet), and stop
at documenting the issue in the form of test_expect_failure.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t4201-shortlog.sh | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index 10c43e6e75..dc1b175653 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -447,4 +447,33 @@ test_expect_success 'unknown revision options are reported correctly' '
 	test_grep "unknown option .*--no-such-option" err
 '
 
+test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '
+	test_write_lines \
+		"Author: A U Thor <author@example.com>" \
+		"" \
+		"    First commit" >in &&
+	printf "%s" "Author: B U Thor <b@example.com>" >>in &&
+	test_write_lines \
+		"A U Thor (1):" \
+		"      First commit" \
+		"" \
+		"B U Thor (1):" \
+		"      " \
+		"" >expect &&
+	git shortlog <in >actual &&
+	test_cmp expect actual
+'
+
+test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '
+	printf "%s\n%s" \
+		"Author: A U Thor <author@example.com>" \
+		"Date:   Mon Jan 1 00:00:00 2024 +0000" >in &&
+	test_write_lines \
+		"A U Thor (1):" \
+		"      " \
+		"" >expect &&
+	git shortlog <in >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 3/6] t7301: demonstrate "clean -i" ask mode deleting past EOF
Message-ID: <20261122140347.41990-4-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-4-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
After builtin/clean.c:ask_each_cmd() asks a "remove this?" question, it
reads the interactive answer from the user to decide what to do.  This
is done in a loop to remove potentially many items.

But this assumes that strbuf_getline() clears the buffer when returning
EOF.  A "y" answer on an incomplete line causes the command to delete
every remaining untracked item instead of stopping on EOF.

Here is a step-by-step walkthrough of this code path.

In ask_each_cmd(), a loop iterates over each candidate in 'del_list'.
While '!eof', it prompts and calls git_read_line_interactively() (which
calls strbuf_getline()) to read into 'confirm'.  On EOF, it prints a
newline, sets 'eof = 1', and then falls through to check 'if
(!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len))' to drop
items that should not be removed, assuming EOF left 'confirm' empty.

Imagine that the user types 'a' to choose 'ask each' mode, and gives 'y'
with no trailing LF:

 * For the first item, strbuf_getline() reads "y" into 'confirm', sets
   feof(stdin) while scanning for LF, and returns 0.  Because "y"
   matches a prefix of "yes", that first item stays in 'del_list' to be
   removed.

 * For the second item, '!eof' is still true, so strbuf_getline() is
   called on 'confirm'.  strbuf_getwholeline() sees feof(stdin) and
   returns EOF without resetting 'confirm'.  ask_each_cmd() sets 'eof =
   1' and falls through with 'confirm' still containing "y", keeping the
   second item in 'del_list' for removal.

 * For every remaining item in 'del_list', 'if (!eof)' is skipped while
   'confirm' remains "y", so every remaining untracked file is removed
   contrary to the "Ctrl-D should stop removing files" intent.

Add a test_expect_failure test to t7301-clean-interactive.sh
illustrating this issue.

Once strbuf_getwholeline() is changed to reset the buffer before
returning on feof(), the EOF read on the second item empties 'confirm',
making '!confirm.len' true for the second and all subsequent items so
only a.out is removed.  Because there are other code paths that will be
"fixed" by such a change to strbuf_getwholeline(), we will not fix it in
this commit (yet), and stop at documenting the issue in the form of
test_expect_failure.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t7301-clean-interactive.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
index de59ba3724..73e923d212 100755
--- a/t/t7301-clean-interactive.sh
+++ b/t/t7301-clean-interactive.sh
@@ -381,6 +381,26 @@ test_expect_success 'git clean -id (ask)' '
 
 '
 
+test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '
+	mkdir -p build docs &&
+	touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+		docs/manual.txt obj.o build/lib.so &&
+	printf "a\ny" |
+	git clean -id &&
+	test_path_is_file Makefile &&
+	test_path_is_file README &&
+	test_path_is_file src/part1.c &&
+	test_path_is_file src/part2.c &&
+	test_path_is_missing a.out &&
+	test_path_is_file docs/manual.txt &&
+	test_path_is_file src/part3.c &&
+	test_path_is_file src/part3.h &&
+	test_path_is_file src/part4.c &&
+	test_path_is_file src/part4.h &&
+	test_path_is_file obj.o &&
+	test_path_is_file build/lib.so
+'
+
 test_expect_success 'git clean -id (ask - Ctrl+D)' '
 
 	mkdir -p build docs &&
-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF
Message-ID: <20261122140347.41990-5-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-5-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
After reading a commit message via builtin/fast-import.c:parse_data(),
parse_new_commit() reads the next line into 'command_buf' via
read_next_command() without checking its return value, checks for
optional "from" and "merge" lines, and then loops over file-change
commands while 'command_buf' is non-empty.

But this assumes that strbuf_getline_lf() clears the buffer when
returning EOF.  If the input ends right after a heredoc commit message
with no trailing blank line, that is not true.  The stale heredoc
delimiter left in 'command_buf' can then be re-parsed as a file-change
command, corrupting the commit's tree.

Here is a step-by-step walkthrough of this code path.

In parse_new_commit(), after reading the commit message with
parse_data(), the function calls read_next_command() without checking
its return value, runs parse_from() and parse_merge(), and then
dispatches file-change commands in a 'while (command_buf.len > 0)' loop.
It assumes EOF clears 'command_buf' so that the loop is skipped.

When a commit message uses a heredoc delimiter matching a file-change
command (e.g., "data <<deleteall") and the stream ends right after the
terminator line ("deleteall\n", with no trailing blank line):

 * parse_data() reads into 'command_buf' via strbuf_getline_lf() until
   'command_buf' matches "deleteall" and breaks out of its loop, leaving
   "deleteall" in 'command_buf'.

 * At the end of parse_data(), skip_optional_lf() calls fgetc(stdin),
   which hits EOF and sets feof(stdin).  (Note that the input does not
   even need an incomplete line.)

 * Back in parse_new_commit(), read_next_command() calls
   strbuf_getline_lf() on 'command_buf'.  strbuf_getwholeline() sees
   feof(stdin) and returns EOF without resetting 'command_buf'.
   read_next_command() records 'stdin_eof' and returns EOF, which
   parse_new_commit() ignores.

 * parse_from() and parse_merge() do not match "deleteall", so execution
   enters 'while (command_buf.len > 0)'.  Because 'command_buf' still
   holds "deleteall", parse_new_commit() calls file_change_deleteall(),
   discarding every file inherited from the parent commit before the
   loop's trailing read_next_command() check sees EOF and breaks.

Add a test_expect_failure test to t9300-fast-import.sh illustrating this
issue.

Once strbuf_getwholeline() is changed to reset the buffer before
returning on feof(), read_next_command() after parse_data() empties
'command_buf'.  The 'while (command_buf.len > 0)' loop is then skipped,
and the parent's tree is preserved.  Because there are other code paths
that will be "fixed" by such a change to strbuf_getwholeline(), we will
not fix it in this commit (yet), and stop at documenting the issue in
the form of test_expect_failure.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t9300-fast-import.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 15f511fb6b..22d697ff9a 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -4087,4 +4087,31 @@ test_expect_success 'Y: rewrite submodules via stdin' '
 	)
 '
 
+test_expect_failure 'fast-import: heredoc terminator at EOF is not re-parsed as command' '
+	test_when_finished "rm -rf eof-deleteall" &&
+	git init eof-deleteall &&
+	(
+		cd eof-deleteall &&
+		git fast-import --quiet <<-\EOF &&
+		commit refs/heads/main
+		mark :1
+		committer A U Thor <author@example.com> 1000000000 +0000
+		data 5
+		init
+		M 100644 inline file
+		data 6
+		hello
+
+		commit refs/heads/main
+		committer A U Thor <author@example.com> 1000000001 +0000
+		data <<deleteall
+		second commit
+		deleteall
+		EOF
+		echo file >expect &&
+		git ls-tree -r --name-only main >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF
Message-ID: <20261122140347.41990-6-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-6-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
strbuf_getwholeline(), and its frontends like strbuf_getline_lf(), has
an early return upon seeing that the stream has reached EOF; curiously,
it does not touch the strbuf when it takes this early return code path.

And indeed, a few callers of these functions make a false assumption
that their buffer will be cleared when the function returns EOF, and due
to that assumption, they are buggy, as we saw in the previous commits.

Teach strbuf_getwholeline() to clear the buffer when taking the fast EOF
return path.  By doing so, we fix all of these code paths, so we can
turn the test_expect_failure tests we added into test_expect_success.

This would break existing callers that expect that strbuf_getwholeline()
or its frontends do not clear the buffer upon EOF, but such callers are
unlikely.  If you call strbuf_getline(&buf), the normal thing to expect
is to lose what you originally had in buf.  Expecting that the original
contents can be read back again when the call returns EOF is not really
sensible.  There is indeed nobody that relies on the fact that the
original contents are still available to read.

If strbuf_getwholeline() is "fixed" to clear the buffer upon EOF, "git
mailinfo" would call handle_body() with an empty 'line' upon feof(), but
if this were a header-only message that was sent as "format=flowed"
type, handle_filter_flowed() would get called and access .buf[len - 1]
for this empty line whose len == 0, reading out of bounds.  This issue
has been addressed in an earlier patch to teach the code to never call
handle_body() after EOF with an empty line.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 strbuf.c                     | 6 ++++--
 t/t4201-shortlog.sh          | 4 ++--
 t/t7301-clean-interactive.sh | 2 +-
 t/t9300-fast-import.sh       | 2 +-
 4 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index c3baa47b3f..62abf01bfa 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -643,8 +643,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	ssize_t r;
 
-	if (feof(fp))
+	if (feof(fp)) {
+		strbuf_reset(sb);
 		return EOF;
+	}
 
 	/* Translate slopbuf to NULL, as we cannot call realloc on it */
 	if (!sb->alloc)
@@ -688,10 +690,10 @@ int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
 {
 	int ch;
 
+	strbuf_reset(sb);
 	if (feof(fp))
 		return EOF;
 
-	strbuf_reset(sb);
 	flockfile(fp);
 	while ((ch = getc_unlocked(fp)) != EOF) {
 		if (!strbuf_avail(sb))
diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index dc1b175653..fc731da0d3 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -447,7 +447,7 @@ test_expect_success 'unknown revision options are reported correctly' '
 	test_grep "unknown option .*--no-such-option" err
 '
 
-test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '
+test_expect_success 'shortlog from stdin with incomplete Author line at EOF' '
 	test_write_lines \
 		"Author: A U Thor <author@example.com>" \
 		"" \
@@ -464,7 +464,7 @@ test_expect_failure 'shortlog from stdin with incomplete Author line at EOF' '
 	test_cmp expect actual
 '
 
-test_expect_failure 'shortlog from stdin with incomplete header line at EOF' '
+test_expect_success 'shortlog from stdin with incomplete header line at EOF' '
 	printf "%s\n%s" \
 		"Author: A U Thor <author@example.com>" \
 		"Date:   Mon Jan 1 00:00:00 2024 +0000" >in &&
diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
index 73e923d212..c7a32fd7f0 100755
--- a/t/t7301-clean-interactive.sh
+++ b/t/t7301-clean-interactive.sh
@@ -381,7 +381,7 @@ test_expect_success 'git clean -id (ask)' '
 
 '
 
-test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '
+test_expect_success 'git clean -id (ask - EOF without trailing LF)' '
 	mkdir -p build docs &&
 	touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
 		docs/manual.txt obj.o build/lib.so &&
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 22d697ff9a..576fb820b3 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -4087,7 +4087,7 @@ test_expect_success 'Y: rewrite submodules via stdin' '
 	)
 '
 
-test_expect_failure 'fast-import: heredoc terminator at EOF is not re-parsed as command' '
+test_expect_success 'fast-import: heredoc terminator at EOF is not re-parsed as command' '
 	test_when_finished "rm -rf eof-deleteall" &&
 	git init eof-deleteall &&
 	(
-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-22 14:03

Subject: [PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF
Message-ID: <20261122140347.41990-7-gitster@pobox.com>
URL: https://gitlist.dev/e/20261122140347.41990-7-gitster%40pobox.com
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
There are a few code paths that fell into infinite loops because they
assumed that strbuf_getwholeline() and friends clear the buffer when
returning EOF.  Now that strbuf_getwholeline() resets the buffer before
returning on feof(), these bugs are gone.

Add a few tests that illustrate the situations in which we used to fall
into infinite loops.  You can revert the previous step and use these
tests to observe the fixed bugs.

Here are step-by-step walkthroughs for these two code paths.

 * "git clean -i", when asked to filter by pattern ('f') in
   filter_by_patterns_cmd(), enters a 'for (;;)' loop that reads
   patterns into 'confirm' via git_read_line_interactively() (which
   calls strbuf_getline()).  On EOF, it prints a newline and relies on
   'if (!confirm.len) break;' to leave the loop.

   When fed a pattern on an incomplete line (no trailing LF) that leaves
   at least one candidate in 'del_list' (e.g., 'f' followed by '*.out'
   without LF), the first call reads '*.out' into 'confirm', sets
   feof(stdin), and returns 0.  On the next iteration,
   strbuf_getwholeline() sees feof(stdin) and returns EOF without
   resetting 'confirm'.  Because 'confirm' still has a non-zero '.len',
   the loop never breaks and re-applies '*.out' forever.

 * "git fast-import", when fed a commit whose final line is a 'merge
   <committish>' without a trailing LF, reads that line into
   'command_buf' via read_next_command() (which calls
   strbuf_getline_lf()), setting feof(stdin) while returning 0 (leaving
   its static 'stdin_eof' flag 0).

   parse_merge() then loops while skip_prefix() matches "merge " at the
   start of 'command_buf', appends the parent to its list, and calls
   read_next_command() without checking its return value, expecting EOF
   to clear 'command_buf'.  Inside read_next_command(),
   strbuf_getwholeline() sees feof(stdin) and returns EOF without
   resetting 'command_buf', and read_next_command() sets 'stdin_eof' to
   EOF and returns.  Back in parse_merge(), 'command_buf' still starts
   with "merge ", so the loop runs again; on every subsequent iteration,
   read_next_command() returns early on 'if (stdin_eof)' without
   touching 'command_buf', looping forever until OOM.

Now strbuf_getwholeline() resets the buffer before returning on
feof(),

 * In "git clean -i", 'confirm' becomes empty on that second iteration,
   the 'if (!confirm.len) break;' check exits filter_by_patterns_cmd(),
   and the main menu then sees EOF and exits cleanly.

 * In "git fast-import", the first EOF in read_next_command() empties
   'command_buf', causing skip_prefix() in parse_merge() to return 0 and
   exit the loop cleanly.

Hence these infinite loops do not trigger anymore.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t7301-clean-interactive.sh | 11 +++++++++++
 t/t9300-fast-import.sh       | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
index c7a32fd7f0..0095674948 100755
--- a/t/t7301-clean-interactive.sh
+++ b/t/t7301-clean-interactive.sh
@@ -492,6 +492,17 @@ test_expect_success 'git clean -id with prefix and path (ask)' '
 
 '
 
+test_expect_success 'git clean -id (filter - EOF without trailing LF)' '
+	mkdir -p build docs &&
+	touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+		docs/manual.txt obj.o build/lib.so &&
+	printf "f\n*.out" |
+	git clean -id &&
+	test_path_is_file a.out &&
+	test_path_is_file docs/manual.txt &&
+	test_path_is_file src/part3.c
+'
+
 test_expect_success TTY 'git clean -i paints the header in HEADER color' '
 	>a.out &&
 	echo q |
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 576fb820b3..1fd3aeaef4 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -4114,4 +4114,37 @@ test_expect_success 'fast-import: heredoc terminator at EOF is not re-parsed as
 	)
 '
 
+test_expect_success 'fast-import: merge line without trailing LF at EOF' '
+	test_when_finished "rm -rf eof-merge" &&
+	git init eof-merge &&
+	(
+		cd eof-merge &&
+		cat >input <<-\EOF &&
+		commit refs/heads/a
+		mark :1
+		committer A U Thor <author@example.com> 1000000000 +0000
+		data 2
+		a
+
+		commit refs/heads/b
+		mark :2
+		committer A U Thor <author@example.com> 1000000001 +0000
+		data 2
+		b
+
+		commit refs/heads/a
+		committer A U Thor <author@example.com> 1000000002 +0000
+		data 6
+		merge
+		from :1
+		EOF
+		printf "merge :2" >>input &&
+		git fast-import --quiet <input &&
+		git rev-parse b >expect &&
+		git rev-parse a^2 >actual &&
+		test_cmp expect actual &&
+		test_must_fail git rev-parse --verify a^3
+	)
+'
+
 test_done
-- 
2.56.1-766-g80003c9184



```

## Junio C Hamano, 2026-11-23 15:37

Subject: Re: [PATCH 0/6] getwholeline should clear the buffer upon EOF
Message-ID: <xmqq4id7d0ep.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq4id7d0ep.fsf%40gitster.g
In-Reply-To: <20261122140347.41990-1-gitster@pobox.com>

```
Junio C Hamano <gitster@pobox.com> writes:

> time.  Because the code that caused the existing bugs does not appear in
> any of these steps, the log messages of patches 2-4 instead give
> detailed walkthroughs and are rather long, which is another reason why
> the nature of this series discourages following the usual pattern and
> doing everything in one step.

By the way, I _could_ have instead called strbuf_reset() at strategic
places in patches 2-4 to "fix" the bug without touching the root cause,
i.e., strbuf_getwholeline(), and added test_expect_success tests.  That
way, the commit log messages for these steps did not have to be such a
detailed walkthrough of how the bugs happen, simply because the code
that make problematic decisions based on a wrong assumption can be read
in the context of these changes.  Which requires patch 5 that fixes the
root cause to revert these tentative "fixes" to call strbuf_reset()
which becomes unnecessary when strbuf_getwholeline() stops leaving the
stale contents in the buffer upon feof().

I chose not to do so, as a series that adds some code in an early step
and immediately removes the same code in a later step is awkward and
wasteful.


```
