Thread (3 messages) 3 messages, 1 author, 1d ago

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

flat view

From: Elia Pinto <hidden>
Date: 2026-11-01 09:19:30
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help