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

Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int

From
Kristofer Karlsson <krka@spotify.com>
Date
Dec 1, 2026, 15:58 UTC
Message-ID
<CAL71e4NovJixz3k3yjDyShGtmgLeL5J7Kog0E=a1C=u-CuWr9A@mail.gmail.com>
In-Reply-To
<aw7jpRvNW53QBXtF@pks.im>
On Tue, 1 Dec 2026 at 16:11, Patrick Steinhardt <ps@pks.im> wrote:
Show 20 quoted lines
>
> 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.

Show 21 quoted lines
>
> > 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.
Show 6 quoted lines
>
> 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

Previous: Patrick Steinhardt
Message 3 of 3 in “tree-walk: reject tree entries whose name length overflows an int”
  1. tree-walk: reject tree entries whose name length overflows an intKristofer Karlsson via GitGitGadget, Nov 30, 2026
  2. Patrick SteinhardtDec 1, 2026
  3. Kristofer KarlssonDec 1, 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.