Git development
 help / color / mirror / Atom feed
* [PATCH] tree-walk: reject tree entries whose name length overflows an int
@ 2026-11-30 14:42 Kristofer Karlsson via GitGitGadget
  2026-12-01 15:11 ` Patrick Steinhardt
  0 siblings, 1 reply; 4+ messages in thread
From: Kristofer Karlsson via GitGitGadget @ 2026-11-30 14:42 UTC (permalink / raw)
  To: git
  Cc: Elia Pinto, Elijah Newren, Johannes Schindelin, Harald Nordgren,
	Kristofer Karlsson, Kristofer Karlsson

From: Kristofer Karlsson <krka@spotify.com>

The EXPENSIVE tests 13-15 in t1600 have been failing in CI on all
64-bit platforms since 53d5cfc3b8 (tree-walk: use size_t for the size
of tree buffers, 2026-10-30) was merged:

  -fatal: index entry path is too long (4294967298 bytes)
  +fatal: malformed mode in tree entry

The tests were added in 89048b48fb (t1600-index: add expensive tests
for over-long tree paths, 2026-10-16).  They use a tree with a single
entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores
that length in the "int" pathlen of struct name_entry, where it wraps
to -2.  update_tree_entry() then uses it to find the next entry, ends
up in the middle of the name, and fails to parse it.

Before 53d5cfc3b8 the tree size was truncated to 32 bits as well, so
the walk saw the 2^32 + 26 byte tree as 26 bytes.  That is exactly
what the wrapped entry length consumed, so the walk stopped without an
error and read-cache rejected the path as the tests expect.  Fixing
the tree size exposed the pathlen bug.

Fix this by keeping the name length in a size_t and rejecting names
longer than INT_MAX.  read-cache already has the same limit for index
entries, and index.maxPathLen cannot be set higher, so such names
could never be used anyway.  Update the tests to expect the new error.

Signed-off-by: Kristofer Karlsson <krka@spotify.com>
---
    tree-walk: reject tree entries whose name length overflows an int
    
    I ran into these t1600 failures in CI while working on something else.
    They started when ep/tree-walk-size-t was merged in "The 26th batch",
    and I could not find anyone working on a fix on the list, so here is
    one.
    
    Harald independently arrived at the same fix at the same time [1], but
    closed his PR in favor of this one.
    
    [1] https://github.com/git/git/pull/2512

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2342%2Fspkrka%2Ft1600-overlong-tree-fix-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2342/spkrka/t1600-overlong-tree-fix-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2342

 t/t1600-index.sh | 4 ++--
 tree-walk.c      | 7 ++++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/t/t1600-index.sh b/t/t1600-index.sh
index dc4d68c1fb..b94e643e25 100755
--- a/t/t1600-index.sh
+++ b/t/t1600-index.sh
@@ -184,8 +184,8 @@ test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,LONG_IS_64BIT \
 		git mktree >tree
 	) &&
 	overlong_tree=$(cat overlong/tree) &&
-	echo "fatal: index entry path is too long" \
-		"(4294967298 bytes)" >expect-overlong
+	echo "fatal: too long filename in tree entry" \
+		"(4294967294 bytes)" >expect-overlong
 '
 
 test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,LONG_IS_64BIT \
diff --git a/tree-walk.c b/tree-walk.c
index aaac1fd48d..f4b4d3fed7 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -17,7 +17,7 @@
 static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)
 {
 	const char *path;
-	unsigned int len;
+	size_t len;
 	uint16_t mode;
 	const unsigned hashsz = desc->algo->rawsz;
 
@@ -36,6 +36,11 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t siz
 		return -1;
 	}
 	len = strlen(path) + 1;
+	if (len - 1 > INT_MAX) {
+		strbuf_addf(err, _("too long filename in tree entry (%"PRIuMAX" bytes)"),
+			    (uintmax_t)(len - 1));
+		return -1;
+	}
 
 	/* Initialize the descriptor entry */
 	desc->entry.path = path;

base-commit: 86e79b2795e3ce3b0ad7685edd368574b0c8b9f2
-- 
gitgitgadget

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

* Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int
  2026-11-30 14:42 [PATCH] tree-walk: reject tree entries whose name length overflows an int Kristofer Karlsson via GitGitGadget
@ 2026-12-01 15:11 ` Patrick Steinhardt
  2026-12-01 15:58   ` Kristofer Karlsson
  0 siblings, 1 reply; 4+ messages in thread
From: Patrick Steinhardt @ 2026-12-01 15:11 UTC (permalink / raw)
  To: Kristofer Karlsson via GitGitGadget
  Cc: git, Elia Pinto, Elijah Newren, Johannes Schindelin,
	Harald Nordgren, Kristofer Karlsson

On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:
> From: Kristofer Karlsson <krka@spotify.com>
> 
> The EXPENSIVE tests 13-15 in t1600 have been failing in CI on all
> 64-bit platforms since 53d5cfc3b8 (tree-walk: use size_t for the size
> of tree buffers, 2026-10-30) was merged:
> 
>   -fatal: index entry path is too long (4294967298 bytes)
>   +fatal: malformed mode in tree entry
> 
> The tests were added in 89048b48fb (t1600-index: add expensive tests
> for over-long tree paths, 2026-10-16).  They use a tree with a single
> entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores
> that length in the "int" pathlen of struct name_entry, where it wraps
> to -2.  update_tree_entry() then uses it to find the next entry, ends
> up in the middle of the name, and fails to parse it.

Things like this in central data structures make me wonder whether we
should be explicit with types. That is, do we really want to have a
different maximum tree entry length depending on the platform that
you're on?

I myself don't think that's sensible, so I wonder whether we should
convert this to e.g. an `uint32_t` (or an `int32_t` in case we need
negative values). But that's of course outside of the scope of this
patch series.

> diff --git a/tree-walk.c b/tree-walk.c
> index aaac1fd48d..f4b4d3fed7 100644
> --- a/tree-walk.c
> +++ b/tree-walk.c
> @@ -17,7 +17,7 @@
>  static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)
>  {
>  	const char *path;
> -	unsigned int len;
> +	size_t len;
>  	uint16_t mode;
>  	const unsigned hashsz = desc->algo->rawsz;
>  
> @@ -36,6 +36,11 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t siz
>  		return -1;
>  	}
>  	len = strlen(path) + 1;

Makes sense to change the type of `len`, as we assign the result of
`strlen()` to it.

> +	if (len - 1 > INT_MAX) {
> +		strbuf_addf(err, _("too long filename in tree entry (%"PRIuMAX" bytes)"),
> +			    (uintmax_t)(len - 1));
> +		return -1;
> +	}

So restricting the length makes sense to me. But as mentioned above, do
we maybe want to check against `INT32_MAX`? I don't think we support any
platforms (or whether those even exist) with where int is 16 bits, but I
think it does make a ton of sense to behave the same on all platforms.

We can then still adapt `struct name_entry::len` at a later point in
time.

Ideally, we'd also have documentation around this somewhere to spell out
that tree entries may at most be INT32_MAX.

Thanks!

Patrick

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

* Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int
  2026-12-01 15:11 ` Patrick Steinhardt
