git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 22, 2026, 14:03 UTC
Message-ID
<20261122140347.41990-7-gitster@pobox.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
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 8 in “getwholeline should clear the buffer upon EOF”
  1. 0/6 getwholeline should clear the buffer upon EOFJunio C Hamano, Nov 22, 2026
  2. 1/6 mailinfo: do not call handle_body() when headers end at EOFJunio C Hamano, Nov 22, 2026
  3. 2/6 t4201: demonstrate shortlog reusing stale buffer on EOFJunio C Hamano, Nov 22, 2026
  4. 3/6 t7301: demonstrate "clean -i" ask mode deleting past EOFJunio C Hamano, Nov 22, 2026
  5. 4/6 t9300: demonstrate fast-import re-parsing heredoc delimiter at EOFJunio C Hamano, Nov 22, 2026
  6. 5/6 strbuf_getwholeline(): clear strbuf upon EOFJunio C Hamano, Nov 22, 2026
  7. 6/6 t7301, t9300: test EOF loops fixed by clearing strbuf upon EOFJunio C Hamano, Nov 22, 2026
  8. Junio C HamanoNov 23, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.