From: Elia Pinto Date: Sun, 01 Nov 2026 09:19:23 GMT Subject: [PATCH 2/2] apply: use size_t for the size of the result written out Message-ID: <6446c73595b4d2d9360a25f816b5d42ee66b5393.1793524457.git.gitter.spiros@gmail.com> In-Reply-To: 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 --- 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