@ 2026-12-01 15:58   ` Kristofer Karlsson
  2026-12-02  7:27     ` Patrick Steinhardt
  0 siblings, 1 reply; 4+ messages in thread
From: Kristofer Karlsson @ 2026-12-01 15:58 UTC (permalink / raw)
  To: Patrick Steinhardt
  Cc: Kristofer Karlsson via GitGitGadget, git, Elia Pinto,
	Elijah Newren, Johannes Schindelin, Harald Nordgren

On Tue, 1 Dec 2026 at 16:11, Patrick Steinhardt <ps@pks.im> wrote:
>
> On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:
> > From: Kristofer Karlsson <krka@spotify.com>
> >
> > The tests were added in 89048b48fb (t1600-index: add expensive tests
> > for over-long tree paths, 2026-10-16).  They use a tree with a single
> > entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores
> > that length in the "int" pathlen of struct name_entry, where it wraps
> > to -2.  update_tree_entry() then uses it to find the next entry, ends
> > up in the middle of the name, and fails to parse it.
>
> Things like this in central data structures make me wonder whether we
> should be explicit with types. That is, do we really want to have a
> different maximum tree entry length depending on the platform that
> you're on?
>
> I myself don't think that's sensible, so I wonder whether we should
> convert this to e.g. an `uint32_t` (or an `int32_t` in case we need
> negative values). But that's of course outside of the scope of this
> patch series.

Yes, that's a bit awkward.  Ideally we should rework the code to
have the appropriate integer types.  I started exploring that path
and changing the struct too, but the changes spread quickly and
I wanted to focus on making the CI green.  Longer term I think
the type migration is still ongoing and would eventually address
this too.

My first thought was size_t, but paths are never that long in
practice, so a 32-bit type matching real filesystem limits seems
like the better choice.

