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

[PATCH v2] fast-import: die on EOF after an inline M command

From
WGWonseok Oh via GitGitGadget <gitgitgadget@gmail.com>
Date
Nov 24, 2026, 05:12 UTC
Message-ID
<pull.2323.v2.git.1795497145494.gitgitgadget@gmail.com>
In-Reply-To
<pull.2323.git.1795246845186.gitgitgadget@gmail.com>
From: Wonseok Oh <marinelay7@gmail.com>

After "M <mode> inline <path>", fast-import expects a "data" command. If the input ends there, file_change_m() leaves its read loop without reading any data and writes an uninitialized object ID into the tree. fast-import then exits 0, and "git fsck" reports a missing blob.

Die when read_next_command() returns EOF in that loop, just as parse_data() already dies on any command other than "data".

Signed-off-by: Wonseok Oh <marinelay7@gmail.com>
---
    fast-import: die on EOF after an inline M command
    
    Changes since v1:
    
     * Check the return value of read_next_command() and die on EOF, and add
       a test for it. As Junio pointed out, v1 ignored the return value and
       looped forever when the input ended with a "cat-blob" line without a
       trailing newline.
     * Reword the commit message to drop the inaccurate comparison with
       note_change_n().
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2323%2Fmarinelay%2Ffast-import-inline-eof-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2323/marinelay/fast-import-inline-eof-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2323
Range-diff vs v1:
 1:  3917798a22 ! 1:  1736db5ae9 fast-import: catch missing data after inline M at EOF
     @@ Metadata
      Author: Wonseok Oh <marinelay7@gmail.com>
      
       ## Commit message ##
     -    fast-import: catch missing data after inline M at EOF
     +    fast-import: die on EOF after an inline M command
      
     -    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.
     +    After "M <mode> inline <path>", fast-import expects a "data" command.
     +    If the input ends there, file_change_m() leaves its read loop without
     +    reading any data and writes an uninitialized object ID into the tree.
     +    fast-import then exits 0, and "git fsck" reports a missing blob.
      
     -    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.
     +    Die when read_next_command() returns EOF in that loop, just as
     +    parse_data() already dies on any command other than "data".
      
          Signed-off-by: Wonseok Oh <marinelay7@gmail.com>
      
     @@ builtin/fast-import.c: static void file_change_m(struct fast_import_state *state
      -		while (read_next_command(state) != EOF) {
      +		for (;;) {
       			const char *v;
     -+			read_next_command(state);
     ++			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 {
     @@ t/t9300-fast-import.sh: test_expect_success 'D: verify file6' '
      +	test_grep "expected ${SQ}data n${SQ} command" err &&
      +	test_must_fail git rev-parse --verify -q refs/heads/D-inline-eof
      +'
     ++
     ++test_expect_success 'D: fail on unterminated cat-blob after inline M at end of input' '
     ++	blob=$(echo hello | git hash-object -w --stdin) &&
     ++	cat >input <<-INPUT_END &&
     ++	commit refs/heads/D-inline-cat-blob-eof
     ++	committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
     ++	data <<COMMIT
     ++	inline data cut off after cat-blob
     ++	COMMIT
     ++
     ++	M 644 inline file
     ++	INPUT_END
     ++	printf "cat-blob %s" "$blob" >>input &&
     ++
     ++	test_must_fail git fast-import <input >out 2>err &&
     ++	test_grep "expected ${SQ}data n${SQ} command" err &&
     ++	test_must_fail git rev-parse --verify -q refs/heads/D-inline-cat-blob-eof
     ++'
      +
       ###
       ### series E
 builtin/fast-import.c  |  4 +++-
 t/t9300-fast-import.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/builtin/fast-import.c b/builtin/fast-import.c
index ab5d2a2344..0a0f5a91a5 100644
--- a/builtin/fast-import.c
+++ b/builtin/fast-import.c
@@ -2456,8 +2456,10 @@ 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;
+			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 {
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 15f511fb6b..29da93be1d 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -745,6 +745,40 @@ test_expect_success 'D: verify file6' '
 	test_cmp expect actual
 '
 
+test_expect_success 'D: fail on inline data cut off at end of input' '
+	cat >input <<-INPUT_END &&
+	commit refs/heads/D-inline-eof
+	committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+	data <<COMMIT
+	inline data cut off
+	COMMIT
+
+	M 644 inline file
+	INPUT_END
+
+	test_must_fail git fast-import <input 2>err &&
+	test_grep "expected ${SQ}data n${SQ} command" err &&
+	test_must_fail git rev-parse --verify -q refs/heads/D-inline-eof
+'
+
+test_expect_success 'D: fail on unterminated cat-blob after inline M at end of input' '
+	blob=$(echo hello | git hash-object -w --stdin) &&
+	cat >input <<-INPUT_END &&
+	commit refs/heads/D-inline-cat-blob-eof
+	committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+	data <<COMMIT
+	inline data cut off after cat-blob
+	COMMIT
+
+	M 644 inline file
+	INPUT_END
+	printf "cat-blob %s" "$blob" >>input &&
+
+	test_must_fail git fast-import <input >out 2>err &&
+	test_grep "expected ${SQ}data n${SQ} command" err &&
+	test_must_fail git rev-parse --verify -q refs/heads/D-inline-cat-blob-eof
+'
+
 ###
 ### series E
 ###

base-commit: f7de81456940dd6f3274e461149bba12965de609
-- 
gitgitgadget
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 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.