From: Wonseok Oh Date: Sun, 22 Nov 2026 04:01:15 GMT Subject: Re: [PATCH] fast-import: catch missing data after inline M at EOF Message-ID: In-Reply-To: (Resending in plain text, as the list rejected my previous message for its HTML part. Sorry for the duplicate.) You're right. After an inline "M" command, this loop should only exit after reading a "data" command. Any other command is already rejected by parse_data(), but EOF just ends the loop and lets the uninitialized object ID go into the tree. So I think EOF should be rejected in the same way, by dying as soon as read_next_command() returns EOF: - while (read_next_command(state) != EOF) { + for (;;) { const char *v; + if (read_next_command(state) == EOF) + die(_("expected 'data n' command, found end of input")); if (skip_prefix(command_buf.buf, "cat-blob ", &v)) parse_cat_blob(state, v); else { parse_and_store_blob(&last_blob, &oid, 0); break; } } This fixes both the original bug and the infinite loop you pointed out. I picked the error message arbitrarily, so I'm happy to change it Thank you. 2026년 11월 22일 (일) 오후 12:46, Wonseok Oh 님이 작성: > > You're right. > After an inline "M" command, this loop should only exit after reading a "data" command. > Any other command is already rejected by parse_data(), > but EOF just ends the loop and lets the uninitialized object ID go into the tree. > So I think EOF should be rejected in the same way, by dying as soon as read_next_command() returns EOF: > > - while (read_next_command(state) != EOF) { > + for (;;) { > const char *v; > + if (read_next_command(state) == EOF) > + die(_("expected 'data n' command, found end of input")); > if (skip_prefix(command_buf.buf, "cat-blob ", &v)) > parse_cat_blob(state, v); > else { > parse_and_store_blob(&last_blob, &oid, 0); > break; > } > } > > This fixes both the original bug and the infinite loop you pointed out. > I picked the error message arbitrarily, so I'm happy to change it > > Thank you. > > 2026년 11월 22일 (일) 오전 5:04, Junio C Hamano 님이 작성: >> >> "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.