git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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.

Previous: Wonseok Oh via GitGitGadgetNext: Wonseok Oh via GitGitGadget
Message 2 of 6 in “fast-import: catch missing data after inline M at EOF”
  1. fast-import: catch missing data after inline M at EOFWonseok Oh via GitGitGadget, Nov 21, 2026
  2. Junio C HamanoNov 21, 2026
  3. fast-import: die on EOF after an inline M commandWonseok Oh via GitGitGadget, Nov 24, 2026
  4. Junio C HamanoNov 24, 2026
  5. Wonseok OhNov 25, 2026
  6. Wonseok OhNov 22, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.