threads / patch / 66970

patchtree-walk: preserve large entry path lengths

Subject: [PATCH] tree-walk: preserve large entry path lengths

## tl;dr

After a tree-buffer change stopped hiding a wrapped length, the expensive t1600 tests failed; the author of the competing patch agreed to keep the long names and widen the type instead. Read the story.

replies: 4people: 3as markdown or json

Alessio Attilio via GitGitGadget· Dec 1, 2026, 18:49 UTC · lore
From: Alessio Attilio <attilio.alessio@protonmail.com>

The tree buffer uses size_t, but decoded entry lengths were still stored in unsigned int and int. A large path could therefore be truncated before tree walkers reached it.

Keep the decoded length as size_t so walkers can reject oversized index paths correctly.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
    tree-walk: preserve large entry path lengths
    
    Tree entry path lengths are decoded into narrower integer types before
    the tree buffer has finished parsing them. Keep the decoded length as
    size_t so oversized paths are not truncated before tree walkers can
    reject them.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2514%2Fkairosci%2Ftopic%2Ftree-walk-pathlen-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2514/kairosci/topic/tree-walk-pathlen-v1
Pull-Request: https://github.com/git/git/pull/2514
 fsck.c      | 3 ++-
 tree-walk.c | 2 +-
 tree-walk.h | 4 ++--
 3 files changed, 5 insertions(+), 4 deletions(-)
Show changes to 3 files +5 −4

fsck.c, tree-walk.c, tree-walk.h

