Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing strbuf upon EOF
Date: Sun, 22 Nov 2026 06:03:47 -0800	[thread overview]
Message-ID: <20261122140347.41990-7-gitster@pobox.com> (raw)
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


  parent reply	other threads:[~2026-11-22 14:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 14:03 [PATCH 0/6] getwholeline should clear the buffer upon EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 1/6] mailinfo: do not call handle_body() when headers end at EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 2/6] t4201: demonstrate shortlog reusing stale buffer on EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 3/6] t7301: demonstrate "clean -i" ask mode deleting past EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF Junio C Hamano
2026-11-22 14:03 ` Junio C Hamano [this message]
2026-11-23 15:37 ` [PATCH 0/6] getwholeline should clear the buffer " Junio C Hamano

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=20261122140347.41990-7-gitster@pobox.com \
    --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