From: Junio C Hamano Date: Sun, 22 Nov 2026 14:03:45 GMT Subject: [PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF Message-ID: <20261122140347.41990-5-gitster@pobox.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 < 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 --- 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 1000000000 +0000 + data 5 + init + M 100644 inline file + data 6 + hello + + commit refs/heads/main + committer A U Thor 1000000001 +0000 + data <expect && + git ls-tree -r --name-only main >actual && + test_cmp expect actual + ) +' + test_done -- 2.56.1-766-g80003c9184