From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF
Date: Sun, 22 Nov 2026 06:03:45 -0800 [thread overview]
Message-ID: <20261122140347.41990-5-gitster@pobox.com> (raw)
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
next prev 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 ` Junio C Hamano [this message]
2026-11-22 14:03 ` [PATCH 5/6] strbuf_getwholeline(): clear strbuf upon EOF Junio C Hamano
2026-11-22 14:03 ` [PATCH 6/6] t7301, t9300: test EOF loops fixed by clearing " Junio C Hamano
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-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.