From: Phillip Wood <phillip.wood123@gmail.com>
To: Elia Pinto <gitter.spiros@gmail.com>, git@vger.kernel.org
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Junio C Hamano <gitster@pobox.com>,
Michael Montalbo <mmontalbo@gmail.com>
Subject: Re: [PATCH 2/3] diff: use size_t for the size passed to count_lines()
Date: Wed, 4 Nov 2026 15:45:47 +0000 [thread overview]
Message-ID: <0b3f8998-bb3d-4a91-822d-291562691a6f@gmail.com> (raw)
In-Reply-To: <0cb42395-3e62-42e0-bfd3-a4265ed7cc33@gmail.com>
On 04/11/2026 13:58, Phillip Wood wrote:
> On 01/11/2026 15:36, Elia Pinto wrote:
>> 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".
>
> That means the counter is subject to overflow as a file can contain as
> many lines as its size.
>
> I don't know what the simulation entailed, but this patch changes the
> return type without changing either the type of the returned value which
> can now overflow, or the types where the return values is stored by
> callers.
Sorry I seem to have misread the patch when I wrote that - the return
type is not changed. We're still left with the possibility of the count
overflowing now that "size" is a size_t though I think.
Phillip
> I'm concerned that the preparation of these patches and the others
> you've sent has relied too heavily on a flawed mechanical process, and
> have not enough on manual review reading the code and reasoning about it.
>
> Thanks
>
> Phillip
>
>
>> 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;
>
next prev parent reply other threads:[~2026-11-04 15:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 15:36 [PATCH 0/3] use size_t for a few sizes that are 32 bits everywhere Elia Pinto
2026-11-01 15:36 ` [PATCH 1/3] csum-file: use size_t for the count passed to hashwrite() Elia Pinto
2026-11-01 15:36 ` [PATCH 2/3] diff: use size_t for the size passed to count_lines() Elia Pinto
2026-11-04 13:58 ` Phillip Wood
2026-11-04 15:45 ` Phillip Wood [this message]
2026-11-01 15:36 ` [PATCH 3/3] diffcore-delta: use size_t for the size of the buffer to hash Elia Pinto
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0b3f8998-bb3d-4a91-822d-291562691a6f@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=gitter.spiros@gmail.com \
--cc=mmontalbo@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox