From: Junio C Hamano Date: Sat, 21 Nov 2026 20:03:58 GMT Subject: Re: [PATCH] fast-import: catch missing data after inline M at EOF Message-ID: In-Reply-To: "Wonseok Oh via GitGitGadget" writes: > If `M inline ` 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. > 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 " 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 ". 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 " 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 " 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.