[PATCH 1/2] tree-walk: use size_t for the size of tree buffers
- From
Elia Pinto <gitter.spiros@gmail.com>
- Date
- Oct 30, 2026, 10:58 UTC
- Message-ID
- <908bcf7a607195d7f5215f191a85f25ab0059e8a.1793357680.git.gitter.spiros@gmail.com>
- In-Reply-To
- <cover.1793357680.git.gitter.spiros@gmail.com>
struct tree_desc keeps the number of bytes left in the tree buffer in an "unsigned int", and init_tree_desc(), init_tree_desc_gently() and decode_tree_entry() take the size as an "unsigned long". The size comes from odb_read_object() as a size_t, so for a tree object of 4GiB or more it is truncated. As the field is an "unsigned int", this happens on all platforms, not only those where "long" is 32 bits.
The walk then sees only the first "size mod 2^32" bytes of the buffer. decode_tree_entry() still checks that the NUL ending the last entry is within that size, so we never read past the buffer. But when the cut falls between two entries, the rest of the tree silently disappears: "git ls-tree" lists fewer entries and exits with 0, and fsck_tree() only checks the entries it sees, so its checks for ".git", ".gitmodules" and the like never look at the others. When the cut falls inside an entry, we die with "too-short tree" instead.
Use size_t for the size in struct tree_desc, in the functions that set it up and advance it, and in the struct dir_state used by get_tree_entry_follow_symlinks().
There is no test, as it would need a tree larger than 4GiB in memory. Truncating the size to 16 bits instead, in init_tree_desc_internal() and update_tree_entry_internal(), makes "git ls-tree" list 10 of the 2058 entries of a 65856-byte tree created with "git mktree", without any error.
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> --- tree-walk.c | 14 +++++++------- tree-walk.h | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/tree-walk.c b/tree-walk.c index a67f06b9eb..aaac1fd48d 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -14,7 +14,7 @@ #include "environment.h" #include "read-cache-ll.h" -static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err) +static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err) { const char *path; unsigned int len; @@ -49,7 +49,7 @@ static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned l static int init_tree_desc_internal(struct tree_desc *desc, const struct object_id *oid, - const void *buffer, unsigned long size, + const void *buffer, size_t size, struct strbuf *err, enum tree_desc_flags flags) { @@ -63,7 +63,7 @@ static int init_tree_desc_internal(struct tree_desc *desc, } void init_tree_desc(struct tree_desc *desc, const struct object_id *tree_oid, - const void *buffer, unsigned long size) + const void *buffer, size_t size) { struct strbuf err = STRBUF_INIT; if (init_tree_desc_internal(desc, tree_oid, buffer, size, &err, 0)) @@ -72,7 +72,7 @@ void init_tree_desc(struct tree_desc *desc, const struct object_id *tree_oid, } int init_tree_desc_gently(struct tree_desc *desc, const struct object_id *oid, - const void *buffer, unsigned long size, + const void *buffer, size_t size, enum tree_desc_flags flags) { struct strbuf err = STRBUF_INIT; @@ -113,8 +113,8 @@ static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err { const void *buf = desc->buffer; const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + desc->algo->rawsz; - unsigned long size = desc->size; - unsigned long len = end - (const unsigned char *)buf; + size_t size = desc->size; + size_t len = end - (const unsigned char *)buf; if (size < len) die(_("too-short tree file")); @@ -561,7 +561,7 @@ int traverse_trees(struct index_state *istate, struct dir_state { void *tree; - unsigned long size; + size_t size; struct object_id oid; }; diff --git a/tree-walk.h b/tree-walk.h index 9646c47ac5..5ccd4a9361 100644 --- a/tree-walk.h +++ b/tree-walk.h @@ -35,7 +35,7 @@ struct tree_desc { struct name_entry entry; /* counts the number of bytes left in the `buffer`. */ - unsigned int size; + size_t size; /* option flags passed via init_tree_desc_gently() */ enum tree_desc_flags { @@ -85,10 +85,10 @@ int update_tree_entry_gently(struct tree_desc *); * members of `struct tree`. */ void init_tree_desc(struct tree_desc *desc, const struct object_id *tree_oid, - const void *buf, unsigned long size); + const void *buf, size_t size); int init_tree_desc_gently(struct tree_desc *desc, const struct object_id *oid, - const void *buf, unsigned long size, + const void *buf, size_t size, enum tree_desc_flags flags); /*
-- 2.53.0