>
> > diff --git a/tree-walk.c b/tree-walk.c
> > index aaac1fd48d..f4b4d3fed7 100644
> > [...]
> > -     unsigned int len;
> > +     size_t len;
> > [...]
>
> Makes sense to change the type of `len`, as we assign the result of
> `strlen()` to it.
>
> > +     if (len - 1 > INT_MAX) {
> > +             strbuf_addf(err, _("too long filename in tree entry (%"PRIuMAX" bytes)"),
> > +                         (uintmax_t)(len - 1));
> > +             return -1;
> > +     }
>
> So restricting the length makes sense to me. But as mentioned above, do
> we maybe want to check against `INT32_MAX`? I don't think we support any
> platforms (or whether those even exist) with where int is 16 bits, but I
> think it does make a ton of sense to behave the same on all platforms.

First I was thinking that the limit should match the declared
field, which is "int" in this case.  But since INT32_MAX is never
larger than INT_MAX on any supported platform, it is both a safe
limit and gives the same behavior everywhere.

I will change to INT32_MAX for a v2 reroll.

>
> We can then still adapt `struct name_entry::len` at a later point in
> time.
>
> Ideally, we'd also have documentation around this somewhere to spell out
> that tree entries may at most be INT32_MAX.

I am not sure it is worth it.  The filesystem limits of paths
and/or names are much lower than 2^31 anyway so this limit
is unlikely to hit (or even impossible?) for any real users.

I also could not really find any suitable document to put it
in, and I think the other limits that are documented are ones
that are more practically reachable.  One exception may be
index.maxPathLen in config/index.adoc, which mentions the same
limit, but that is documented because it is a config option
and thus needs to say which values are accepted, so it is
not fully comparable.

Thanks for taking a look!
Kristofer

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

* Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int
  2026-12-01 15:58   ` Kristofer Karlsson
@ 2026-12-02  7:27     ` Patrick Steinhardt
  0 siblings, 0 replies; 4+ messages in thread
From: Patrick Steinhardt @ 2026-12-02  7:27 UTC (permalink / raw)
  To: Kristofer Karlsson
  Cc: Kristofer Karlsson via GitGitGadget, git, Elia Pinto,
	Elijah Newren, Johannes Schindelin, Harald Nordgren

On Tue, Dec 01, 2026 at 04:58:56PM +0100, Kristofer Karlsson wrote:
> On Tue, 1 Dec 2026 at 16:11, Patrick Steinhardt <ps@pks.im> wrote:
> > On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:
> > > From: Kristofer Karlsson <krka@spotify.com>
> > We can then still adapt `struct name_entry::len` at a later point in
> > time.
> >
> > Ideally, we'd also have documentation around this somewhere to spell out
> > that tree entries may at most be INT32_MAX.
> 
> I am not sure it is worth it.  The filesystem limits of paths
> and/or names are much lower than 2^31 anyway so this limit
> is unlikely to hit (or even impossible?) for any real users.

True, but it's still possible to generate such tree entries regardless
of that, for example via git-mktree(1) or git-hash-object(1). You
wouldn't ever be able to check those out, but no matter which limit we
pick there will always be some platforms that won't be able to do that.

> I also could not really find any suitable document to put it
> in, and I think the other limits that are documented are ones
> that are more practically reachable.  One exception may be
> index.maxPathLen in config/index.adoc, which mentions the same
> limit, but that is documented because it is a config option
> and thus needs to say which values are accepted, so it is
> not fully comparable.

I think the best place to add that to would be gitdatamodel(7). We
already document the object formats in there, so it might be a natural
place to add this limitation. E.g.:

diff --git a/Documentation/gitdatamodel.adoc b/Documentation/gitdatamodel.adoc
index 056b866f254..53927622987 100644
--- a/Documentation/gitdatamodel.adoc
+++ b/Documentation/gitdatamodel.adoc
@@ -85,7 +85,7 @@ tree::
     It can contain files or other trees (which are subdirectories).
     It lists, for each item in the tree:
 +
-1. The *filename*, for example `hello.py`
+1. The *filename*, for example `hello.py`, which may not be longer than XYZ bytes
 2. The *file type*, which must be one of these five types:
   - *regular file*
   - *executable file*

Thanks!

Patrick

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

end of thread, other threads:[~2026-12-02  7:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 14:42 [PATCH] tree-walk: reject tree entries whose name length overflows an int Kristofer Karlsson via GitGitGadget
2026-12-01 15:11 ` Patrick Steinhardt
2026-12-01 15:58   ` Kristofer Karlsson
2026-12-02  7:27     ` Patrick Steinhardt

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