Re: [PATCH] fast-import: catch missing data after inline M at EOF
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 21, 2026, 20:03 UTC
- Message-ID
- <xmqqeccekl3l.fsf@gitster.g>
- In-Reply-To
- <pull.2323.git.1795246845186.gitgitgadget@gmail.com>
"Wonseok Oh via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 9 quoted lines
> If `M <mode> inline <path>` appears at the end of input, > file_change_m() exits its read loop on EOF without reading a `data` > command. It then puts an uninitialized stack object ID into the tree > and exits 0, leaving a tree entry for a missing blob that `git fsck` > flags. > > note_change_n() avoids this by calling parse_and_store_blob() at EOF, > which dies on the missing `data` command. Do the same in > file_change_m(), and add a test.
This reasoning seems wrong. The reason note_change_n() can afford to call parse_and_store_blob() is because it does not loop. This codepath needs a separate solution, I am afraid.
Show 15 quoted lines
> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index ab5d2a2344..2b271222e1 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -2456,8 +2456,9 @@ static void file_change_m(struct fast_import_state *state,
> if (S_ISDIR(mode))
> die(_("directories cannot be specified 'inline': %s"),
> command_buf.buf);
> - while (read_next_command(state) != EOF) {
> + for (;;) {
> const char *v;
> + read_next_command(state);
> if (skip_prefix(command_buf.buf, "cat-blob ", &v))
> parse_cat_blob(state, v);
> else {Imagine that "cat-blob <object name>" appears on an incomplete line without a trailing LF at the end of the stream. The updated code ignores the return value of read_next_command(), which would make us loop forever here, I suspect.
* We are about to read the final line "cat-blob <object name>". read_next_command() calls strbuf_getline_lf(), which returns 0 (which is assigned to stdin_eof), stuffs the incomplete line in command_buf.buf and returns. We have "cat-blob <object name>" in the command_buf (without the terminating LF).
* We call skip_prefix() and call parse_cat_blob(). It does its thing, but does not do anything to affect this loop.
* The loop calls read_next_command() again. strbuf_getline_lf() is called, because stdin_eof in read_next_command() is still 0. It notices that the underlying stdin stream has already given EOF, and returns EOF. read_next_command() reacts to this EOF, sets stdin_eof = EOF, and returns EOF.
* You updated the code to ignore this EOF. In the meantime, nobody touched command_buf in the previous step. We call skip_prefix() and call parse_cat_blob() with the same "cat-blob <object name>" that lacks the terminating newline.
* The loop calls read_next_command() again. strbuf_getline_lf() is not even called and we get EOF back. Again you ignore it and we do the parse_cat_blob() dance once again. Nobody will get us out of this loop.
In short, you have to pay attention to what read_next_command() returns, even if that does not mean you have to leave the function immediately, I think.