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

[PATCH v2 6/7] packfile,delta: drop the `cast_size_t_to_ulong()` wrappers

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 15, 2026, 11:52 UTC
Message-ID
<12c142f8abb0df11f716a9cf6d0e4f41966f8548.1781524349.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2137.v2.git.1781524349.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

When I started the transition from `unsigned long` to `size_t`, in the interest of keeping the patches reviewable, I introduced these calls to prevent data type narrowing from silently failing to handle large object sizes. I also introduced `*_sz()` variants that would allow most of the callers to keep using that `unsigned long` that the 90s kindly asked to be returned.

After the preceding commits, the only places that called the narrow wrappers either no longer exist or already use the `_sz` form internally, so the wrappers just narrow values back through `cast_size_t_to_ulong()` for no reason.

Drop them and rename the `_sz` variants back to the natural names.
Assisted-by: Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 delta.h       | 14 ++------------
 packfile.c    | 28 ++++++++--------------------
 packfile.h    |  2 +-
 patch-delta.c |  4 ++--
 4 files changed, 13 insertions(+), 35 deletions(-)
diff --git a/delta.h b/delta.h
index bb149dc82b..eb5c6d2fdb 100644
--- a/delta.h
+++ b/delta.h
@@ -86,11 +86,8 @@ void *patch_delta(const void *src_buf, size_t src_size,
  * This must be called twice on the delta data buffer, first to get the
  * expected source buffer size, and again to get the target buffer size.
  */
-/*
- * Size_t variant that doesn't truncate - use for >4GB objects on Windows.
- */
-static inline size_t get_delta_hdr_size_sz(const unsigned char **datap,
-					   const unsigned char *top)
+static inline size_t get_delta_hdr_size(const unsigned char **datap,
+					const unsigned char *top)
 {
 	const unsigned char *data = *datap;
 	size_t cmd, size = 0;
@@ -104,11 +101,4 @@ static inline size_t get_delta_hdr_size_sz(const unsigned char **datap,
 	return size;
 }
 
-static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
-					       const unsigned char *top)
-{
-	size_t size = get_delta_hdr_size_sz(datap, top);
-	return cast_size_t_to_ulong(size);
-}
-
 #endif
diff --git a/packfile.c b/packfile.c
index dab0a9b16d..c174982d10 100644
--- a/packfile.c
+++ b/packfile.c
@@ -1164,11 +1164,12 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
 }
 
 /*
- * Size_t variant for >4GB delta results on Windows.
+ * Read a delta object's header at curpos in p (already inflated as needed)
+ * and return the size of the result object (the post-application target).
  */
