Skip to contentSkip to conversation

use size_t when receiving and verifying packs

PATCH Superseded Elia Pinto 3 participants 20 messages Oct 31 – Nov 19

lore
Share & tools
lore

Elia Pinto

author
#0 lore raw

cover use size_t when receiving and verifying packs

This series continues the search for truncated sizes, this time focusing on the side that receives and verifies packs: unpack-objects, index-pack, and pack verification in git fsck. These components keep object sizes as "unsigned long", while the underlying pack code already uses size_t.

Each patch addresses a specific part of the pack pipeline:
- 1/4 unpack-objects: on Windows, it dies when receiving an object of 4GiB or more;
- 2/4 index-pack write_compressed(): on all platforms ("unsigned int"), --fix-thin with a local base of 4GiB or more dies with "serious inflate inconsistency", verified by simulating at 16 bits;
- 3/4 index-pack: on Windows, it triggers a false "SHA1 COLLISION FOUND" when the object already exists locally;
- 4/4 pack-check: on Windows, "git fsck" checks a prefix of objects in packs.

These are all failures (die) or checks on a prefix; no corrupt data ends up being written.

No tests are added, for the same reason as the object parsing series (requiring 4GiB objects and 32-bit long platforms, or objects of 4GiB on all platforms for 2/4).

This series closes the pack-verification boundary left open for fsck_object() by the object parsing and checking series:
https://lore.kernel.org/git/[email protected]/
It does not depend on other in-flight series and merges cleanly with them. For the full effect on non-streamed objects in 1/4, the downstream fsck_object() / parse_object_buffer() (from the series above) and odb_write_object() (from v3 of "use size_t for a few more object and buffer sizes") are needed as well:
https://lore.kernel.org/git/[email protected]/
Pack-objects (delta search below core.bigFileThreshold) is left for a later series.

Based on master 50fe5cc63f.

Elia Pinto (4):
unpack-objects: use size_t for object sizes
index-pack: use size_t in write_compressed()
index-pack: use size_t for object sizes
pack-check: use size_t for the size passed to verify callbacks

 builtin/fsck.c           |  2 +-
 builtin/index-pack.c     | 14 +++++++-------
 builtin/unpack-objects.c | 36 +++++++++++++++++-------------------
 odb.h                    |  2 +-
 pack.h                   |  2 +-
 5 files changed, 27 insertions(+), 29 deletions(-)

base-commit: 50fe5cc63f9cd6e89b87a2c9ccf96a444f4cfdb7

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH 1/4 unpack-objects: use size_t for object sizes

builtin/unpack-objects.c uses "unsigned long" to track object sizes throughout the file, while sizes read from packs are handled as size_t. On platforms where "long" is 32 bits, this truncates the size for objects of 4GiB or more.

When handling large objects on those platforms, unpack-objects fails rather than corrupting data. For instance, streaming large blobs (exceeding core.bigFileThreshold) passes a truncated size to stream_blob(), causing the stream write to fail with an error.

Use size_t across the board in builtin/unpack-objects.c and remove the now-unnecessary size cast in unpack_delta_entry(). Downstream consumers like odb_write_object() and fsck_object() are updated separately.

Triggering this requires processing 4GiB or more of input on a platform where "long" is 32 bits, so no test is added.

---
 builtin/unpack-objects.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
builtin/unpack-objects.c +17 -19
index 351948724a..6c830a606f 100644
@@ -40,7 +40,7 @@ static struct progress *progress;
  */
 struct obj_buffer {
 	char *buffer;
-	unsigned long size;
+	size_t size;
 };
 
 static struct decoration obj_decorate;
@@ -50,7 +50,7 @@ static struct obj_buffer *lookup_object_buffer(struct object *base)
 	return lookup_decoration(&obj_decorate, base);
 }
 
