[PATCH 3/4] index-pack: use size_t for object sizes
- From
Elia Pinto <gitter.spiros@gmail.com>
- Date
- Oct 31, 2026, 11:39 UTC
- Message-ID
- <02582827457e53b61548f544f57fd7f4044e725e.1793446357.git.gitter.spiros@gmail.com>
- In-Reply-To
- <cover.1793446357.git.gitter.spiros@gmail.com>
Functions like sha1_object() and append_obj_to_pack() in builtin/index-pack.c take object sizes as an "unsigned long", even though struct object_entry already tracks them as size_t. On platforms where "long" is 32 bits, this truncates the size for objects of 4GiB or more.
When receiving, on those platforms, a large object that already exists locally, sha1_object() compares the real size (size_t) against the truncated size, resulting in a false "SHA1 COLLISION FOUND" error and aborting index-pack.
Use size_t for the size parameter across these functions.
Triggering this requires processing 4GiB or more of input on a platform where "long" is 32 bits, so no test is added.
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com> --- builtin/index-pack.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/builtin/index-pack.c b/builtin/index-pack.c index f65228468b..d4c073654d 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -588,7 +588,7 @@ static void *unpack_raw_entry(struct object_entry *obj, } static void *unpack_data(struct object_entry *obj, - int (*consume)(const unsigned char *, unsigned long, void *), + int (*consume)(const unsigned char *, size_t, void *), void *cb_data) { off_t from = obj[0].idx.offset + obj[0].hdr_size; @@ -765,10 +765,10 @@ struct compare_data { struct object_entry *entry; struct odb_stream *st; unsigned char *buf; - unsigned long buf_size; + size_t buf_size; }; -static int compare_objects(const unsigned char *buf, unsigned long size, +static int compare_objects(const unsigned char *buf, size_t size, void *cb_data) { struct compare_data *data = cb_data; @@ -881,7 +881,7 @@ static void do_record_outgoing_links(struct object *obj) } static void sha1_object(const void *data, struct object_entry *obj_entry, - unsigned long size, enum object_type type, + size_t size, enum object_type type, const struct object_id *oid) { void *new_data = NULL; @@ -1438,11 +1438,11 @@ static size_t write_compressed(struct hashfile *f, void *in, size_t size) static struct object_entry *append_obj_to_pack(struct hashfile *f, const unsigned char *sha1, void *buf, - unsigned long size, enum object_type type) + size_t size, enum object_type type) { struct object_entry *obj = &objects[nr_objects++]; unsigned char header[10]; - unsigned long s = size; + size_t s = size; int n = 0; unsigned char c = (type << 4) | (s & 15); s >>= 4;
-- 2.53.0