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

[PATCH 2/2] apply: use size_t for the size of the result written out

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Nov 1, 2026, 09:19 UTC
Message-ID
<6446c73595b4d2d9360a25f816b5d42ee66b5393.1793524457.git.gitter.spiros@gmail.com>
In-Reply-To
<cover.1793524457.git.gitter.spiros@gmail.com>

While "git apply" limits the patch size, the result size is stored as size_t but passed to functions using "unsigned long size".

On platforms where "long" is 32 bits but size_t is 64 bits, such as 64-bit Windows, applying a patch to a file of 4GiB or more causes the result to be written out truncated with exit 0, silently losing data; this also affects "git am" and "git apply --index", which writes the truncated content to the index.

This was verified using a 16-bit simulation, where "git apply" left a 7901-line file truncated to 2859 lines.

Use size_t in create_file(), create_one_file(), try_create_file(), and add_index_file(). add_index_file() passes the size on to odb_write_object(), which takes a size_t with the "use size_t for a few more object and buffer sizes" series.

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

Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 apply.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/apply.c b/apply.c
index a0b7119c63..02d6791ad7 100644
--- a/apply.c
+++ b/apply.c
@@ -4458,7 +4458,7 @@ static int add_index_file(struct apply_state *state,
 			  const char *path,
 			  unsigned mode,
 			  void *buf,
-			  unsigned long size)
+			  size_t size)
 {
 	struct stat st;
 	struct cache_entry *ce;
@@ -4513,7 +4513,7 @@ static int add_index_file(struct apply_state *state,
  */
 static int try_create_file(struct apply_state *state, const char *path,
 			   unsigned int mode, const char *buf,
-			   unsigned long size)
+			   size_t size)
 {
 	int fd, res;
 	struct strbuf nbuf = STRBUF_INIT;
@@ -4564,7 +4564,7 @@ static int create_one_file(struct apply_state *state,
 			   char *path,
 			   unsigned mode,
 			   const char *buf,
-			   unsigned long size)
+			   size_t size)
 {
 	char *newpath = NULL;
 	int res;
@@ -4686,7 +4686,7 @@ static int create_file(struct apply_state *state, struct patch *patch)
 {
 	char *path = patch->new_name;
 	unsigned mode = patch->new_mode;
-	unsigned long size = patch->resultsize;
+	size_t size = patch->resultsize;
 	char *buf = patch->result;
 
 	if (!mode)
-- 
2.53.0
Previous: Elia PintoNext: Elia Pinto
Message 3 of 6 in “archive, apply: do not truncate files of 4GiB or more”
  1. 0/2 archive, apply: do not truncate files of 4GiB or moreElia Pinto, Nov 1, 2026
  2. 1/2 archive: use size_t for the size of archive entriesElia Pinto, Nov 1, 2026
  3. 2/2 apply: use size_t for the size of the result written outElia Pinto, Nov 1, 2026
  4. Elia PintoNov 19, 2026
  5. Patrick SteinhardtNov 19, 2026
  6. Elia PintoNov 19, 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.