-static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
+static void add_object_buffer(struct object *object, char *buffer, size_t size)
 {
 	struct obj_buffer *obj;
 	CALLOC_ARRAY(obj, 1);
@@ -114,10 +114,10 @@ static void use(int bytes)
  * allocated buffer which is reused to hold temporary zstream output
  * and return NULL instead of returning garbage data.
  */
-static void *get_data(unsigned long size)
+static void *get_data(size_t size)
 {
 	git_zstream stream;
-	unsigned long bufsize = dry_run && size > 8192 ? 8192 : size;
+	size_t bufsize = dry_run && size > 8192 ? 8192 : size;
 	void *buf = xmallocz(bufsize);
 
 	memset(&stream, 0, sizeof(stream));
@@ -161,7 +161,7 @@ struct delta_info {
 	struct object_id base_oid;
 	unsigned nr;
 	off_t base_offset;
-	unsigned long size;
+	size_t size;
 	void *delta;
 	struct delta_info *next;
 };
@@ -170,7 +170,7 @@ static struct delta_info *delta_list;
 
 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
 			      off_t base_offset,
-			      void *delta, unsigned long size)
+			      void *delta, size_t size)
 {
 	struct delta_info *info = xmalloc(sizeof(*info));
 
@@ -261,7 +261,7 @@ static void write_rest(void)
 }
 
 static void added_object(unsigned nr, enum object_type type,
-			 void *data, unsigned long size);
+			 void *data, size_t size);
 
 /*
  * Write out nr-th object from the list, now we know the contents
@@ -269,7 +269,7 @@ static void added_object(unsigned nr, enum object_type type,
  * to be checked at the end.
  */
 static void write_object(unsigned nr, enum object_type type,
-			 void *buf, unsigned long size)
+			 void *buf, size_t size)
 {
 	if (!strict) {
 		if (odb_write_object(the_repository->objects, buf, size, type,
@@ -310,8 +310,8 @@ static void write_object(unsigned nr, enum object_type type,
 }
 
 static void resolve_delta(unsigned nr, enum object_type type,
-			  void *base, unsigned long base_size,
-			  void *delta, unsigned long delta_size)
+			  void *base, size_t base_size,
+			  void *delta, size_t delta_size)
 {
 	void *result;
 	size_t result_size;
@@ -330,7 +330,7 @@ static void resolve_delta(unsigned nr, enum object_type type,
  * resolve all the deltified objects that are based on it.
  */
 static void added_object(unsigned nr, enum object_type type,
-			 void *data, unsigned long size)
+			 void *data, size_t size)
 {
 	struct delta_info **p = &delta_list;
 	struct delta_info *info;
@@ -349,7 +349,7 @@ static void added_object(unsigned nr, enum object_type type,
 	}
 }
 
-static void unpack_non_delta_entry(enum object_type type, unsigned long size,
+static void unpack_non_delta_entry(enum object_type type, size_t size,
 				   unsigned nr)
 {
 	void *buf = get_data(size);
@@ -386,7 +386,7 @@ static ssize_t zlib_stream_read(struct odb_stream *in_stream,
 	return buf_len - zstream->avail_out;
 }
 
-static void stream_blob(unsigned long size, unsigned nr)
+static void stream_blob(size_t size, unsigned nr)
 {
 	git_zstream zstream = { 0 };
 	struct zlib_stream in_stream = {
@@ -420,7 +420,7 @@ static void stream_blob(unsigned long size, unsigned nr)
 }
 
 static int resolve_against_held(unsigned nr, const struct object_id *base,
-				void *delta_data, unsigned long delta_size)
+				void *delta_data, size_t delta_size)
 {
 	struct object *obj;
 	struct obj_buffer *obj_buffer;
@@ -435,12 +435,11 @@ static int resolve_against_held(unsigned nr, const struct object_id *base,
 	return 1;
 }
 
-static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
+static void unpack_delta_entry(enum object_type type, size_t delta_size,
 			       unsigned nr)
 {
 	void *delta_data, *base;
-	unsigned long base_size;
-	size_t base_size_st = 0;
+	size_t base_size = 0;
 	struct object_id base_oid;
 
 	if (type == OBJ_REF_DELTA) {
@@ -517,8 +516,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
 		return;
 
 	base = odb_read_object(the_repository->objects, &base_oid,
-			       &type, &base_size_st);
-	base_size = cast_size_t_to_ulong(base_size_st);
+			       &type, &base_size);
 	if (!base) {
 		error("failed to read delta-pack base object %s",
 		      oid_to_hex(&base_oid));
Signature · 1 line
2.53.0
author
#2 lore raw

PATCH 2/4 index-pack: use size_t in write_compressed()

In reply to Elia Pinto

write_compressed() in builtin/index-pack.c takes the object size as an "unsigned int" and returns an "int", which are 32 bits wide on all platforms. It is used by append_obj_to_pack() when resolving thin packs with --fix-thin.

With a local base of 4GiB or more, write_compressed() compresses only the first "size mod 2^32" bytes, while the pack header written by append_obj_to_pack() records the full size. Later, when resolving deltas against that base, unpack_data() encounters an early end of stream and dies with "serious inflate inconsistency". Shrinking the parameter to 16 bits and using a 100KiB base makes "git index-pack --fix-thin" die the same way.

Use size_t for both the size parameter and the return value.

Triggering this requires processing a local base of 4GiB or more, so no test is added.

---
 builtin/index-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/index-pack.c +1 -1
index 0264c18c5f..f65228468b 100644
@@ -1411,7 +1411,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
 		    nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
 }
 
-static int write_compressed(struct hashfile *f, void *in, unsigned int size)
+static size_t write_compressed(struct hashfile *f, void *in, size_t size)
 {
 	git_zstream stream;
 	int status;
Signature · 1 line
2.53.0
author
#3 lore raw

PATCH 3/4 index-pack: use size_t for object sizes

In reply to Elia Pinto

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.

---
 builtin/index-pack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
builtin/index-pack.c +6 -6
index f65228468b..d4c073654d 100644
@@ -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;
Signature · 1 line
2.53.0
author
#4 lore raw

PATCH 4/4 pack-check: use size_t for the size passed to verify callbacks

In reply to Elia Pinto

verify_pack() in pack-check.c passes each object to a verify_fn callback (defined in pack.h) taking the object size as an "unsigned long", even though pack-check already handles it as a size_t. This signature is also replicated in struct odb_fsck_options (odb.h) and implemented by fsck_obj_buffer() in builtin/fsck.c.

On platforms where "long" is 32 bits, running "git fsck" on packs with objects of 4GiB or more passes a truncated size to fsck_obj_buffer(), causing parse_object_buffer() and fsck_object() to inspect only a prefix.

Use size_t for the size parameter in verify_fn, struct odb_fsck_options.object_cb, and fsck_obj_buffer().

Triggering this requires processing objects of 4GiB or more on a platform where "long" is 32 bits, so no test is added.

---
 builtin/fsck.c | 2 +-
 odb.h          | 2 +-
 pack.h         | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
builtin/fsck.c +1 -1
index da90cf84bf..9f62aa1f4e 100644
@@ -390,7 +390,7 @@ static void check_connectivity(struct repository *repo)
 }
 
 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
-			   unsigned long size, void *buffer, int *eaten, void *cb_data)
+			   size_t size, void *buffer, int *eaten, void *cb_data)
 {
 	struct repository *repo = cb_data;
 	struct object *obj;
odb.h +1 -1
index dd85b880a7..9cb36ed5ad 100644
@@ -197,7 +197,7 @@ struct odb_fsck_options {
 	enum odb_fsck_flags flags;
 
 	int (*object_cb)(const struct object_id *oid, enum object_type type,
-			 unsigned long size, void *buffer, int *eaten, void *cb_data);
+			 size_t size, void *buffer, int *eaten, void *cb_data);
 	void *object_payload;
 };
 
pack.h +1 -1
index ada506b5c5..d9649c6ee0 100644
@@ -88,7 +88,7 @@ struct progress;
 /* Note, the data argument could be NULL if object type is blob */
 typedef int (*verify_fn)(const struct object_id *oid,
 			 enum object_type type,
-			 unsigned long size,
+			 size_t size,
 			 void *buffer, int *eaten,
 			 void *fn_data);
 
Signature · 1 line
2.53.0
author
#5 lore raw

v2 cover use size_t when receiving and verifying packs

In reply to Elia Pinto

This series fixes object sizes that are truncated when receiving and verifying packs. Most of them are kept in an "unsigned long", which is a problem where "long" is 32 bits while size_t is 64, such as on 64-bit Windows; 2/4 is about an "unsigned int", which is 32 bits everywhere.

1/4 unpack-objects fails with "inflate returned -5" on an object of
4GiB or more that is not streamed, dies on a delta base that large, and silently writes a truncated object for a delta result that large. With 1/4, "unpack-objects -n" and large delta bases work, and writing such an object, unless it is streamed, dies with a clear error.

2/4 "index-pack --fix-thin" with a local base of 4GiB or more
compresses only part of it and dies later with "serious inflate inconsistency" (all platforms).

3/4 index-pack reports a false "SHA1 COLLISION FOUND" for a large
object that already exists locally.

4/4 "git fsck" checks only a prefix of large objects in packs.

None of them comes with a test, as each needs an object of 4GiB or more; 1/4 and 2/4 describe how I checked them with the size truncated to 16 bits.

Changes since v1:

- Rebased onto master at e1ec09b23c (The 22nd batch). v1's 1/4 no longer applied: e07ac16d1c (unpack-objects: pass the size of a streamed blob as size_t, 2026-11-06) by Kristofer Karlsson, now in master, makes the same change to stream_blob(). 1/4 drops that hunk; Kristofer is Cc'd.

- The message of 1/4 used stream_blob() as its only example, so it is rewritten around the paths that are still affected: objects that are not streamed, all blobs with "git unpack-objects -n", and deltas.

- 1/4 no longer drops the cast_size_t_to_ulong() in unpack_delta_entry(), but moves it to write_object(), where the size is passed to odb_write_object() and parse_object_buffer(). v1 let objects of 4GiB or more reach these functions and be written truncated.

- 4/4 says that parse_object_buffer() and fsck_object() still see a truncated size until they take a size_t.

- The messages of 1/4, 3/4 and 4/4 now say that the problem is "long" being 32 bits while size_t is 64. The code of 2/4, 3/4 and 4/4 is unchanged.

There were no comments on v1.

v1 left pack-objects for a later series. That fix has since been merged as ep/pack-objects-streamed-large-blobs-fix (ed0aed1e17); it covers the streamed path and explains why the other truncations in pack-objects cannot be reached.

The callees odb_write_object(), parse_object_buffer() and fsck_object() still take an "unsigned long" on master. They are changed by two other series of mine, which are not queued yet:

https://lore.kernel.org/git/[email protected]/
https://lore.kernel.org/git/[email protected]/

The series is based on master at e1ec09b23c.

Elia Pinto (4):
unpack-objects: use size_t for object sizes
index-pack: use size_t in write_compressed()
index-pack: use size_t for object sizes
pack-check: use size_t for the size passed to verify callbacks

 builtin/fsck.c           |  2 +-
 builtin/index-pack.c     | 14 +++++++-------
 builtin/unpack-objects.c | 42 ++++++++++++++++++++--------------------
 odb.h                    |  2 +-
 pack.h                   |  2 +-
 5 files changed, 31 insertions(+), 31 deletions(-)

Range-diff against v1:
1: f259eeb6a6 ! 1: b2c3d349a4 unpack-objects: use size_t for object sizes
@@ Metadata
## Commit message ##
unpack-objects: use size_t for object sizes

- builtin/unpack-objects.c uses "unsigned long" to track object sizes
- throughout the file, while sizes read from packs are handled as size_t.
- On platforms where "long" is 32 bits, this truncates the size for
- objects of 4GiB or more.
+ Since f2063855fb (index-pack, unpack-objects: use size_t for object
+ size, 2026-05-08), unpack_one() decodes the size of an object into a
+ size_t, and e07ac16d1c (unpack-objects: pass the size of a streamed
+ blob as size_t, 2026-11-06) passes it on as such to stream_blob(),
+ which handles blobs larger than core.bigFileThreshold. The rest of
+ builtin/unpack-objects.c still takes the size as an "unsigned long":

- When handling large objects on those platforms, unpack-objects fails
- rather than corrupting data. For instance, streaming large blobs
- (exceeding core.bigFileThreshold) passes a truncated size to
- stream_blob(), causing the stream write to fail with an error.
+ - unpack_non_delta_entry() and get_data(), used for commits, trees,
+ tags and blobs below core.bigFileThreshold, and for all blobs with
+ "git unpack-objects -n", which never streams;

- Use size_t across the board in builtin/unpack-objects.c and remove the
- now-unnecessary size cast in unpack_delta_entry(). Downstream consumers
- like odb_write_object() and fsck_object() are updated separately.
+ - unpack_delta_entry() and the functions that keep and resolve
+ deltas;

- Triggering this requires processing 4GiB or more of input on a platform
- where "long" is 32 bits, so no test is added.
+ - struct obj_buffer, which keeps objects around for --strict.
+
+ On platforms where "long" is 32 bits while size_t is 64, such as 64-bit
+ Windows, the size of an object of 4GiB or more is therefore truncated:
+
+ - For an object that is not streamed, get_data() allocates a buffer
+ of the truncated size, inflate runs out of room in it, and
+ unpack-objects fails with "inflate returned -5". Truncating the
+ size to 16 bits in get_data() and unpack_non_delta_entry()
+ reproduces this with a pack holding a 70000-byte blob, both with
+ and without -n.
+
+ - unpack_delta_entry() reads the base of a delta through
+ cast_size_t_to_ulong(), so a base of 4GiB or more makes it die even
+ when the result would be smaller.
+
+ - The result of a delta reaches write_object() as a size_t, but
+ write_object() takes it as an "unsigned long". A result of 4GiB or
+ more is silently truncated there, and an object with the wrong
+ contents, hence the wrong name, is written.
+
+ Use size_t across builtin/unpack-objects.c. The functions that
+ write_object() hands objects to, odb_write_object() and
+ parse_object_buffer(), still take an "unsigned long", as does
+ fsck_object(); they are updated separately. Until then, move the size
+ cast from unpack_delta_entry() to write_object(), where the size is
+ passed to them. fsck_object() only sees objects that went through
+ parse_object_buffer() first, and so does write_cached_object(), which
+ writes them out later; neither needs a cast of its own.
+
+ As a result, on those platforms:
+
+ - "git unpack-objects -n" handles objects of any size;
+
+ - a delta with a base of 4GiB or more and a smaller result is
+ resolved;
+
+ - an object of 4GiB or more that is not streamed and has to be
+ written, including the result of a delta, makes unpack-objects die
+ with "object too large to read on this platform" instead of failing
+ with "inflate returned -5" or being written truncated.
+
+ Truncating the size to 16 bits where write_object() passes it on
+ confirms the last point for a 70000-byte blob: with the cast,
+ unpack-objects dies with "object too large to read on this platform:
+ 70000 is cut off to 4464"; without it, it exits with 0 and writes a
+ truncated blob under another name.
+
+ Triggering this requires processing an object of 4GiB or more on a
+ platform where "long" is 32 bits while size_t is 64, so no test is
+ added.

@@ builtin/unpack-objects.c: static void added_object(unsigned nr, enum object_type
+ void *buf, size_t size)
{
if (!strict) {
- if (odb_write_object(the_repository->objects, buf, size, type,
+- if (odb_write_object(the_repository->objects, buf, size, type,
++ if (odb_write_object(the_repository->objects, buf,
++ cast_size_t_to_ulong(size), type,
+ &obj_list[nr].oid) < 0)
+ die("failed to write object");
+ added_object(nr, type, buf, size);
+@@ builtin/unpack-objects.c: static void write_object(unsigned nr, enum object_type type,
+ obj_list[nr].obj = NULL;
+ } else if (type == OBJ_BLOB) {
+ struct blob *blob;
+- if (odb_write_object(the_repository->objects, buf, size, type,
++ if (odb_write_object(the_repository->objects, buf,
++ cast_size_t_to_ulong(size), type,
+ &obj_list[nr].oid) < 0)
+ die("failed to write object");
+ added_object(nr, type, buf, size);
+@@ builtin/unpack-objects.c: static void write_object(unsigned nr, enum object_type type,
+ &obj_list[nr].oid);
+ added_object(nr, type, buf, size);
+ obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
+- type, size, buf,
++ type, cast_size_t_to_ulong(size), buf,
+ &eaten);
+ if (!obj)
+ die("invalid %s", type_name(type));
@@ builtin/unpack-objects.c: static void write_object(unsigned nr, enum object_type type,
}

@@ builtin/unpack-objects.c: static void added_object(unsigned nr, enum object_type
unsigned nr)
{
void *buf = get_data(size);
-@@ builtin/unpack-objects.c: static ssize_t zlib_stream_read(struct odb_stream *in_stream,
- return buf_len - zstream->avail_out;
- }
-
--static void stream_blob(unsigned long size, unsigned nr)
-+static void stream_blob(size_t size, unsigned nr)
- {
- git_zstream zstream = { 0 };
- struct zlib_stream in_stream = {
-@@ builtin/unpack-objects.c: static void stream_blob(unsigned long size, unsigned nr) +@@ builtin/unpack-objects.c: static void stream_blob(size_t size, unsigned nr)
}

static int resolve_against_held(unsigned nr, const struct object_id *base,

2:  438f7c6ba2 = 2:  9952f0fb10 index-pack: use size_t in write_compressed()
3:  0258282745 ! 3:  e66860d274 index-pack: use size_t for object sizes

@@ Commit message
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.
+ where "long" is 32 bits while size_t is 64, such as 64-bit Windows,
+ this truncates the size of 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
@@ Commit message
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.
+ where "long" is 32 bits while size_t is 64, so no test is added.

4: 87b0d2bd96 ! 4: 00939c7f1b pack-check: use size_t for the size passed to verify callbacks
@@ Commit message
replicated in struct odb_fsck_options (odb.h) and implemented by fsck_obj_buffer() in builtin/fsck.c.

- On platforms where "long" is 32 bits, running "git fsck" on packs with
- objects of 4GiB or more passes a truncated size to fsck_obj_buffer(),
- causing parse_object_buffer() and fsck_object() to inspect only a
- prefix.
+ On platforms where "long" is 32 bits while size_t is 64, such as
+ 64-bit Windows, running "git fsck" on packs with objects of 4GiB or
+ more passes a truncated size to fsck_obj_buffer(), causing
+ parse_object_buffer() and fsck_object() to inspect only a prefix.

Use size_t for the size parameter in verify_fn, struct odb_fsck_options.object_cb, and fsck_obj_buffer().
+ parse_object_buffer() and fsck_object() themselves still take an
+ "unsigned long"; until they are updated separately, they keep
+ inspecting only a prefix.

Triggering this requires processing objects of 4GiB or more on a
- platform where "long" is 32 bits, so no test is added.
+ platform where "long" is 32 bits while size_t is 64, so no test is
+ added.

base-commit: e1ec09b23c8e0deb21a0b95ac5996422277395d4

Signature · 1 line
2.53.0
author
#6 lore raw

PATCH v2 1/4 unpack-objects: use size_t for object sizes

Since f2063855fb (index-pack, unpack-objects: use size_t for object size, 2026-05-08), unpack_one() decodes the size of an object into a size_t, and e07ac16d1c (unpack-objects: pass the size of a streamed blob as size_t, 2026-11-06) passes it on as such to stream_blob(), which handles blobs larger than core.bigFileThreshold. The rest of builtin/unpack-objects.c still takes the size as an "unsigned long":

- unpack_non_delta_entry() and get_data(), used for commits, trees, tags and blobs below core.bigFileThreshold, and for all blobs with "git unpack-objects -n", which never streams;

- unpack_delta_entry() and the functions that keep and resolve deltas;

- struct obj_buffer, which keeps objects around for --strict.

On platforms where "long" is 32 bits while size_t is 64, such as 64-bit Windows, the size of an object of 4GiB or more is therefore truncated:

- For an object that is not streamed, get_data() allocates a buffer of the truncated size, inflate runs out of room in it, and unpack-objects fails with "inflate returned -5". Truncating the size to 16 bits in get_data() and unpack_non_delta_entry() reproduces this with a pack holding a 70000-byte blob, both with and without -n.

- unpack_delta_entry() reads the base of a delta through cast_size_t_to_ulong(), so a base of 4GiB or more makes it die even when the result would be smaller.

- The result of a delta reaches write_object() as a size_t, but write_object() takes it as an "unsigned long". A result of 4GiB or more is silently truncated there, and an object with the wrong contents, hence the wrong name, is written.

Use size_t across builtin/unpack-objects.c. The functions that write_object() hands objects to, odb_write_object() and parse_object_buffer(), still take an "unsigned long", as does fsck_object(); they are updated separately. Until then, move the size cast from unpack_delta_entry() to write_object(), where the size is passed to them. fsck_object() only sees objects that went through parse_object_buffer() first, and so does write_cached_object(), which writes them out later; neither needs a cast of its own.

As a result, on those platforms:

- "git unpack-objects -n" handles objects of any size;

- a delta with a base of 4GiB or more and a smaller result is resolved;

- an object of 4GiB or more that is not streamed and has to be written, including the result of a delta, makes unpack-objects die with "object too large to read on this platform" instead of failing with "inflate returned -5" or being written truncated.

Truncating the size to 16 bits where write_object() passes it on confirms the last point for a 70000-byte blob: with the cast, unpack-objects dies with "object too large to read on this platform:
70000 is cut off to 4464"; without it, it exits with 0 and writes a truncated blob under another name.

Triggering this requires processing an object of 4GiB or more on a platform where "long" is 32 bits while size_t is 64, so no test is added.

---
 builtin/unpack-objects.c | 42 ++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 21 deletions(-)
builtin/unpack-objects.c +21 -21
index d2f25bed49..9bd355b2c4 100644
@@ -40,7 +40,7 @@ static struct progress *progress;
  */
 struct obj_buffer {
 	char *buffer;
-	unsigned long size;
+	size_t size;
 };
 
 static struct decoration obj_decorate;
@@ -50,7 +50,7 @@ static struct obj_buffer *lookup_object_buffer(struct object *base)
 	return lookup_decoration(&obj_decorate, base);
 }
 
-static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
+static void add_object_buffer(struct object *object, char *buffer, size_t size)
 {
 	struct obj_buffer *obj;
 	CALLOC_ARRAY(obj, 1);
@@ -114,10 +114,10 @@ static void use(int bytes)
  * allocated buffer which is reused to hold temporary zstream output
  * and return NULL instead of returning garbage data.
  */
-static void *get_data(unsigned long size)
+static void *get_data(size_t size)
 {
 	git_zstream stream;
-	unsigned long bufsize = dry_run && size > 8192 ? 8192 : size;
+	size_t bufsize = dry_run && size > 8192 ? 8192 : size;
 	void *buf = xmallocz(bufsize);
 
 	memset(&stream, 0, sizeof(stream));
@@ -161,7 +161,7 @@ struct delta_info {
 	struct object_id base_oid;
 	unsigned nr;
 	off_t base_offset;
-	unsigned long size;
+	size_t size;
 	void *delta;
 	struct delta_info *next;
 };
@@ -170,7 +170,7 @@ static struct delta_info *delta_list;
 
 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
 			      off_t base_offset,
-			      void *delta, unsigned long size)
+			      void *delta, size_t size)
 {
 	struct delta_info *info = xmalloc(sizeof(*info));
 
@@ -261,7 +261,7 @@ static void write_rest(void)
 }
 
 static void added_object(unsigned nr, enum object_type type,
-			 void *data, unsigned long size);
+			 void *data, size_t size);
 
 /*
  * Write out nr-th object from the list, now we know the contents
@@ -269,10 +269,11 @@ static void added_object(unsigned nr, enum object_type type,
  * to be checked at the end.
  */
 static void write_object(unsigned nr, enum object_type type,
-			 void *buf, unsigned long size)
+			 void *buf, size_t size)
 {
 	if (!strict) {
-		if (odb_write_object(the_repository->objects, buf, size, type,
+		if (odb_write_object(the_repository->objects, buf,
+				     cast_size_t_to_ulong(size), type,
 				     &obj_list[nr].oid) < 0)
 			die("failed to write object");
 		added_object(nr, type, buf, size);
@@ -280,7 +281,8 @@ static void write_object(unsigned nr, enum object_type type,
 		obj_list[nr].obj = NULL;
 	} else if (type == OBJ_BLOB) {
 		struct blob *blob;
-		if (odb_write_object(the_repository->objects, buf, size, type,
+		if (odb_write_object(the_repository->objects, buf,
+				     cast_size_t_to_ulong(size), type,
 				     &obj_list[nr].oid) < 0)
 			die("failed to write object");
 		added_object(nr, type, buf, size);
@@ -299,7 +301,7 @@ static void write_object(unsigned nr, enum object_type type,
 				 &obj_list[nr].oid);
 		added_object(nr, type, buf, size);
 		obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
-					  type, size, buf,
+					  type, cast_size_t_to_ulong(size), buf,
 					  &eaten);
 		if (!obj)
 			die("invalid %s", type_name(type));
@@ -310,8 +312,8 @@ static void write_object(unsigned nr, enum object_type type,
 }
 
 static void resolve_delta(unsigned nr, enum object_type type,
-			  void *base, unsigned long base_size,
-			  void *delta, unsigned long delta_size)
+			  void *base, size_t base_size,
+			  void *delta, size_t delta_size)
 {
 	void *result;
 	size_t result_size;
@@ -330,7 +332,7 @@ static void resolve_delta(unsigned nr, enum object_type type,
  * resolve all the deltified objects that are based on it.
  */
 static void added_object(unsigned nr, enum object_type type,
-			 void *data, unsigned long size)
+			 void *data, size_t size)
 {
 	struct delta_info **p = &delta_list;
 	struct delta_info *info;
@@ -349,7 +351,7 @@ static void added_object(unsigned nr, enum object_type type,
 	}
 }
 
-static void unpack_non_delta_entry(enum object_type type, unsigned long size,
+static void unpack_non_delta_entry(enum object_type type, size_t size,
 				   unsigned nr)
 {
 	void *buf = get_data(size);
@@ -420,7 +422,7 @@ static void stream_blob(size_t size, unsigned nr)
 }
 
 static int resolve_against_held(unsigned nr, const struct object_id *base,
-				void *delta_data, unsigned long delta_size)
+				void *delta_data, size_t delta_size)
 {
 	struct object *obj;
 	struct obj_buffer *obj_buffer;
@@ -435,12 +437,11 @@ static int resolve_against_held(unsigned nr, const struct object_id *base,
 	return 1;
 }
 
-static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
+static void unpack_delta_entry(enum object_type type, size_t delta_size,
 			       unsigned nr)
 {
 	void *delta_data, *base;
-	unsigned long base_size;
-	size_t base_size_st = 0;
+	size_t base_size = 0;
 	struct object_id base_oid;
 
 	if (type == OBJ_REF_DELTA) {
@@ -517,8 +518,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
 		return;
 
 	base = odb_read_object(the_repository->objects, &base_oid,
-			       &type, &base_size_st);
-	base_size = cast_size_t_to_ulong(base_size_st);
+			       &type, &base_size);
 	if (!base) {
 		error("failed to read delta-pack base object %s",
 		      oid_to_hex(&base_oid));
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

Quoted 62 lines · Elia Pinto

Since f2063855fb (index-pack, unpack-objects: use size_t for object size, 2026-05-08), unpack_one() decodes the size of an object into a size_t, and e07ac16d1c (unpack-objects: pass the size of a streamed blob as size_t, 2026-11-06) passes it on as such to stream_blob(), which handles blobs larger than core.bigFileThreshold. The rest of builtin/unpack-objects.c still takes the size as an "unsigned long":

- unpack_non_delta_entry() and get_data(), used for commits, trees, tags and blobs below core.bigFileThreshold, and for all blobs with "git unpack-objects -n", which never streams;

- unpack_delta_entry() and the functions that keep and resolve deltas;

- struct obj_buffer, which keeps objects around for --strict.

On platforms where "long" is 32 bits while size_t is 64, such as 64-bit Windows, the size of an object of 4GiB or more is therefore truncated:

- For an object that is not streamed, get_data() allocates a buffer of the truncated size, inflate runs out of room in it, and unpack-objects fails with "inflate returned -5". Truncating the size to 16 bits in get_data() and unpack_non_delta_entry() reproduces this with a pack holding a 70000-byte blob, both with and without -n.

- unpack_delta_entry() reads the base of a delta through cast_size_t_to_ulong(), so a base of 4GiB or more makes it die even when the result would be smaller.

- The result of a delta reaches write_object() as a size_t, but write_object() takes it as an "unsigned long". A result of 4GiB or more is silently truncated there, and an object with the wrong contents, hence the wrong name, is written.

Use size_t across builtin/unpack-objects.c. The functions that write_object() hands objects to, odb_write_object() and parse_object_buffer(), still take an "unsigned long", as does fsck_object(); they are updated separately. Until then, move the size cast from unpack_delta_entry() to write_object(), where the size is passed to them. fsck_object() only sees objects that went through parse_object_buffer() first, and so does write_cached_object(), which writes them out later; neither needs a cast of its own.

As a result, on those platforms:

- "git unpack-objects -n" handles objects of any size;

- a delta with a base of 4GiB or more and a smaller result is resolved;

- an object of 4GiB or more that is not streamed and has to be written, including the result of a delta, makes unpack-objects die with "object too large to read on this platform" instead of failing with "inflate returned -5" or being written truncated.

Truncating the size to 16 bits where write_object() passes it on confirms the last point for a 70000-byte blob: with the cast, unpack-objects dies with "object too large to read on this platform:
70000 is cut off to 4464"; without it, it exits with 0 and writes a truncated blob under another name.

Quoted 3 lines

Triggering this requires processing an object of 4GiB or more on a platform where "long" is 32 bits while size_t is 64, so no test is added.

Phew, this commit message really is extremely verbose, and I eventually lost steam reading through it halfways. It's good to document what you're doing, but this here might have gone off the rails a bit with that.

What's worth documenting is the unexpected parts in your patch, and things that may not be immediately obvious to a reviewer. But you also document what specifically you are doing to what function, and that's quite obvious from the diff itself. So overall I bet that this message could be condensed to half or even a third of its size without losing much information.

Quoted 8 lines
builtin/unpack-objects.c +0 -0
index d2f25bed49..9bd355b2c4 100644
@@ -269,10 +269,11 @@ static void added_object(unsigned nr, enum object_type type,
  * to be checked at the end.
  */
 static void write_object(unsigned nr, enum object_type type,

builtin/unpack-objects.c static void added_object(unsigned nr, enum object_type type, · line 279

Quoted 10 lines
builtin/unpack-objects.c +3 -2
-			 void *buf, unsigned long size)
+			 void *buf, size_t size)
 {
 	if (!strict) {
-		if (odb_write_object(the_repository->objects, buf, size, type,
+		if (odb_write_object(the_repository->objects, buf,
+				     cast_size_t_to_ulong(size), type,
 				     &obj_list[nr].oid) < 0)
 			die("failed to write object");
 		added_object(nr, type, buf, size);

These casts here make me a bit unhappy, as you also have a patch series in flight that fix odb_write_object(). So you'll have to send another patch series after both have landed to remove those again.

Would it make more sense to defer this series here until the other one lands?

builtin/unpack-objects.c static void write_object(unsigned nr, enum object_type type, · line 307

Quoted 9 lines
builtin/unpack-objects.c +1 -1
@@ -299,7 +301,7 @@ static void write_object(unsigned nr, enum object_type type,
 				 &obj_list[nr].oid);
 		added_object(nr, type, buf, size);
 		obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
-					  type, size, buf,
+					  type, cast_size_t_to_ulong(size), buf,
 					  &eaten);
 		if (!obj)
 			die("invalid %s", type_name(type));

Likewise, even though I don't know whether you also have a patch series in flight fixing this one from the top of my head.

Patrick

#8 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

Quoted 6 lines · Patrick Steinhardt

What's worth documenting is the unexpected parts in your patch, and things that may not be immediately obvious to a reviewer. But you also document what specifically you are doing to what function, and that's quite obvious from the diff itself. So overall I bet that this message could be condensed to half or even a third of its size without losing much information.

Good observation and matches my impression on recent proposed log messages I see on the list, not limited to this author but by others. Perhaps people should paste their text to their nearby faviourite LLM and ask it to condense it down by 70% without losing substance, and notch it down if they feel the result lost too much, or something.

#9 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Patrick Steinhardt

What's worth documenting is the unexpected parts in your patch, and things that may not be immediately obvious to a reviewer. But you also document what specifically you are doing to what function, and that's quite obvious from the diff itself. So overall I bet that this message could be condensed to half or even a third of its size without losing much information.

Quoted 6 lines

Good observation and matches my impression on recent proposed log messages I see on the list, not limited to this author but by others. Perhaps people should paste their text to their nearby faviourite LLM and ask it to condense it down by 70% without losing substance, and notch it down if they feel the result lost too much, or something.

That assumes that it's their text in the first place, and in many cases I'd say it's obvious that it is not. The offending texts really look too similar for this to be sheer coincidence. So we can probably avoid one roundtrip by telling the LLM to be concise in the first go already.

Patrick

author
#10 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

In reply to Patrick Steinhardt
Quoted 2 lines · Patrick Steinhardt

Phew, this commit message really is extremely verbose, and I eventually lost steam reading through it halfways.

[...]

Quoted 3 lines

So overall I bet that this message
could be condensed to half or even a third of its size without losing much information.

Fair enough. I will cut it down to what the diff does not show: why the cast moves to write_object() and what changes for the user.

Quoted 6 lines

These casts here make me a bit unhappy, as you also have a patch series in flight that fix odb_write_object(). So you'll have to send another patch series after both have landed to remove those again.

Would it make more sense to defer this series here until the other one lands?

Yes. odb_write_object() is changed by "use size_t for a few more object and buffer sizes" [1], and parse_object_buffer() and fsck_object() by "use size_t when parsing and checking objects" [2]. Once both are in, write_object() can pass the size on without a cast.

4/4 depends on [2] as well (see my reply there), so I will hold back 1/4 and 4/4 and resend them on top of [1] and [2]. 2/4 and 3/4 depend on neither: 2/4 fixes a bug on all platforms, 3/4 a false "SHA1 COLLISION FOUND". Unless you or Junio prefer otherwise, I will send those two on their own as v3, with your comments on 2/4 addressed.

[1] https://lore.kernel.org/git/[email protected]/
[2] https://lore.kernel.org/git/[email protected]/

Thanks,
Elia

#11 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

Quoted 23 lines · Elia Pinto
Quoted 2 lines · Patrick Steinhardt

Phew, this commit message really is extremely verbose, and I eventually lost steam reading through it halfways.

[...]

Quoted 3 lines

So overall I bet that this message
could be condensed to half or even a third of its size without losing much information.

Fair enough. I will cut it down to what the diff does not show: why the cast moves to write_object() and what changes for the user.

Quoted 6 lines

These casts here make me a bit unhappy, as you also have a patch series in flight that fix odb_write_object(). So you'll have to send another patch series after both have landed to remove those again.

Would it make more sense to defer this series here until the other one lands?

Yes. odb_write_object() is changed by "use size_t for a few more object and buffer sizes" [1], and parse_object_buffer() and fsck_object() by "use size_t when parsing and checking objects" [2]. Once both are in, write_object() can pass the size on without a cast.

Quoted 5 lines

4/4 depends on [2] as well (see my reply there), so I will hold back 1/4 and 4/4 and resend them on top of [1] and [2]. 2/4 and 3/4 depend on neither: 2/4 fixes a bug on all platforms, 3/4 a false "SHA1 COLLISION FOUND". Unless you or Junio prefer otherwise, I will send those two on their own as v3, with your comments on 2/4 addressed.

Do we maybe just want to hold off the whole series until the dependency lands? As you've seen, having a bunch of similar series out there does not guarantee that all of them land faster, but rather has the effect that some of them don't get any reviews at all.

That's not all that surprising, as reviewing these kinds of series is a bit tedious. That doesn't mean the changes are not useful, it's rather that we are short on reviewing capacity.

Patrick

author
#12 lore raw

Re: [PATCH v2 1/4] unpack-objects: use size_t for object sizes

Quoted 2 lines · Patrick Steinhardt

Do we maybe just want to hold off the whole series until the dependency lands?

OK, I'll resend the series when needed. Thanks,
Elia

author
#13 lore raw

PATCH v2 2/4 index-pack: use size_t in write_compressed()

In reply to Elia Pinto

write_compressed() in builtin/index-pack.c takes the object size as an "unsigned int" and returns an "int", which are 32 bits wide on all platforms. It is used by append_obj_to_pack() when resolving thin packs with --fix-thin.

With a local base of 4GiB or more, write_compressed() compresses only the first "size mod 2^32" bytes, while the pack header written by append_obj_to_pack() records the full size. Later, when resolving deltas against that base, unpack_data() encounters an early end of stream and dies with "serious inflate inconsistency". Shrinking the parameter to 16 bits and using a 100KiB base makes "git index-pack --fix-thin" die the same way.

Use size_t for both the size parameter and the return value.

Triggering this requires processing a local base of 4GiB or more, so no test is added.

---
 builtin/index-pack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/index-pack.c +1 -1
index 0264c18c5f..f65228468b 100644
@@ -1411,7 +1411,7 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
 		    nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas);
 }
 
-static int write_compressed(struct hashfile *f, void *in, unsigned int size)
+static size_t write_compressed(struct hashfile *f, void *in, size_t size)
 {
 	git_zstream stream;
 	int status;
Signature · 1 line
2.53.0
#14 lore raw

Re: [PATCH v2 2/4] index-pack: use size_t in write_compressed()

Quoted 13 lines · Elia Pinto

write_compressed() in builtin/index-pack.c takes the object size as an "unsigned int" and returns an "int", which are 32 bits wide on all platforms. It is used by append_obj_to_pack() when resolving thin packs with --fix-thin.

With a local base of 4GiB or more, write_compressed() compresses only the first "size mod 2^32" bytes, while the pack header written by append_obj_to_pack() records the full size. Later, when resolving deltas against that base, unpack_data() encounters an early end of stream and dies with "serious inflate inconsistency". Shrinking the parameter to 16 bits and using a 100KiB base makes "git index-pack --fix-thin" die the same way.

Quoted 1 line

Use size_t for both the size parameter and the return value.

Hmm. It's probably an improvement, but the only caller does this:

	obj[1].idx.offset += write_compressed(f, buf, size);

That's a struct pack_idx_entry, but its offset field is of type off_t, which is signed. It feels sufficiently unlikely to ever overflow, but should we verify regardless that we don't?

Quoted 2 lines

Triggering this requires processing a local base of 4GiB or more, so no test is added.

Not even an EXPENSIVE one?

Patrick

author
#15 lore raw

Re: [PATCH v2 2/4] index-pack: use size_t in write_compressed()

Quoted 7 lines · Patrick Steinhardt

Hmm. It's probably an improvement, but the only caller does this:

	obj[1].idx.offset += write_compressed(f, buf, size);

That's a struct pack_idx_entry, but its offset field is of type off_t, which is signed. It feels sufficiently unlikely to ever overflow, but should we verify regardless that we don't?

It costs little, and index-pack already does so for the data it
reads: use() dies with "pack too large for current definition of off_t" when signed_add_overflows(consumed_bytes, bytes). I will do the same in append_obj_to_pack() in v3.

Quoted 1 line

Not even an EXPENSIVE one?

I can add one. It needs a local base of 4GiB and a thin pack with a delta against it. Writing that small delta by hand keeps pack-objects out of the picture, so only index-pack has to hold the base in memory. As it still takes a lot of memory and time, I would guard it with its own variable, like t5608 does with GIT_TEST_CLONE_2GB.

Thanks,
Elia

author
#16 lore raw

PATCH v2 3/4 index-pack: use size_t for object sizes

In reply to Elia Pinto

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 while size_t is 64, such as 64-bit Windows, this truncates the size of 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 while size_t is 64, so no test is added.

---
 builtin/index-pack.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
builtin/index-pack.c +6 -6
index f65228468b..d4c073654d 100644
@@ -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;
Signature · 1 line
2.53.0
author
#17 lore raw

PATCH v2 4/4 pack-check: use size_t for the size passed to verify callbacks

In reply to Elia Pinto

verify_pack() in pack-check.c passes each object to a verify_fn callback (defined in pack.h) taking the object size as an "unsigned long", even though pack-check already handles it as a size_t. This signature is also replicated in struct odb_fsck_options (odb.h) and implemented by fsck_obj_buffer() in builtin/fsck.c.

On platforms where "long" is 32 bits while size_t is 64, such as 64-bit Windows, running "git fsck" on packs with objects of 4GiB or more passes a truncated size to fsck_obj_buffer(), causing parse_object_buffer() and fsck_object() to inspect only a prefix.

Use size_t for the size parameter in verify_fn,
struct odb_fsck_options.object_cb, and fsck_obj_buffer(). parse_object_buffer() and fsck_object() themselves still take an "unsigned long"; until they are updated separately, they keep inspecting only a prefix.

Triggering this requires processing objects of 4GiB or more on a platform where "long" is 32 bits while size_t is 64, so no test is added.

---
 builtin/fsck.c | 2 +-
 odb.h          | 2 +-
 pack.h         | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
builtin/fsck.c +1 -1
index c4558efcf1..5849194686 100644
@@ -392,7 +392,7 @@ static void check_connectivity(struct repository *repo)
 }
 
 static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
-			   unsigned long size, void *buffer, int *eaten, void *cb_data)
+			   size_t size, void *buffer, int *eaten, void *cb_data)
 {
 	struct repository *repo = cb_data;
 	struct object *obj;
odb.h +1 -1
index 857637f980..9d4ef41d9b 100644
@@ -210,7 +210,7 @@ struct odb_fsck_options {
 	enum odb_fsck_flags flags;
 
 	int (*object_cb)(const struct object_id *oid, enum object_type type,
-			 unsigned long size, void *buffer, int *eaten, void *cb_data);
+			 size_t size, void *buffer, int *eaten, void *cb_data);
 	void *object_payload;
 };
 
pack.h +1 -1
index 2f2ce97c11..acc72a9527 100644
@@ -88,7 +88,7 @@ struct progress;
 /* Note, the data argument could be NULL if object type is blob */
 typedef int (*verify_fn)(const struct object_id *oid,
 			 enum object_type type,
-			 unsigned long size,
+			 size_t size,
 			 void *buffer, int *eaten,
 			 void *fn_data);
 
Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH v2 4/4] pack-check: use size_t for the size passed to verify callbacks

Quoted 10 lines · Elia Pinto

verify_pack() in pack-check.c passes each object to a verify_fn callback (defined in pack.h) taking the object size as an "unsigned long", even though pack-check already handles it as a size_t. This signature is also replicated in struct odb_fsck_options (odb.h) and implemented by fsck_obj_buffer() in builtin/fsck.c.

On platforms where "long" is 32 bits while size_t is 64, such as 64-bit Windows, running "git fsck" on packs with objects of 4GiB or more passes a truncated size to fsck_obj_buffer(), causing parse_object_buffer() and fsck_object() to inspect only a prefix.

The more interesting question to me is: if they only inspect a prefix, do they actually notice that something is off and report the error? If not that certainly sounds like a bug to me.

Patrick

author
#19 lore raw

Re: [PATCH v2 4/4] pack-check: use size_t for the size passed to verify callbacks

Quoted 3 lines · Patrick Steinhardt

The more interesting question to me is: if they only inspect a prefix, do they actually notice that something is off and report the error? If not that certainly sounds like a bug to me.

Looking at it again, on master the callback is not even reached for such an object. Just before calling it, verify_packfile() checks the object with check_object_signature(), which also takes an "unsigned long". It hashes only the prefix, the hash does not match, and fsck reports "packed <oid> from <pack> is corrupt" for an object that is fine. So fsck does complain, but for the wrong reason, and the commit message is wrong to say that the callbacks look at a prefix today.

check_object_signature() is changed by 1/4 of "use size_t when parsing and checking objects" [1]. Only then is the callback reached, and it needs this patch to get the whole size, plus the rest of [1] for parse_object_buffer() and fsck_object(). So 4/4 belongs on top of [1], and I will resend it there with a corrected message.

Blobs are not affected: on these platforms core.bigFileThreshold, an "unsigned long" itself, is below 4GiB, so every blob that large is checked with stream_object_signature() and reaches the callback without its data.

[1] https://lore.kernel.org/git/[email protected]/

Thanks,
Elia