git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[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
Previous: Elia PintoNext: Elia Pinto
Message 4 of 13 in “use size_t when receiving and verifying packs”
  1. 0/4 use size_t when receiving and verifying packsElia Pinto, Oct 31, 2026
  2. 1/4 unpack-objects: use size_t for object sizesElia Pinto, Oct 31, 2026
  3. 2/4 index-pack: use size_t in write_compressed()Elia Pinto, Oct 31, 2026
  4. 3/4 index-pack: use size_t for object sizesElia Pinto, Oct 31, 2026
  5. 4/4 pack-check: use size_t for the size passed to verify callbacksElia Pinto, Oct 31, 2026
  6. 0/4 use size_t when receiving and verifying packsElia Pinto, Nov 18, 2026
  7. 1/4 unpack-objects: use size_t for object sizesElia Pinto, Nov 18, 2026
  8. Patrick SteinhardtNov 18, 2026
  9. 2/4 index-pack: use size_t in write_compressed()Elia Pinto, Nov 18, 2026
  10. Patrick SteinhardtNov 18, 2026
  11. 3/4 index-pack: use size_t for object sizesElia Pinto, Nov 18, 2026
  12. 4/4 pack-check: use size_t for the size passed to verify callbacksElia Pinto, Nov 18, 2026
  13. Patrick SteinhardtNov 18, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.