[PATCH 4/6] t9300: demonstrate fast-import re-parsing heredoc delimiter at EOF
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 22, 2026, 14:03 UTC
- 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 <<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