{"thread":{"id":"66959","subject":"[PATCH] tree-walk: reject tree entries whose name length overflows an int","startedAt":"2026-11-30T14:42:52Z","lastAt":"2026-12-01T15:58:56Z","messageCount":3,"participants":["Kristofer Karlsson via GitGitGadget","Patrick Steinhardt","Kristofer Karlsson"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559698","messageId":"pull.2342.git.1796049772272.gitgitgadget@gmail.com","threadId":"66959","inReplyTo":null,"subject":"[PATCH] tree-walk: reject tree entries whose name length overflows an int","fromName":"Kristofer Karlsson via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-30T14:42:52Z","receivedAt":"2026-11-30T14:42:52Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"From: Kristofer Karlsson <krka@spotify.com>\n\nThe EXPENSIVE tests 13-15 in t1600 have been failing in CI on all\n64-bit platforms since 53d5cfc3b8 (tree-walk: use size_t for the size\nof tree buffers, 2026-10-30) was merged:\n\n  -fatal: index entry path is too long (4294967298 bytes)\n  +fatal: malformed mode in tree entry\n\nThe tests were added in 89048b48fb (t1600-index: add expensive tests\nfor over-long tree paths, 2026-10-16).  They use a tree with a single\nentry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores\nthat length in the \"int\" pathlen of struct name_entry, where it wraps\nto -2.  update_tree_entry() then uses it to find the next entry, ends\nup in the middle of the name, and fails to parse it.\n\nBefore 53d5cfc3b8 the tree size was truncated to 32 bits as well, so\nthe walk saw the 2^32 + 26 byte tree as 26 bytes.  That is exactly\nwhat the wrapped entry length consumed, so the walk stopped without an\nerror and read-cache rejected the path as the tests expect.  Fixing\nthe tree size exposed the pathlen bug.\n\nFix this by keeping the name length in a size_t and rejecting names\nlonger than INT_MAX.  read-cache already has the same limit for index\nentries, and index.maxPathLen cannot be set higher, so such names\ncould never be used anyway.  Update the tests to expect the new error.\n\nSigned-off-by: Kristofer Karlsson <krka@spotify.com>\n---\n    tree-walk: reject tree entries whose name length overflows an int\n    \n    I ran into these t1600 failures in CI while working on something else.\n    They started when ep/tree-walk-size-t was merged in \"The 26th batch\",\n    and I could not find anyone working on a fix on the list, so here is\n    one.\n    \n    Harald independently arrived at the same fix at the same time [1], but\n    closed his PR in favor of this one.\n    \n    [1] https://github.com/git/git/pull/2512\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2342%2Fspkrka%2Ft1600-overlong-tree-fix-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2342/spkrka/t1600-overlong-tree-fix-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2342\n\n t/t1600-index.sh | 4 ++--\n tree-walk.c      | 7 ++++++-\n 2 files changed, 8 insertions(+), 3 deletions(-)\n\ndiff --git a/t/t1600-index.sh b/t/t1600-index.sh\nindex dc4d68c1fb..b94e643e25 100755\n--- a/t/t1600-index.sh\n+++ b/t/t1600-index.sh\n@@ -184,8 +184,8 @@ test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,LONG_IS_64BIT \\\n \t\tgit mktree >tree\n \t) &&\n \toverlong_tree=$(cat overlong/tree) &&\n-\techo \"fatal: index entry path is too long\" \\\n-\t\t\"(4294967298 bytes)\" >expect-overlong\n+\techo \"fatal: too long filename in tree entry\" \\\n+\t\t\"(4294967294 bytes)\" >expect-overlong\n '\n \n test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,LONG_IS_64BIT \\\ndiff --git a/tree-walk.c b/tree-walk.c\nindex aaac1fd48d..f4b4d3fed7 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -17,7 +17,7 @@\n static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)\n {\n \tconst char *path;\n-\tunsigned int len;\n+\tsize_t len;\n \tuint16_t mode;\n \tconst unsigned hashsz = desc->algo->rawsz;\n \n@@ -36,6 +36,11 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t siz\n \t\treturn -1;\n \t}\n \tlen = strlen(path) + 1;\n+\tif (len - 1 > INT_MAX) {\n+\t\tstrbuf_addf(err, _(\"too long filename in tree entry (%\"PRIuMAX\" bytes)\"),\n+\t\t\t    (uintmax_t)(len - 1));\n+\t\treturn -1;\n+\t}\n \n \t/* Initialize the descriptor entry */\n \tdesc->entry.path = path;\n\nbase-commit: 86e79b2795e3ce3b0ad7685edd368574b0c8b9f2\n-- \ngitgitgadget\n\n"},{"id":"559801","messageId":"aw7jpRvNW53QBXtF@pks.im","threadId":"66959","inReplyTo":"pull.2342.git.1796049772272.gitgitgadget@gmail.com","subject":"Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-12-01T15:11:33Z","receivedAt":"2026-12-01T15:11:33Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:\n> From: Kristofer Karlsson <krka@spotify.com>\n> \n> The EXPENSIVE tests 13-15 in t1600 have been failing in CI on all\n> 64-bit platforms since 53d5cfc3b8 (tree-walk: use size_t for the size\n> of tree buffers, 2026-10-30) was merged:\n> \n>   -fatal: index entry path is too long (4294967298 bytes)\n>   +fatal: malformed mode in tree entry\n> \n> The tests were added in 89048b48fb (t1600-index: add expensive tests\n> for over-long tree paths, 2026-10-16).  They use a tree with a single\n> entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores\n> that length in the \"int\" pathlen of struct name_entry, where it wraps\n> to -2.  update_tree_entry() then uses it to find the next entry, ends\n> up in the middle of the name, and fails to parse it.\n\nThings like this in central data structures make me wonder whether we\nshould be explicit with types. That is, do we really want to have a\ndifferent maximum tree entry length depending on the platform that\nyou're on?\n\nI myself don't think that's sensible, so I wonder whether we should\nconvert this to e.g. an `uint32_t` (or an `int32_t` in case we need\nnegative values). But that's of course outside of the scope of this\npatch series.\n\n> diff --git a/tree-walk.c b/tree-walk.c\n> index aaac1fd48d..f4b4d3fed7 100644\n> --- a/tree-walk.c\n> +++ b/tree-walk.c\n> @@ -17,7 +17,7 @@\n>  static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)\n>  {\n>  \tconst char *path;\n> -\tunsigned int len;\n> +\tsize_t len;\n>  \tuint16_t mode;\n>  \tconst unsigned hashsz = desc->algo->rawsz;\n>  \n> @@ -36,6 +36,11 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t siz\n>  \t\treturn -1;\n>  \t}\n>  \tlen = strlen(path) + 1;\n\nMakes sense to change the type of `len`, as we assign the result of\n`strlen()` to it.\n\n> +\tif (len - 1 > INT_MAX) {\n> +\t\tstrbuf_addf(err, _(\"too long filename in tree entry (%\"PRIuMAX\" bytes)\"),\n> +\t\t\t    (uintmax_t)(len - 1));\n> +\t\treturn -1;\n> +\t}\n\nSo restricting the length makes sense to me. But as mentioned above, do\nwe maybe want to check against `INT32_MAX`? I don't think we support any\nplatforms (or whether those even exist) with where int is 16 bits, but I\nthink it does make a ton of sense to behave the same on all platforms.\n\nWe can then still adapt `struct name_entry::len` at a later point in\ntime.\n\nIdeally, we'd also have documentation around this somewhere to spell out\nthat tree entries may at most be INT32_MAX.\n\nThanks!\n\nPatrick\n\n"},{"id":"559810","messageId":"CAL71e4NovJixz3k3yjDyShGtmgLeL5J7Kog0E=a1C=u-CuWr9A@mail.gmail.com","threadId":"66959","inReplyTo":"aw7jpRvNW53QBXtF@pks.im","subject":"Re: [PATCH] tree-walk: reject tree entries whose name length overflows an int","fromName":"Kristofer Karlsson","fromEmail":"krka@spotify.com","sentAt":"2026-12-01T15:58:56Z","receivedAt":"2026-12-01T15:58:56Z","isPatch":true,"sender":{"key":"krka@spotify.com","avatar":"https://avatars.githubusercontent.com/u/5349266?v=4"},"body":"On Tue, 1 Dec 2026 at 16:11, Patrick Steinhardt <ps@pks.im> wrote:\n>\n> On Mon, Nov 30, 2026 at 02:42:52PM +0000, Kristofer Karlsson via GitGitGadget wrote:\n> > From: Kristofer Karlsson <krka@spotify.com>\n> >\n> > The tests were added in 89048b48fb (t1600-index: add expensive tests\n> > for over-long tree paths, 2026-10-16).  They use a tree with a single\n> > entry whose name is 2^32 - 2 bytes long.  decode_tree_entry() stores\n> > that length in the \"int\" pathlen of struct name_entry, where it wraps\n> > to -2.  update_tree_entry() then uses it to find the next entry, ends\n> > up in the middle of the name, and fails to parse it.\n>\n> Things like this in central data structures make me wonder whether we\n> should be explicit with types. That is, do we really want to have a\n> different maximum tree entry length depending on the platform that\n> you're on?\n>\n> I myself don't think that's sensible, so I wonder whether we should\n> convert this to e.g. an `uint32_t` (or an `int32_t` in case we need\n> negative values). But that's of course outside of the scope of this\n> patch series.\n\nYes, that's a bit awkward.  Ideally we should rework the code to\nhave the appropriate integer types.  I started exploring that path\nand changing the struct too, but the changes spread quickly and\nI wanted to focus on making the CI green.  Longer term I think\nthe type migration is still ongoing and would eventually address\nthis too.\n\nMy first thought was size_t, but paths are never that long in\npractice, so a 32-bit type matching real filesystem limits seems\nlike the better choice.\n\n>\n> > diff --git a/tree-walk.c b/tree-walk.c\n> > index aaac1fd48d..f4b4d3fed7 100644\n> > [...]\n> > -     unsigned int len;\n> > +     size_t len;\n> > [...]\n>\n> Makes sense to change the type of `len`, as we assign the result of\n> `strlen()` to it.\n>\n> > +     if (len - 1 > INT_MAX) {\n> > +             strbuf_addf(err, _(\"too long filename in tree entry (%\"PRIuMAX\" bytes)\"),\n> > +                         (uintmax_t)(len - 1));\n> > +             return -1;\n> > +     }\n>\n> So restricting the length makes sense to me. But as mentioned above, do\n> we maybe want to check against `INT32_MAX`? I don't think we support any\n> platforms (or whether those even exist) with where int is 16 bits, but I\n> think it does make a ton of sense to behave the same on all platforms.\n\nFirst I was thinking that the limit should match the declared\nfield, which is \"int\" in this case.  But since INT32_MAX is never\nlarger than INT_MAX on any supported platform, it is both a safe\nlimit and gives the same behavior everywhere.\n\nI will change to INT32_MAX for a v2 reroll.\n\n>\n> We can then still adapt `struct name_entry::len` at a later point in\n> time.\n>\n> Ideally, we'd also have documentation around this somewhere to spell out\n> that tree entries may at most be INT32_MAX.\n\nI am not sure it is worth it.  The filesystem limits of paths\nand/or names are much lower than 2^31 anyway so this limit\nis unlikely to hit (or even impossible?) for any real users.\n\nI also could not really find any suitable document to put it\nin, and I think the other limits that are documented are ones\nthat are more practically reachable.  One exception may be\nindex.maxPathLen in config/index.adoc, which mentions the same\nlimit, but that is documented because it is a config option\nand thus needs to say which values are accepted, so it is\nnot fully comparable.\n\nThanks for taking a look!\nKristofer\n\n"}]}