-static size_t get_size_from_delta_sz(struct packed_git *p,
-				     struct pack_window **w_curs,
-				     off_t curpos)
+size_t get_size_from_delta(struct packed_git *p,
+			   struct pack_window **w_curs,
+			   off_t curpos)
 {
 	const unsigned char *data;
 	unsigned char delta_head[20], *in;
@@ -1215,18 +1216,10 @@ static size_t get_size_from_delta_sz(struct packed_git *p,
 	data = delta_head;
 
 	/* ignore base size */
-	get_delta_hdr_size_sz(&data, delta_head+sizeof(delta_head));
+	get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
 
 	/* Read the result size */
-	return get_delta_hdr_size_sz(&data, delta_head+sizeof(delta_head));
-}
-
-unsigned long get_size_from_delta(struct packed_git *p,
-				  struct pack_window **w_curs,
-				  off_t curpos)
-{
-	size_t size = get_size_from_delta_sz(p, w_curs, curpos);
-	return cast_size_t_to_ulong(size);
+	return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
 }
 
 int unpack_object_header(struct packed_git *p,
@@ -1634,12 +1627,7 @@ static int packed_object_info_with_index_pos(struct packed_git *p, off_t obj_off
 				ret = -1;
 				goto out;
 			}
-			/*
-			 * Use size_t variant to avoid die() on >4GB deltas.
-			 * oi->sizep is unsigned long, so truncation may occur,
-			 * but streaming code uses its own size_t tracking.
-			 */
-			size = get_size_from_delta_sz(p, &w_curs, tmp_pos);
+			size = get_size_from_delta(p, &w_curs, tmp_pos);
 			if (size == 0) {
 				ret = -1;
 				goto out;
diff --git a/packfile.h b/packfile.h
index 0b5ae3f9fc..bd4494906d 100644
--- a/packfile.h
+++ b/packfile.h
@@ -458,7 +458,7 @@ int is_pack_valid(struct packed_git *);
 void *unpack_entry(struct repository *r, struct packed_git *, off_t,
 		   enum object_type *, size_t *);
 unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, size_t *sizep);
-unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
+size_t get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
 int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, size_t *);
 off_t get_delta_base(struct packed_git *p, struct pack_window **w_curs,
 		     off_t *curpos, enum object_type type,
diff --git a/patch-delta.c b/patch-delta.c
index 44cda97994..42199fa956 100644
--- a/patch-delta.c
+++ b/patch-delta.c
@@ -27,12 +27,12 @@ void *patch_delta(const void *src_buf, size_t src_size,
 	top = (const unsigned char *) delta_buf + delta_size;
 
 	/* make sure the orig file size matches what we expect */
-	size = get_delta_hdr_size_sz(&data, top);
+	size = get_delta_hdr_size(&data, top);
 	if (size != src_size)
 		return NULL;
 
 	/* now the result size */
-	size = get_delta_hdr_size_sz(&data, top);
+	size = get_delta_hdr_size(&data, top);
 	dst_buf = xmallocz(size);
 
 	out = dst_buf;
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 25 of 29 in “More work supporting objects larger than 4GB on Windows”
  1. 0/7 More work supporting objects larger than 4GB on WindowsJohannes Schindelin via GitGitGadget, Jun 4, 2026
  2. 1/7 compat/msvc: use _chsize_s for ftruncateJohannes Schindelin via GitGitGadget, Jun 4, 2026
  3. 2/7 patch-delta: use size_t for sizesJohannes Schindelin via GitGitGadget, Jun 4, 2026
  4. Patrick SteinhardtJun 8, 2026
  5. Johannes SchindelinJun 15, 2026
  6. 3/7 pack-objects(check_pack_inflate()): use size_t instead of unsigned longJohannes Schindelin via GitGitGadget, Jun 4, 2026
  7. Patrick SteinhardtJun 8, 2026
  8. Johannes SchindelinJun 15, 2026
  9. 4/7 packfile: widen unpack_entry()'s size out-parameter to size_tJohannes Schindelin via GitGitGadget, Jun 4, 2026
  10. Patrick SteinhardtJun 8, 2026
  11. Johannes SchindelinJun 15, 2026
  12. 5/7 pack-objects: use size_t for in-core object sizesJohannes Schindelin via GitGitGadget, Jun 4, 2026
  13. 6/7 packfile,delta: drop the `cast_size_t_to_ulong()` wrappersJohannes Schindelin via GitGitGadget, Jun 4, 2026
  14. Patrick SteinhardtJun 8, 2026
  15. 7/7 odb: use size_t for object_info.sizep and the size APIsJohannes Schindelin via GitGitGadget, Jun 4, 2026
  16. Patrick SteinhardtJun 8, 2026
  17. Johannes SchindelinJun 15, 2026
  18. 0/7 More work supporting objects larger than 4GB on WindowsJohannes Schindelin via GitGitGadget, Jun 15, 2026
  19. 1/7 compat/msvc: use _chsize_s for ftruncateJohannes Schindelin via GitGitGadget, Jun 15, 2026
  20. 2/7 patch-delta: use size_t for sizesJohannes Schindelin via GitGitGadget, Jun 15, 2026
  21. Junio C HamanoJun 16, 2026
  22. 3/7 pack-objects(check_pack_inflate()): use size_t instead of unsigned longJohannes Schindelin via GitGitGadget, Jun 15, 2026
  23. 4/7 packfile: widen unpack_entry()'s size out-parameter to size_tJohannes Schindelin via GitGitGadget, Jun 15, 2026
  24. 5/7 pack-objects: use size_t for in-core object sizesJohannes Schindelin via GitGitGadget, Jun 15, 2026
  25. 6/7 packfile,delta: drop the `cast_size_t_to_ulong()` wrappersJohannes Schindelin via GitGitGadget, Jun 15, 2026
  26. 7/7 odb: use size_t for object_info.sizep and the size APIsJohannes Schindelin via GitGitGadget, Jun 15, 2026
  27. Junio C HamanoJun 15, 2026
  28. Patrick SteinhardtJun 18, 2026
  29. Junio C HamanoJun 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.