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

[PATCH 2/3] diff: use size_t for the size passed to count_lines()

From
Elia Pinto <gitter.spiros@gmail.com>
Date
Nov 1, 2026, 15:36 UTC
Message-ID
<2011b90d80c5a823878055fd1016bc38b6a072b7.1793546752.git.gitter.spiros@gmail.com>
In-Reply-To
<cover.1793546752.git.gitter.spiros@gmail.com>

count_lines() takes the size as an "int", whereas callers have "unsigned long" or "size_t". It is used for the diffstat of complete rewrites ("git diff -B --stat") and in emit_rewrite_diff().

With a file of 2GiB or more, the "int" becomes negative (or truncated above 4GiB), leading to an incorrect line count. This only affects the diffstat output, with no memory access issues.

This was verified using a 16-bit simulation, where "git diff -B --stat" reported 0 changed lines instead of the actual count.

Make the size parameter a size_t. The line counter remains "int".

Triggering this requires processing a file of 2GiB or more, so no test is added.

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..ed7a931c41 100644
--- a/diff.c
+++ b/diff.c
@@ -655,7 +655,7 @@ struct line_range_callback {
 	int ret;			/* latched error from orig_line_fn */
 };
 
-static int count_lines(const char *data, int size)
+static int count_lines(const char *data, size_t size)
 {
 	int count, ch, completely_empty = 1, nl_just_seen = 0;
 	count = 0;
-- 
2.53.0
Previous: Elia PintoNext: Elia Pinto
Message 3 of 4 in “use size_t for a few sizes that are 32 bits everywhere”
  1. 0/3 use size_t for a few sizes that are 32 bits everywhereElia Pinto, Nov 1, 2026
  2. 1/3 csum-file: use size_t for the count passed to hashwrite()Elia Pinto, Nov 1, 2026
  3. 2/3 diff: use size_t for the size passed to count_lines()Elia Pinto, Nov 1, 2026
  4. 3/3 diffcore-delta: use size_t for the size of the buffer to hashElia Pinto, Nov 1, 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.