Git development
 help / color / mirror / Atom feed
* [PATCH] fast-import: catch missing data after inline M at EOF
@ 2026-11-21  7:40 Wonseok Oh via GitGitGadget
  2026-11-21 20:03 ` Junio C Hamano
  2026-11-24  5:12 ` [PATCH v2] fast-import: die on EOF after an inline M command Wonseok Oh via GitGitGadget
  0 siblings, 2 replies; 6+ messages in thread
From: Wonseok Oh via GitGitGadget @ 2026-11-21  7:40 UTC (permalink / raw)
  To: git; +Cc: Wonseok Oh, Wonseok Oh

From: Wonseok Oh <marinelay7@gmail.com>

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.

Signed-off-by: Wonseok Oh <marinelay7@gmail.com>
---
    fast-import: catch missing data after inline M at EOF

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2323%2Fmarinelay%2Ffast-import-inline-eof-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2323/marinelay/fast-import-inline-eof-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2323

 builtin/fast-import.c  |  3 ++-
 t/t9300-fast-import.sh | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

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 {
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
index 15f511fb6b..c291b0cf70 100755
--- a/t/t9300-fast-import.sh
+++ b/t/t9300-fast-import.sh
@@ -745,6 +745,22 @@ 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
+'
+
 ###
 ### series E
 ###

base-commit: f7de81456940dd6f3274e461149bba12965de609
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] fast-import: catch missing data after inline M at EOF
  2026-11-21  7:40 [PATCH] fast-import: catch missing data after inline M at EOF Wonseok Oh via GitGitGadget
@ 2026-11-21 20:03 ` Junio C Hamano
       [not found]   ` <CAD2pp5sCB8kj6243g63X+Msg=cFR4ODYto5OUA3rYrnioVxShQ@mail.gmail.com>
  2026-11-24  5:12 ` [PATCH v2] fast-import: die on EOF after an inline M command Wonseok Oh via GitGitGadget
  1 sibling, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2026-11-21 20:03 UTC (permalink / raw)
  To: Wonseok Oh via GitGitGadget; +Cc: git, Wonseok Oh, Wonseok Oh

"Wonseok Oh via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.

> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] fast-import: catch missing data after inline M at EOF
       [not found]   ` <CAD2pp5sCB8kj6243g63X+Msg=cFR4ODYto5OUA3rYrnioVxShQ@mail.gmail.com>
@ 2026-11-22  4:01     ` Wonseok Oh
  0 siblings, 0 replies; 6+ messages in thread
From: Wonseok Oh @ 2026-11-22  4:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

(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 <wonseok_oh@korea.ac.kr>님이 작성:
>
> 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 <gitster@pobox.com>님이 작성:
>>
>> "Wonseok Oh via GitGitGadget" <gitgitgadget@gmail.com> writes:
>>
>> > 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.
>>
>> > 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] fast-import: die on EOF after an inline M command
  2026-11-21  7:40 [PATCH] fast-import: catch missing data after inline M at EOF Wonseok Oh via GitGitGadget
  2026-11-21 20:03 ` Junio C Hamano
@ 2026-11-24  5:12 ` Wonseok Oh via GitGitGadget
  2026-11-24 18:12   ` Junio C Hamano
  1 sibling, 1 reply; 6+ messages in thread
From: Wonseok Oh via GitGitGadget @ 2026-11-24  5:12 UTC (permalink / raw)
  To: git; +Cc: Wonseok Oh, Wonseok Oh

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] fast-import: die on EOF after an inline M command
  2026-11-24  5:12 ` [PATCH v2] fast-import: die on EOF after an inline M command Wonseok Oh via GitGitGadget
@ 2026-11-24 18:12   ` Junio C Hamano
  2026-11-25  1:46     ` Wonseok Oh
  0 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2026-11-24 18:12 UTC (permalink / raw)
  To: Wonseok Oh via GitGitGadget; +Cc: git, Wonseok Oh, Wonseok Oh

"Wonseok Oh via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.

This describes the problem accurately, but wouldn't the series at
https://lore.kernel.org/git/20261122140347.41990-1-gitster@pobox.com/
solve this infinite loop, together with a few others?

Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] fast-import: die on EOF after an inline M command
  2026-11-24 18:12   ` Junio C Hamano
@ 2026-11-25  1:46     ` Wonseok Oh
  0 siblings, 0 replies; 6+ messages in thread
From: Wonseok Oh @ 2026-11-25  1:46 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Wonseok Oh via GitGitGadget, git

Hello Junio,

> This describes the problem accurately, but wouldn't the series at
> https://lore.kernel.org/git/20261122140347.41990-1-gitster@pobox.com/
> solve this infinite loop, together with a few others?

With your series, v1 patch no longer gets stuck
in an infinite loop on the unterminated "cat-blob" input.

If you prefer v1 on top of your series,
I'll go back to it.

Thanks.

2026년 11월 25일 (수) 오전 3:12, Junio C Hamano <gitster@pobox.com>님이 작성:
>
> "Wonseok Oh via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > 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.
>
> This describes the problem accurately, but wouldn't the series at
> https://lore.kernel.org/git/20261122140347.41990-1-gitster@pobox.com/
> solve this infinite loop, together with a few others?
>
> Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-25  1:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  7:40 [PATCH] fast-import: catch missing data after inline M at EOF Wonseok Oh via GitGitGadget
2026-11-21 20:03 ` Junio C Hamano
     [not found]   ` <CAD2pp5sCB8kj6243g63X+Msg=cFR4ODYto5OUA3rYrnioVxShQ@mail.gmail.com>
2026-11-22  4:01     ` Wonseok Oh
2026-11-24  5:12 ` [PATCH v2] fast-import: die on EOF after an inline M command Wonseok Oh via GitGitGadget
2026-11-24 18:12   ` Junio C Hamano
2026-11-25  1:46     ` Wonseok Oh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox