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

[PATCH 2/2] diff: die instead of truncating sizes of 4GiB or more

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Nov 1, 2026, 08:44 UTC
Message-ID
<93415b30f2ffdafbd8ce0e615b1ef1aa8bb0ad7d.1793521916.git.gitter.spiros@gmail.com>
In-Reply-To
<cover.1793521916.git.gitter.spiros@gmail.com>

On platforms where "long" is 32 bits but size_t is 64 bits, such as 64-bit Windows, worktree files, textconv outputs, convert_to_git() results, and temporary diff files of 4GiB or larger have their sizes silently truncated when they are stored in an "unsigned long", such as the size field of struct diff_filespec.

This causes "git diff" to produce a silent incorrect diff based on a prefix, and commands like "git add -p" to apply a patch that truncates the file. With the worktree size truncated to 16 bits, "git diff --stat" reports 1 insertion and 5042 deletions for a one-line change in a 7901-line file, and applying the resulting patch leaves 2859 lines.

Use cast_size_t_to_ulong() in these code paths, matching the blob side, so that Git dies with a clear error instead of producing wrong results or corrupted patches. Widening diff_filespec.size is a much larger change left for a separate patch.

Triggering this requires processing 4GiB or more of input 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>
---
 combine-diff.c  | 11 +++++++----
 diff-no-index.c |  2 +-
 diff.c          |  8 ++++----
 3 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/combine-diff.c b/combine-diff.c
index 717d537688..edf0427a80 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -322,7 +322,8 @@ static char *grab_blob(struct repository *r,
 	} else if (textconv) {
 		struct diff_filespec *df = alloc_filespec(path);
 		fill_filespec(df, oid, 1, mode);
-		*size = fill_textconv(r, textconv, df, &blob);
+		*size = cast_size_t_to_ulong(fill_textconv(r, textconv,
+							   df, &blob));
 		free_filespec(df);
 	} else {
 		size_t size_st = 0;
@@ -1069,7 +1070,9 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 		} else if (textconv) {
 			struct diff_filespec *df = alloc_filespec(elem->path);
 			fill_filespec(df, null_oid(the_hash_algo), 0, st.st_mode);
-			result_size = fill_textconv(opt->repo, textconv, df, &result);
+			result_size = cast_size_t_to_ulong(
+				fill_textconv(opt->repo, textconv,
+					      df, &result));
 			free_filespec(df);
 		} else if (0 <= (fd = open(elem->path, O_RDONLY))) {
 			size_t len = xsize_t(st.st_size);
@@ -1086,7 +1089,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 			if (!is_file)
 				elem->mode = canon_mode(S_IFLNK);
 
-			result_size = len;
+			result_size = cast_size_t_to_ulong(len);
 			result = xmallocz(len);
 
 			done = read_in_full(fd, result, len);
@@ -1103,7 +1106,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
 						   elem->path, result, len, &buf, global_conv_flags_eol)) {
 					free(result);
 					result = strbuf_detach(&buf, &len);
-					result_size = len;
+					result_size = cast_size_t_to_ulong(len);
 				}
 			}
 		}
diff --git a/diff-no-index.c b/diff-no-index.c
index a9151cc27a..dee80eb4ed 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -116,7 +116,7 @@ static void populate_common(struct diff_filespec *s, struct strbuf *buf)
 
 	s->should_munmap = 0;
 	s->data = strbuf_detach(buf, &size);
-	s->size = size;
+	s->size = cast_size_t_to_ulong(size);
 	s->should_free = 1;
 	s->is_stdin = 1;
 }
diff --git a/diff.c b/diff.c
index b4ac17f8ef..9b1c69b378 100644
--- a/diff.c
+++ b/diff.c
@@ -4516,7 +4516,7 @@ int diff_populate_filespec(struct repository *r,
 			s->size = 0;
 			return err;
 		}
-		s->size = xsize_t(st.st_size);
+		s->size = cast_size_t_to_ulong(xsize_t(st.st_size));
 		if (!s->size)
 			goto empty;
 		if (S_ISLNK(st.st_mode)) {
@@ -4569,7 +4569,7 @@ int diff_populate_filespec(struct repository *r,
 			munmap(s->data, s->size);
 			s->should_munmap = 0;
 			s->data = strbuf_detach(&buf, &size);
-			s->size = size;
+			s->size = cast_size_t_to_ulong(size);
 			s->should_free = 1;
 		}
 	}
@@ -4662,7 +4662,7 @@ static void prep_temp_blob(struct index_state *istate,
 	if (convert_to_working_tree(istate, path,
 			(const char *)blob, (size_t)size, &buf, &meta)) {
 		blob = buf.buf;
-		size = buf.len;
+		size = cast_size_t_to_ulong(buf.len);
 	}
 	if (write_in_full(temp->tempfile->fd, blob, size) < 0 ||
 	    close_tempfile_gently(temp->tempfile))
@@ -7838,7 +7838,7 @@ int textconv_object(struct repository *r,
 		return 0;
 	}
 
-	*buf_size = fill_textconv(r, textconv, df, buf);
+	*buf_size = cast_size_t_to_ulong(fill_textconv(r, textconv, df, buf));
 	free_filespec(df);
 	return 1;
 }
-- 
2.53.0
Previous: Elia PintoNext: Elia Pinto
Message 3 of 4 in “grep, diff: do not silently truncate sizes of 4GiB or more”
  1. 0/2 grep, diff: do not silently truncate sizes of 4GiB or moreElia Pinto, Nov 1, 2026
  2. 1/2 grep: use size_t for the size of buffers to searchElia Pinto, Nov 1, 2026
  3. 2/2 diff: die instead of truncating sizes of 4GiB or moreElia Pinto, Nov 1, 2026
  4. 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.