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

[PATCH 6/6] diff: use size_t in count_trailing_blank()

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Oct 30, 2026, 10:28 UTC
Message-ID
<79f1781abdd196ac35ae888ea5470705c56eaa4a.1793355390.git.gitter.spiros@gmail.com>
In-Reply-To
<cover.1793355390.git.gitter.spiros@gmail.com>

Since 3c7a31204f (xdiff: use size_t for buffer sizes, 2026-09-30), mmfile_t stores its size as a size_t, but count_trailing_blank() still copies it into a "long". On platforms where "long" is 32 bits wide, like Windows, a buffer between 2GiB and 4GiB gives a negative size, and "ptr += size - 1" points about 2GiB before the start of the buffer, which we then dereference.

check_blank_at_eof() calls this function whenever the whitespace rule includes blank-at-eof, which is the default, and it does so before xdi_diff() rejects inputs larger than MAX_XDIFF_SIZE. Large files are normally treated as binary and never get here, but a "diff" attribute or a textconv driver makes us diff them as text. A repository can ship such an attribute in its .gitattributes, so "git show" or "git log -p" on a commit adding a 3GiB file can be made to crash.

Use size_t; the rest of the function works unchanged with it.

No test, as triggering this needs a platform with a 32-bit "long" and a blob larger than 2GiB.

Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 diff.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/diff.c b/diff.c
index b4ac17f8ef..e42e53d43e 100644
--- a/diff.c
+++ b/diff.c
@@ -711,7 +711,7 @@ static unsigned long diff_filespec_size(struct repository *r,
 static int count_trailing_blank(mmfile_t *mf)
 {
 	char *ptr = mf->ptr;
-	long size = mf->size;
+	size_t size = mf->size;
 	int cnt = 0;
 
 	if (!size)
-- 
2.53.0
Previous: Elia Pinto
Message 7 of 7 in “loose object size checks and a few size truncations”
  1. 0/6 loose object size checks and a few size truncationsElia Pinto, Oct 30, 2026
  2. 1/6 object-file: check size of inflated loose object against its headerElia Pinto, Oct 30, 2026
  3. 2/6 object-file: use size_t for loose object sizesElia Pinto, Oct 30, 2026
  4. 3/6 odb/source-loose: check object size when streaming loose objectsElia Pinto, Oct 30, 2026
  5. 4/6 cat-file: use size_t for the length passed to batch_write()Elia Pinto, Oct 30, 2026
  6. 5/6 notes: avoid truncating the size of concatenated notesElia Pinto, Oct 30, 2026
  7. 6/6 diff: use size_t in count_trailing_blank()Elia Pinto, Oct 30, 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.