Re: [PATCH v2] tree-walk: preserve large entry path lengths
- From
Kristofer Karlsson <krka@spotify.com>
- Date
- Dec 2, 2026, 15:59 UTC
- Message-ID
- <CAL71e4M7jPzQhJQFr=PbHQGznW=Ba6YRDohJogPmCQbTBfxw5g@mail.gmail.com>
- In-Reply-To
- <pull.2514.v2.git.git.1796221514213.gitgitgadget@gmail.com>
On Wed, 2 Dec 2026 at 15:25, Alessio Attilio via GitGitGadget <gitgitgadget@gmail.com> wrote:
Show 6 quoted lines
> > From: Alessio Attilio <attilio.alessio@protonmail.com> > > Tree entry path lengths were stored in unsigned int and int even > though tree buffers are sized with size_t. This truncated paths larger > than those types can represent.
Nit: the path itself was not truncated, but its length wrapped (to -2 in the test).
Show 5 quoted lines
> This became visible after 53d5cfc3b8 (tree-walk: use size_t for the > size of tree buffers, 2026-10-30) stopped truncating the tree size: > walkers could reach the full entry, but its path length had already > wrapped. In particular, index path length checks then saw the wrong > length, causing t1600 tests 13-15 to fail with --long.
The tests fail before reaching the index: update_tree_entry() uses the wrapped length to find the next entry, ends up in the middle of the name, and dies with "malformed mode in tree entry". The index check is what makes the tests pass again with your fix.
> Keep the decoded length as size_t throughout tree-walk, including its > callers, so the walkers can correctly handle or reject large paths.
There are two callers left that narrow it to an int: read_tree_at() in tree.c and emit_path() in tree-diff.c. I tried ls-tree and diff-tree on a tree with a 2^32 - 2 byte name with this patch:
huge file name: ls-tree -r ok huge file name: diff-tree -r fatal: size_t overflow: 4 + 18446744073709551614 huge dir name: ls-tree -r fatal: size_t overflow: 4 + 18446744073709551614 huge dir name: diff-tree -r fatal: size_t overflow: 4 + 18446744073709551614
So it dies cleanly rather than misbehaving, but it would be nice to fix these too. Both lengths are only passed to strbuf_add(), so it does not spread any further. With the following on top, all four commands succeed, and t1600 (with --long), t1450, t3100, t3103, t4013, t4017, t5000 and t7810 pass:
diff --git a/tree-diff.c b/tree-diff.c --- a/tree-diff.c +++ b/tree-diff.c @@ -208,7 +208,7 @@ static void emit_path(struct combine_diff_path ***tail, unsigned short mode; const char *path; const struct object_id *oid; - int pathlen; + size_t pathlen; int old_baselen = base->len; int i, isdir, recurse = 0, emitthis = 1; diff --git a/tree.c b/tree.c --- a/tree.c +++ b/tree.c @@ -20,7 +20,7 @@ int read_tree_at(struct repository *r, struct tree_desc desc; struct name_entry entry; struct object_id oid; - int len, oldlen = base->len; + size_t len, oldlen = base->len; enum interesting retval = entry_not_interesting; if (depth > r->settings.max_allowed_tree_depth) There are more ints further down, like old_baselen above, but those hold the length of the leading directories rather than of the entry itself, so I think they are better left to a separate series. Please also wait a bit before sending a new version, so that others have time to weigh in. SubmittingPatches puts it as "allow some time for reviewers to provide feedback before sending a new version, rather than sending an updated series immediately after receiving a review". Thanks, Kristofer