diff --git a/fsck.c b/fsck.c
index 94c8651c7d..9dbf7b6b0d 100644
--- a/fsck.c
+++ b/fsck.c
@@ -659,7 +659,8 @@ static int fsck_tree(const struct object_id *tree_oid,
 		has_dotdot |= !strcmp(name, "..");
 		has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
 		has_zero_pad |= *(char *)desc.buffer == '0';
-		has_large_name |= tree_entry_len(&desc.entry) > max_tree_entry_len;
+		has_large_name |= max_tree_entry_len < 0 ||
+			tree_entry_len(&desc.entry) > (size_t)max_tree_entry_len;
 
 		if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
 			if (!S_ISLNK(mode))
diff --git a/tree-walk.c b/tree-walk.c
index aaac1fd48d..e2b654232e 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;
 
diff --git a/tree-walk.h b/tree-walk.h
index 5ccd4a9361..7c154e7ace 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -16,7 +16,7 @@ struct repository;
 struct name_entry {
 	struct object_id oid;
 	const char *path;
-	int pathlen;
+	size_t pathlen;
 	unsigned int mode;
 };
 
@@ -61,7 +61,7 @@ static inline const struct object_id *tree_entry_extract(struct tree_desc *desc,
  * memory structure of a tree entry to avoid the overhead of using a
  * generic strlen().
  */
-static inline int tree_entry_len(const struct name_entry *ne)
+static inline size_t tree_entry_len(const struct name_entry *ne)
 {
 	return ne->pathlen;
 }

base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
-- 
gitgitgadget
Kristofer Karlsson· Dec 2, 2026, 13:43 UTC · re: Alessio Attilio via GitGitGadget · lore

Re: [PATCH] tree-walk: preserve large entry path lengths

On Tue, 1 Dec 2026 at 19:49, Alessio Attilio via GitGitGadget <gitgitgadget@gmail.com> wrote:

Show 6 quoted lines
>
> From: Alessio Attilio <attilio.alessio@protonmail.com>
>
> The tree buffer uses size_t, but decoded entry lengths were still
> stored in unsigned int and int. A large path could therefore be
> truncated before tree walkers reached it.

This fixes the same CI failure in t1600 that I sent a patch for yesterday [1], but from the other direction: you keep the long name, while mine rejects names longer than INT32_MAX in decode_tree_entry().

The commit message could say more about how this broke. The t1600 tests started failing when 53d5cfc3b8 (tree-walk: use size_t for the size of tree buffers, 2026-10-30) stopped truncating the tree size, which used to hide the wrapped pathlen. Feel free to reuse parts of the explanation from my commit message if needed.

> Keep the decoded length as size_t so walkers can reject oversized
> index paths correctly.

I applied your patch and ran t1600 with --long, and tests 13-15 pass again without any test changes. t1450 also passes.

Show 5 quoted lines
> diff --git a/fsck.c b/fsck.c
> [...]
> -               has_large_name |= tree_entry_len(&desc.entry) > max_tree_entry_len;
> +               has_large_name |= max_tree_entry_len < 0 ||
> +                       tree_entry_len(&desc.entry) > (size_t)max_tree_entry_len;

Instead of the extra "< 0" check, how about turning max_tree_entry_len into a size_t and parsing it with git_parse_size_t()? It was an ssize_t only because tree_entry_len() used to return an int, and a negative limit does not make much sense anyway.

[snip]
> diff --git a/tree-walk.h b/tree-walk.h
> [...]
> -       int pathlen;
> +       size_t pathlen;

From reading the callers, most of the code that consumes pathlen already takes a size_t, so this looks correct to me. One exception I noticed is "int te_len = tree_entry_len(&entry)" in builtin/grep.c, which still narrows it to an int.

Patrick, thinking about this more, I believe this is a better fix than mine since it can avoid adding a new limit on path lengths. Platforms that cannot handle such a tree still fail, just earlier, when loading it.

This also matches how 0fbcaef6b4 (fsck: detect very large tree pathnames, 2023-08-31) dealt with very long names: instead of a hard limit when parsing trees, it added a configurable fsck check, which transfer.fsckObjects can use to keep such trees out.

For the documentation, rather than a hard maximum in gitdatamodel(7), it may then be better to say that names longer than 4096 bytes are not portable, which is what fsck.largePathname already warns about.

If you agree, I will drop my patch in favor of this one.
[1] https://lore.kernel.org/git/pull.2342.git.1796049772272.gitgitgadget@gmail.com/
ATTILIO ALESSIO· Dec 2, 2026, 14:22 UTC · re: Kristofer Karlsson · lore

Re: [PATCH] tree-walk: preserve large entry path lengths

Hi, Kristofer!
I agree this is the better approach. 
I’ve updated the patch to parse max_tree_entry_len as a size_t, fix the narrowing in builtin/grep.c, expand the commit message with the 53d5cfc3b8 context, and document that names longer than 4096 bytes are not portable.
I’ll reroll the series shortly. 
Please drop your patch in favor of this one.

Best, Alessio

Alessio Attilio via GitGitGadget· Dec 2, 2026, 14:25 UTC · re: Alessio Attilio via GitGitGadget · lore

[PATCH v2] tree-walk: preserve large entry path lengths

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.

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.

Keep the decoded length as size_t throughout tree-walk, including its callers, so the walkers can correctly handle or reject large paths. Document that names longer than 4096 bytes are not portable, while leaving rejection of such trees to the configurable fsck check.

Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
---
    tree-walk: preserve large entry path lengths
    
    Tree entry path lengths are decoded into narrower integer types before
    the tree buffer has finished parsing them. Keep the decoded length as
    size_t so oversized paths are not truncated before tree walkers can
    reject them.
    
    Changes since v1:
    
     * Explain that 53d5cfc3b8 (tree-walk: use size_t for the size of tree
       buffers, 2026-10-30) stopped truncating the tree size, exposing the
       wrapped path length and causing t1600.13-15 to fail with --long.
     * Parse the fsck.largePathname limit as size_t with git_parse_size_t().
     * Preserve the entry length as size_t in builtin/grep.c.
     * Document that names longer than 4096 bytes are not portable.
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2514%2Fkairosci%2Ftopic%2Ftree-walk-pathlen-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2514/kairosci/topic/tree-walk-pathlen-v2
Pull-Request: https://github.com/git/git/pull/2514
Range-diff vs v1:
 1:  6df3340782 ! 1:  f59137c9c8 tree-walk: preserve large entry path lengths
     @@ Metadata
       ## Commit message ##
          tree-walk: preserve large entry path lengths
      
     -    The tree buffer uses size_t, but decoded entry lengths were still
     -    stored in unsigned int and int. A large path could therefore be
     -    truncated before tree walkers reached it.
     +    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.
      
     -    Keep the decoded length as size_t so walkers can reject oversized
     -    index paths correctly.
     +    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.
     +
     +    Keep the decoded length as size_t throughout tree-walk, including its
     +    callers, so the walkers can correctly handle or reject large paths.
     +    Document that names longer than 4096 bytes are not portable, while
     +    leaving rejection of such trees to the configurable fsck check.
      
          Signed-off-by: Alessio Attilio <attilio.alessio@protonmail.com>
      
     + ## Documentation/gitdatamodel.adoc ##
     +@@ Documentation/gitdatamodel.adoc: 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`. Names longer than 4096 bytes
     ++   are not portable across all systems.
     + 2. The *file type*, which must be one of these five types:
     +   - *regular file*
     +   - *executable file*
     +
     + ## builtin/grep.c ##
     +@@ builtin/grep.c: static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
     + 	}
     + 
     + 	while (tree_entry(tree, &entry)) {
     +-		int te_len = tree_entry_len(&entry);
     ++		size_t te_len = tree_entry_len(&entry);
     + 
     + 		if (match != all_entries_interesting) {
     + 			strbuf_addstr(&name, base->buf + tn_len);
     +
       ## fsck.c ##
     -@@ fsck.c: static int fsck_tree(const struct object_id *tree_oid,
     - 		has_dotdot |= !strcmp(name, "..");
     - 		has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
     - 		has_zero_pad |= *(char *)desc.buffer == '0';
     --		has_large_name |= tree_entry_len(&desc.entry) > max_tree_entry_len;
     -+		has_large_name |= max_tree_entry_len < 0 ||
     -+			tree_entry_len(&desc.entry) > (size_t)max_tree_entry_len;
     +@@
     + #include "config.h"
     + #include "help.h"
     + 
     +-static ssize_t max_tree_entry_len = 4096;
     ++static size_t max_tree_entry_len = 4096;
       
     - 		if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
     - 			if (!S_ISLNK(mode))
     + #define STR(x) #x
     + #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
     +@@ fsck.c: void fsck_set_msg_type(struct fsck_options *options,
     + 			msg_type_str = to_free =
     + 				xmemdupz(msg_type_str, colon - msg_type_str);
     + 			colon++;
     +-			if (!git_parse_ssize_t(colon, &max_tree_entry_len))
     ++			if (!git_parse_size_t(colon, &max_tree_entry_len))
     + 				die("unable to parse max tree entry len: %s", colon);
     + 		}
     + 	}
      
       ## tree-walk.c ##
      @@
 Documentation/gitdatamodel.adoc | 3 ++-
 builtin/grep.c                  | 2 +-
 fsck.c                          | 4 ++--
 tree-walk.c                     | 2 +-
 tree-walk.h                     | 4 ++--
 5 files changed, 8 insertions(+), 7 deletions(-)
Show changes to 5 files +8 −7

Documentation/gitdatamodel.adoc, builtin/grep.c, fsck.c, tree-walk.c, tree-walk.h

diff --git a/Documentation/gitdatamodel.adoc b/Documentation/gitdatamodel.adoc
index 056b866f25..f10617d729 100644
--- a/Documentation/gitdatamodel.adoc
+++ b/Documentation/gitdatamodel.adoc
@@ -85,7 +85,8 @@ 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`. Names longer than 4096 bytes
+   are not portable across all systems.
 2. The *file type*, which must be one of these five types:
   - *regular file*
   - *executable file*
diff --git a/builtin/grep.c b/builtin/grep.c
index 22f0a1b0d0..c056312f06 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -627,7 +627,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 	}
 
 	while (tree_entry(tree, &entry)) {
-		int te_len = tree_entry_len(&entry);
+		size_t te_len = tree_entry_len(&entry);
 
 		if (match != all_entries_interesting) {
 			strbuf_addstr(&name, base->buf + tn_len);
diff --git a/fsck.c b/fsck.c
index 94c8651c7d..8f68112b6d 100644
--- a/fsck.c
+++ b/fsck.c
@@ -23,7 +23,7 @@
 #include "config.h"
 #include "help.h"
 
-static ssize_t max_tree_entry_len = 4096;
+static size_t max_tree_entry_len = 4096;
 
 #define STR(x) #x
 #define MSG_ID(id, msg_type) { STR(id), NULL, NULL, FSCK_##msg_type },
@@ -167,7 +167,7 @@ void fsck_set_msg_type(struct fsck_options *options,
 			msg_type_str = to_free =
 				xmemdupz(msg_type_str, colon - msg_type_str);
 			colon++;
-			if (!git_parse_ssize_t(colon, &max_tree_entry_len))
+			if (!git_parse_size_t(colon, &max_tree_entry_len))
 				die("unable to parse max tree entry len: %s", colon);
 		}
 	}
diff --git a/tree-walk.c b/tree-walk.c
index aaac1fd48d..e2b654232e 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;
 
diff --git a/tree-walk.h b/tree-walk.h
index 5ccd4a9361..7c154e7ace 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -16,7 +16,7 @@ struct repository;
 struct name_entry {
 	struct object_id oid;
 	const char *path;
-	int pathlen;
+	size_t pathlen;
 	unsigned int mode;
 };
 
@@ -61,7 +61,7 @@ static inline const struct object_id *tree_entry_extract(struct tree_desc *desc,
  * memory structure of a tree entry to avoid the overhead of using a
  * generic strlen().
  */
-static inline int tree_entry_len(const struct name_entry *ne)
+static inline size_t tree_entry_len(const struct name_entry *ne)
 {
 	return ne->pathlen;
 }

base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
-- 
gitgitgadget
Kristofer Karlsson· Dec 2, 2026, 15:59 UTC · re: Alessio Attilio via GitGitGadget · lore

Re: [PATCH v2] tree-walk: preserve large entry path lengths

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:

Show changes to 2 files +2 −2

tree-diff.c, tree.c

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

← back to recent threads