* [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
@ 2026-10-23 3:56 Srujith A Poojary via GitGitGadget
2026-10-23 5:04 ` Junio C Hamano
` (8 more replies)
0 siblings, 9 replies; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-23 3:56 UTC (permalink / raw)
To: git; +Cc: Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
Replace manual multiplication and addition in create_delta_index()
with safe integer helpers (st_mult, st_add, st_add3) to prevent
overflows while preserving NULL returns on failure.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..45df070ab9 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
@ 2026-10-23 5:04 ` Junio C Hamano
2026-10-23 5:29 ` Srujith Poojary
2026-10-23 5:56 ` [PATCH v2] " Srujith A Poojary via GitGitGadget
` (7 subsequent siblings)
8 siblings, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-23 5:04 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget; +Cc: git, Srujith A Poojary
"Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
writes:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
> Replace manual multiplication and addition in create_delta_index()
> with safe integer helpers (st_mult, st_add, st_add3) to prevent
> overflows while preserving NULL returns on failure.
I do not quite understand what the last part of this explanation
wants to say, i.e., "while preserving NULL returns on failure". It
makes it sound as if the function would return NULL if the integer
helpers detect integer overflow, but I do not think that is what
actually the updated code does at all. st_add() and friends will
simply die() when overflow happens, so you will not even reach
malloc(), let alone return NULL when the malloc fails.
And if you reply to this review message, please do not top-post.
Thanks.
>
> Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
> ---
> [Outreachy PATCH] diff-delta: use safe integer arithmetic
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/2276
>
> diff-delta.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 7cbedeb507..45df070ab9 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> hmask = hsize - 1;
>
> /* allocate lookup index */
> - memsize = sizeof(*hash) * hsize +
> - sizeof(*entry) * entries;
> + memsize = st_add(st_mult(sizeof(*hash), hsize),
> + st_mult(sizeof(*entry), entries));
> mem = malloc(memsize);
> if (!mem)
> return NULL;
> @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> * Now create the packed index in array form
> * rather than linked lists.
> */
> - memsize = sizeof(*index)
> - + sizeof(*packed_hash) * (hsize+1)
> - + sizeof(*packed_entry) * entries;
> + memsize = st_add3(sizeof(*index),
> + st_mult(sizeof(*packed_hash), hsize + 1),
> + st_mult(sizeof(*packed_entry), entries));
> mem = malloc(memsize);
> if (!mem) {
> free(hash);
>
> base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
2026-10-23 5:04 ` Junio C Hamano
@ 2026-10-23 5:29 ` Srujith Poojary
2026-10-23 15:30 ` Junio C Hamano
0 siblings, 1 reply; 31+ messages in thread
From: Srujith Poojary @ 2026-10-23 5:29 UTC (permalink / raw)
To: Junio C Hamano, Usman Akinyemi; +Cc: Srujith A Poojary via GitGitGadget, git
On Fri, Oct 23, 2026 at 10:34 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> > Replace manual multiplication and addition in create_delta_index()
> > with safe integer helpers (st_mult, st_add, st_add3) to prevent
> > overflows while preserving NULL returns on failure.
>
> I do not quite understand what the last part of this explanation
> wants to say, i.e., "while preserving NULL returns on failure". It
> makes it sound as if the function would return NULL if the integer
> helpers detect integer overflow, but I do not think that is what
> actually the updated code does at all. st_add() and friends will
> simply die() when overflow happens, so you will not even reach
> malloc(), let alone return NULL when the malloc fails.
>
> And if you reply to this review message, please do not top-post.
>
> Thanks.
>
> >
> > Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
> > ---
> > [Outreachy PATCH] diff-delta: use safe integer arithmetic
> >
> > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v1
> > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v1
> > Pull-Request: https://github.com/gitgitgadget/git/pull/2276
> >
> > diff-delta.c | 10 +++++-----
> > 1 file changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/diff-delta.c b/diff-delta.c
> > index 7cbedeb507..45df070ab9 100644
> > --- a/diff-delta.c
> > +++ b/diff-delta.c
> > @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > hmask = hsize - 1;
> >
> > /* allocate lookup index */
> > - memsize = sizeof(*hash) * hsize +
> > - sizeof(*entry) * entries;
> > + memsize = st_add(st_mult(sizeof(*hash), hsize),
> > + st_mult(sizeof(*entry), entries));
> > mem = malloc(memsize);
> > if (!mem)
> > return NULL;
> > @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > * Now create the packed index in array form
> > * rather than linked lists.
> > */
> > - memsize = sizeof(*index)
> > - + sizeof(*packed_hash) * (hsize+1)
> > - + sizeof(*packed_entry) * entries;
> > + memsize = st_add3(sizeof(*index),
> > + st_mult(sizeof(*packed_hash), hsize + 1),
> > + st_mult(sizeof(*packed_entry), entries));
> > mem = malloc(memsize);
> > if (!mem) {
> > free(hash);
> >
> > base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
Hi Junio,
Thank you for clarifying this. You are right—st_add() and st_mult()
call die() on overflow rather than returning NULL.
I will update the commit message to accurately reflect this behavior
and submit v2.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v2] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
2026-10-23 5:04 ` Junio C Hamano
@ 2026-10-23 5:56 ` Srujith A Poojary via GitGitGadget
2026-10-23 15:15 ` Phillip Wood
2026-10-24 3:48 ` [PATCH v3] " Srujith A Poojary via GitGitGadget
` (6 subsequent siblings)
8 siblings, 1 reply; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-23 5:56 UTC (permalink / raw)
To: git; +Cc: Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
Replace manual multiplication and addition in create_delta_index()
with safe integer helpers (st_mult, st_add, st_add3) to prevent
overflows.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v1:
1: a0aafe63a7 ! 1: 14ffd630ca [Outreachy PATCH] diff-delta: use safe integer arithmetic
@@ Metadata
Author: Srujith A Poojary <poojarysrujith@gmail.com>
## Commit message ##
- [Outreachy PATCH] diff-delta: use safe integer arithmetic
+ diff-delta: use safe integer arithmetic
Replace manual multiplication and addition in create_delta_index()
with safe integer helpers (st_mult, st_add, st_add3) to prevent
- overflows while preserving NULL returns on failure.
+ overflows.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..45df070ab9 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v2] diff-delta: use safe integer arithmetic
2026-10-23 5:56 ` [PATCH v2] " Srujith A Poojary via GitGitGadget
@ 2026-10-23 15:15 ` Phillip Wood
2026-10-23 18:49 ` Junio C Hamano
0 siblings, 1 reply; 31+ messages in thread
From: Phillip Wood @ 2026-10-23 15:15 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget, git; +Cc: Srujith A Poojary
Hi Srujith
On 23/10/2026 06:56, Srujith A Poojary via GitGitGadget wrote:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
> Replace manual multiplication and addition in create_delta_index()
> with safe integer helpers (st_mult, st_add, st_add3) to prevent
> overflows.
Sounds like a good idea
> diff --git a/diff-delta.c b/diff-delta.c
> index 7cbedeb507..45df070ab9 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> hmask = hsize - 1;
>
> /* allocate lookup index */
> - memsize = sizeof(*hash) * hsize +
> - sizeof(*entry) * entries;
> + memsize = st_add(st_mult(sizeof(*hash), hsize),
> + st_mult(sizeof(*entry), entries));
The alignment is off here, see
https://github.com/gitgitgadget/git/actions/runs/39906906057/job/120260845179?pr=2276
for a suggested fix from clang-format. I think you should be able to use
"git clang-format HEAD^" to fix it.
> mem = malloc(memsize);
> if (!mem)
> return NULL;
> @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> * Now create the packed index in array form
> * rather than linked lists.
> */
> - memsize = sizeof(*index)
> - + sizeof(*packed_hash) * (hsize+1)
> - + sizeof(*packed_entry) * entries;
> + memsize = st_add3(sizeof(*index),
> + st_mult(sizeof(*packed_hash), hsize + 1),
What are the bounds of hsize? Can adding one to it overflow? The
alignment is off here as well, all the arguments to st_add3() should be
aligned.
Thanks
Phillip
> + st_mult(sizeof(*packed_entry), entries));
> mem = malloc(memsize);
> if (!mem) {
> free(hash);
>
> base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
2026-10-23 5:29 ` Srujith Poojary
@ 2026-10-23 15:30 ` Junio C Hamano
2026-10-23 16:56 ` Srujith Poojary
0 siblings, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-23 15:30 UTC (permalink / raw)
To: Srujith Poojary; +Cc: Usman Akinyemi, Srujith A Poojary via GitGitGadget, git
Srujith Poojary <poojarysrujith@gmail.com> writes:
> Thank you for clarifying this. You are right—st_add() and st_mult()
> call die() on overflow rather than returning NULL.
> I will update the commit message to accurately reflect this behavior
> and submit v2.
Please don't, before making sure if it can overflow. With the
number of entries capped like this:
if (bufsize >= 0xffffffffUL)
entries = 0xfffffffeU / RABIN_WINDOW;
and then computed
hsize = entries / 4;
for (i = 4; (1u << i) < hsize; i++);
hsize = 1 << i;
/* allocate lookup index */
memsize = sizeof(*hash) * hsize +
sizeof(*entry) * entries;
it may not be possible to overflow size_t even on 32-bit platforms.
I didn't have time to do math yet myself, though.
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
2026-10-23 15:30 ` Junio C Hamano
@ 2026-10-23 16:56 ` Srujith Poojary
2026-10-23 18:45 ` Junio C Hamano
0 siblings, 1 reply; 31+ messages in thread
From: Srujith Poojary @ 2026-10-23 16:56 UTC (permalink / raw)
To: Junio C Hamano, phillip.wood123
Cc: Usman Akinyemi, Srujith A Poojary via GitGitGadget, git
On Fri, Oct 23, 2026 at 9:00 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Srujith Poojary <poojarysrujith@gmail.com> writes:
>
> > Thank you for clarifying this. You are right—st_add() and st_mult()
> > call die() on overflow rather than returning NULL.
> > I will update the commit message to accurately reflect this behavior
> > and submit v2.
>
> Please don't, before making sure if it can overflow. With the
> number of entries capped like this:
>
> if (bufsize >= 0xffffffffUL)
> entries = 0xfffffffeU / RABIN_WINDOW;
>
> and then computed
>
> hsize = entries / 4;
> for (i = 4; (1u << i) < hsize; i++);
> hsize = 1 << i;
> /* allocate lookup index */
> memsize = sizeof(*hash) * hsize +
> sizeof(*entry) * entries;
>
> it may not be possible to overflow size_t even on 32-bit platforms.
> I didn't have time to do math yet myself, though.
>
Hi Phillip and Junio,
Thank you for the detailed feedback.
I checked the bounds: since entries is capped by (0xffffffu /
RABIN_WINDOW), hsize remains small, making a size_t overflow
impossible even on 32-bit systems.
I ran git clang-format HEAD^ to fix the code alignment as suggested
and updated the patch.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic
2026-10-23 16:56 ` Srujith Poojary
@ 2026-10-23 18:45 ` Junio C Hamano
0 siblings, 0 replies; 31+ messages in thread
From: Junio C Hamano @ 2026-10-23 18:45 UTC (permalink / raw)
To: Srujith Poojary
Cc: phillip.wood123, Usman Akinyemi,
Srujith A Poojary via GitGitGadget, git
Srujith Poojary <poojarysrujith@gmail.com> writes:
> On Fri, Oct 23, 2026 at 9:00 PM Junio C Hamano <gitster@pobox.com> wrote:
>>
>> Srujith Poojary <poojarysrujith@gmail.com> writes:
>>
>> > Thank you for clarifying this. You are right—st_add() and st_mult()
>> > call die() on overflow rather than returning NULL.
>> > I will update the commit message to accurately reflect this behavior
>> > and submit v2.
>>
>> Please don't, before making sure if it can overflow. With the
>> number of entries capped like this:
>>
>> if (bufsize >= 0xffffffffUL)
>> entries = 0xfffffffeU / RABIN_WINDOW;
>>
>> and then computed
>>
>> hsize = entries / 4;
>> for (i = 4; (1u << i) < hsize; i++);
>> hsize = 1 << i;
>> /* allocate lookup index */
>> memsize = sizeof(*hash) * hsize +
>> sizeof(*entry) * entries;
>>
>> it may not be possible to overflow size_t even on 32-bit platforms.
>> I didn't have time to do math yet myself, though.
>>
>
> Hi Phillip and Junio,
>
> Thank you for the detailed feedback.
>
> I checked the bounds: since entries is capped by (0xffffffu /
> RABIN_WINDOW), hsize remains small, making a size_t overflow
> impossible even on 32-bit systems.
Hmph, I actually came to a different conclusion, but my math is
probably weaker X-<.
entries is capped by 1^32 / 16 = 1^28, and hsize is capped at 1^26.
sizeof(*hash) is 4 bytes on 32-bit platforms.
struct index_entry has a pointer and an unsigned, i.e. 8 bytes.
struct unpacked_index_entry has the above index_entry plus a
pointer, i.e. 8+4=12 bytes or 16 bytes (with 4-byte padding).
That is the sizeof(*entry).
If "struct unpacked_index_entry" has 4-byte padding to align at
8-byte boundary, then memsize = 4 * 1^26 + 16 * 1^28 can overflow
size_t's range that is 1^32. If the struct does not have extra
padding, this would fit below 1^32. We have no guarantee what the
compiler would do, so we need to assume the worst.
Of course it is entirely a different matter if malloc() would allow
such a large allocation even if math does not overflow, but the
focus of this patch is to make sure memsize is computed as a large
allocation in such a case without wrapping around.
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v2] diff-delta: use safe integer arithmetic
2026-10-23 15:15 ` Phillip Wood
@ 2026-10-23 18:49 ` Junio C Hamano
2026-10-24 3:44 ` Srujith Poojary
0 siblings, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-23 18:49 UTC (permalink / raw)
To: Phillip Wood; +Cc: Srujith A Poojary via GitGitGadget, git, Srujith A Poojary
Phillip Wood <phillip.wood123@gmail.com> writes:
>> + memsize = st_add3(sizeof(*index),
>> + st_mult(sizeof(*packed_hash), hsize + 1),
>
> What are the bounds of hsize? Can adding one to it overflow? The
> alignment is off here as well, all the arguments to st_add3() should be
> aligned.
Very good question, and I think hsize is capped at 1^26 in a code
early in this function, which makes this safe. It needs to be
explained why it is safe in the log message, though.
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v2] diff-delta: use safe integer arithmetic
2026-10-23 18:49 ` Junio C Hamano
@ 2026-10-24 3:44 ` Srujith Poojary
0 siblings, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-24 3:44 UTC (permalink / raw)
To: Junio C Hamano
Cc: Phillip Wood, Srujith A Poojary via GitGitGadget, git,
Usman Akinyemi
On Sat, Oct 24, 2026 at 12:19 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
> >> + memsize = st_add3(sizeof(*index),
> >> + st_mult(sizeof(*packed_hash), hsize + 1),
> >
> > What are the bounds of hsize? Can adding one to it overflow? The
> > alignment is off here as well, all the arguments to st_add3() should be
> > aligned.
>
> Very good question, and I think hsize is capped at 1^26 in a code
> early in this function, which makes this safe. It needs to be
> explained why it is safe in the log message, though.
Hi Junio,
Thank you for analyzing the struct alignment and memory allocation limits.
I have updated the commit message to explain why st_mult/st_add are
necessary on 32-bit platforms due to padding, as well as documenting
that hsize + 1 is safe because hsize is capped at 2^26.
I also fixed the code formatting using git clang-format HEAD^.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v3] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
2026-10-23 5:04 ` Junio C Hamano
2026-10-23 5:56 ` [PATCH v2] " Srujith A Poojary via GitGitGadget
@ 2026-10-24 3:48 ` Srujith A Poojary via GitGitGadget
2026-10-24 16:15 ` Junio C Hamano
2026-10-24 18:00 ` [PATCH v4] " Srujith A Poojary via GitGitGadget
` (5 subsequent siblings)
8 siblings, 1 reply; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-24 3:48 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
Replace manual multiplication and addition in create_delta_index()
with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
overflows when computing memory allocation sizes.
On 32-bit platforms, struct alignment padding could cause the total
allocation size calculation to exceed 2^32, making safe integer arithmetic
necessary. Note that hsize + 1 inside st_add3() is safe from overflow
because hsize is capped at 2^26 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v2:
1: 14ffd630ca ! 1: d288bfe9e3 diff-delta: use safe integer arithmetic
@@ Commit message
diff-delta: use safe integer arithmetic
Replace manual multiplication and addition in create_delta_index()
- with safe integer helpers (st_mult, st_add, st_add3) to prevent
- overflows.
+ with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
+ overflows when computing memory allocation sizes.
+
+ On 32-bit platforms, struct alignment padding could cause the total
+ allocation size calculation to exceed 2^32, making safe integer arithmetic
+ necessary. Note that hsize + 1 inside st_add3() is safe from overflow
+ because hsize is capped at 2^26 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
@@ diff-delta.c: struct delta_index * create_delta_index(const void *buf, size_t bu
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
-+ st_mult(sizeof(*entry), entries));
++ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ diff-delta.c: struct delta_index * create_delta_index(const void *buf, size_t bu
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
-+ st_mult(sizeof(*packed_hash), hsize + 1),
-+ st_mult(sizeof(*packed_entry), entries));
++ st_mult(sizeof(*packed_hash), hsize + 1),
++ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v3] diff-delta: use safe integer arithmetic
2026-10-24 3:48 ` [PATCH v3] " Srujith A Poojary via GitGitGadget
@ 2026-10-24 16:15 ` Junio C Hamano
2026-10-24 17:26 ` Srujith Poojary
0 siblings, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-24 16:15 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget; +Cc: git, Phillip Wood, Srujith A Poojary
"Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
writes:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
> Replace manual multiplication and addition in create_delta_index()
> with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
> overflows when computing memory allocation sizes.
>
> On 32-bit platforms, struct alignment padding could cause the total
> allocation size calculation to exceed 2^32, making safe integer arithmetic
> necessary.
I wonder if "X could cause Y" above skips a bit too many steps. I
was expecting more along the lines of ...
We will need to allocate N instances of A and M instances of B, N
and M are capped to X and Y respectively by this code
... code snippet ...
and size of an instance of A is 8 and size of B could be either W or
Z. When size of B is W, then the total allocation would be ...
And that analysis applies only on the first allocation. I didn't
even look at the packed side of the allocation size, expecting that
you would know how to analyze after seeing my example on the other
allocation.
> Note that hsize + 1 inside st_add3() is safe from overflow
> because hsize is capped at 2^26 early in the function.
It is misleading to say hsize+1 is inside st_add3(), though.
> diff --git a/diff-delta.c b/diff-delta.c
> index 7cbedeb507..efebce3aca 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> hmask = hsize - 1;
>
> /* allocate lookup index */
> - memsize = sizeof(*hash) * hsize +
> - sizeof(*entry) * entries;
> + memsize = st_add(st_mult(sizeof(*hash), hsize),
> + st_mult(sizeof(*entry), entries));
> mem = malloc(memsize);
> if (!mem)
> return NULL;
> @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> * Now create the packed index in array form
> * rather than linked lists.
> */
> - memsize = sizeof(*index)
> - + sizeof(*packed_hash) * (hsize+1)
> - + sizeof(*packed_entry) * entries;
> + memsize = st_add3(sizeof(*index),
> + st_mult(sizeof(*packed_hash), hsize + 1),
> + st_mult(sizeof(*packed_entry), entries));
> mem = malloc(memsize);
> if (!mem) {
> free(hash);
>
> base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v3] diff-delta: use safe integer arithmetic
2026-10-24 16:15 ` Junio C Hamano
@ 2026-10-24 17:26 ` Srujith Poojary
2026-10-25 18:10 ` Junio C Hamano
0 siblings, 1 reply; 31+ messages in thread
From: Srujith Poojary @ 2026-10-24 17:26 UTC (permalink / raw)
To: Junio C Hamano
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi
On Sat, Oct 24, 2026 at 9:45 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> > Replace manual multiplication and addition in create_delta_index()
> > with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
> > overflows when computing memory allocation sizes.
> >
> > On 32-bit platforms, struct alignment padding could cause the total
> > allocation size calculation to exceed 2^32, making safe integer arithmetic
> > necessary.
>
> I wonder if "X could cause Y" above skips a bit too many steps. I
> was expecting more along the lines of ...
>
> We will need to allocate N instances of A and M instances of B, N
> and M are capped to X and Y respectively by this code
>
> ... code snippet ...
>
> and size of an instance of A is 8 and size of B could be either W or
> Z. When size of B is W, then the total allocation would be ...
>
> And that analysis applies only on the first allocation. I didn't
> even look at the packed side of the allocation size, expecting that
> you would know how to analyze after seeing my example on the other
> allocation.
>
> > Note that hsize + 1 inside st_add3() is safe from overflow
> > because hsize is capped at 2^26 early in the function.
>
> It is misleading to say hsize+1 is inside st_add3(), though.
>
>
> > diff --git a/diff-delta.c b/diff-delta.c
> > index 7cbedeb507..efebce3aca 100644
> > --- a/diff-delta.c
> > +++ b/diff-delta.c
> > @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > hmask = hsize - 1;
> >
> > /* allocate lookup index */
> > - memsize = sizeof(*hash) * hsize +
> > - sizeof(*entry) * entries;
> > + memsize = st_add(st_mult(sizeof(*hash), hsize),
> > + st_mult(sizeof(*entry), entries));
> > mem = malloc(memsize);
> > if (!mem)
> > return NULL;
> > @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > * Now create the packed index in array form
> > * rather than linked lists.
> > */
> > - memsize = sizeof(*index)
> > - + sizeof(*packed_hash) * (hsize+1)
> > - + sizeof(*packed_entry) * entries;
> > + memsize = st_add3(sizeof(*index),
> > + st_mult(sizeof(*packed_hash), hsize + 1),
> > + st_mult(sizeof(*packed_entry), entries));
> > mem = malloc(memsize);
> > if (!mem) {
> > free(hash);
> >
> > base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
Hi Junio,
Thank you for the detailed correction.
I have expanded the commit message to include the step-by-step
breakdown for both the unpacked and packed index allocations,
detailing the caps on hsize (2^26) and entries (2^28) alongside struct
padding on 32-bit systems. I also corrected the phrasing regarding
(hsize + 1) being passed as an argument to st_add3().
I have updated the patch series accordingly.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v4] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (2 preceding siblings ...)
2026-10-24 3:48 ` [PATCH v3] " Srujith A Poojary via GitGitGadget
@ 2026-10-24 18:00 ` Srujith A Poojary via GitGitGadget
2026-10-25 19:20 ` [PATCH v5] " Srujith A Poojary via GitGitGadget
` (4 subsequent siblings)
8 siblings, 0 replies; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-24 18:00 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
Replace manual multiplication and addition in create_delta_index()
with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
overflows when computing memory allocation sizes.
For the unpacked index, we allocate hsize instances of hash pointers and
entries instances of struct index_entry:
- hsize is capped at 2^26.
- entries is capped at 2^28.
On 32-bit platforms with 4-byte padding for struct unpacked_index_entry,
the calculation size (sizeof(*hash) * hsize + sizeof(*entry) * entries) can
exceed 2^32, making safe integer arithmetic necessary.
Similarly, for the packed index, we compute allocation size across
struct index, packed_hash, and entries. Using safe helpers prevents
wrap-around across both allocations.
Note that passing (hsize + 1) as an argument to st_add3() is safe from
overflow because hsize is capped at 2^26 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v3:
1: d288bfe9e3 ! 1: 0d5a729cde diff-delta: use safe integer arithmetic
@@ Commit message
with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
overflows when computing memory allocation sizes.
- On 32-bit platforms, struct alignment padding could cause the total
- allocation size calculation to exceed 2^32, making safe integer arithmetic
- necessary. Note that hsize + 1 inside st_add3() is safe from overflow
- because hsize is capped at 2^26 early in the function.
+ For the unpacked index, we allocate hsize instances of hash pointers and
+ entries instances of struct index_entry:
+ - hsize is capped at 2^26.
+ - entries is capped at 2^28.
+ On 32-bit platforms with 4-byte padding for struct unpacked_index_entry,
+ the calculation size (sizeof(*hash) * hsize + sizeof(*entry) * entries) can
+ exceed 2^32, making safe integer arithmetic necessary.
+
+ Similarly, for the packed index, we compute allocation size across
+ struct index, packed_hash, and entries. Using safe helpers prevents
+ wrap-around across both allocations.
+
+ Note that passing (hsize + 1) as an argument to st_add3() is safe from
+ overflow because hsize is capped at 2^26 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v3] diff-delta: use safe integer arithmetic
2026-10-24 17:26 ` Srujith Poojary
@ 2026-10-25 18:10 ` Junio C Hamano
2026-10-25 19:21 ` Srujith Poojary
0 siblings, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-25 18:10 UTC (permalink / raw)
To: Srujith Poojary
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi
Srujith Poojary <poojarysrujith@gmail.com> writes:
> Thank you for the detailed correction.
>
> I have expanded the commit message to include the step-by-step
> breakdown for both the unpacked and packed index allocations,
> detailing the caps on hsize (2^26) and entries (2^28) alongside struct
> padding on 32-bit systems. I also corrected the phrasing regarding
> (hsize + 1) being passed as an argument to st_add3().
It seems that the text of v4 did not "detail" anything like that;
you just parroted the conclusion, the final numbers I gave you,
without giving any hint for the readers to validate the math. The
readers who are interested in validating are forced to figure out
how many bytes your calculation assumes each struct type involved
consume---and if they find that your conclusion is wrong, they have
no clue where in your computation things went wrong.
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v5] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (3 preceding siblings ...)
2026-10-24 18:00 ` [PATCH v4] " Srujith A Poojary via GitGitGadget
@ 2026-10-25 19:20 ` Srujith A Poojary via GitGitGadget
2026-10-26 17:37 ` Junio C Hamano
2026-10-27 4:51 ` [PATCH v6] " Srujith A Poojary via GitGitGadget
` (3 subsequent siblings)
8 siblings, 1 reply; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-25 19:20 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
In create_delta_index(), buffer sizes for allocating the delta index
are calculated using arithmetic operations on entries and hsize:
memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
To prevent potential size_t wrap-around when computing allocation sizes
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
which may be padded to 16 bytes depending on compiler alignment rules.
- sizeof(struct index_entry *) is 4 bytes.
- entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
limiting entries to 1,048,575.
- hsize is the smallest power of 2 greater than or equal to entries,
capped at 1,048,576.
While entries is bounded in current code, using st_mult() and st_add()
guarantees that any unexpected growth in struct sizes or bounds changes
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- Passing (hsize + 1) as an argument to st_add3() is safe from overflow
because hsize is capped at 1,048,576 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v4:
1: 0d5a729cde ! 1: 23fcfd3227 diff-delta: use safe integer arithmetic
@@ Metadata
## Commit message ##
diff-delta: use safe integer arithmetic
- Replace manual multiplication and addition in create_delta_index()
- with safe integer helpers (st_mult, st_add, st_add3) to prevent potential
- overflows when computing memory allocation sizes.
-
- For the unpacked index, we allocate hsize instances of hash pointers and
- entries instances of struct index_entry:
- - hsize is capped at 2^26.
- - entries is capped at 2^28.
- On 32-bit platforms with 4-byte padding for struct unpacked_index_entry,
- the calculation size (sizeof(*hash) * hsize + sizeof(*entry) * entries) can
- exceed 2^32, making safe integer arithmetic necessary.
-
- Similarly, for the packed index, we compute allocation size across
- struct index, packed_hash, and entries. Using safe helpers prevents
- wrap-around across both allocations.
-
- Note that passing (hsize + 1) as an argument to st_add3() is safe from
- overflow because hsize is capped at 2^26 early in the function.
+ In create_delta_index(), buffer sizes for allocating the delta index
+ are calculated using arithmetic operations on entries and hsize:
+
+ memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
+
+ To prevent potential size_t wrap-around when computing allocation sizes
+ on 32-bit systems, replace manual arithmetic with overflow-checked
+ helpers (st_mult, st_add, st_add3).
+
+ For the unpacked index allocation:
+ - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
+ which may be padded to 16 bytes depending on compiler alignment rules.
+ - sizeof(struct index_entry *) is 4 bytes.
+ - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
+ limiting entries to 1,048,575.
+ - hsize is the smallest power of 2 greater than or equal to entries,
+ capped at 1,048,576.
+
+ While entries is bounded in current code, using st_mult() and st_add()
+ guarantees that any unexpected growth in struct sizes or bounds changes
+ will safely die() on size_t overflow instead of wrapping around.
+
+ For the packed index allocation:
+ - Passing (hsize + 1) as an argument to st_add3() is safe from overflow
+ because hsize is capped at 1,048,576 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v3] diff-delta: use safe integer arithmetic
2026-10-25 18:10 ` Junio C Hamano
@ 2026-10-25 19:21 ` Srujith Poojary
0 siblings, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-25 19:21 UTC (permalink / raw)
To: Junio C Hamano
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi
On Sun, Oct 25, 2026 at 11:40 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Srujith Poojary <poojarysrujith@gmail.com> writes:
>
> > Thank you for the detailed correction.
> >
> > I have expanded the commit message to include the step-by-step
> > breakdown for both the unpacked and packed index allocations,
> > detailing the caps on hsize (2^26) and entries (2^28) alongside struct
> > padding on 32-bit systems. I also corrected the phrasing regarding
> > (hsize + 1) being passed as an argument to st_add3().
>
> It seems that the text of v4 did not "detail" anything like that;
> you just parroted the conclusion, the final numbers I gave you,
> without giving any hint for the readers to validate the math. The
> readers who are interested in validating are forced to figure out
> how many bytes your calculation assumes each struct type involved
> consume---and if they find that your conclusion is wrong, they have
> no clue where in your computation things went wrong.
>
Hi Junio,
Thank you for pointing that out. You are completely right—I should
have explicitly detailed the struct sizes and exact code bounds
instead of quoting summary numbers.
I have updated the commit message in v5 to explicitly state:
- The byte sizes of each struct on 32-bit platforms (12/16 bytes for
unpacked_index_entry, 4 bytes for hash pointers).
- The exact origin of the bounds from code: entries capped by
(0xffffffu / RABIN_WINDOW = 1,048,575) and hsize capped at 1,048,576.
- Why st_mult/st_add/st_add3 are used to guard against size_t wrap-around.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v5] diff-delta: use safe integer arithmetic
2026-10-25 19:20 ` [PATCH v5] " Srujith A Poojary via GitGitGadget
@ 2026-10-26 17:37 ` Junio C Hamano
2026-10-26 18:47 ` Srujith Poojary
2026-10-27 4:50 ` Srujith Poojary
0 siblings, 2 replies; 31+ messages in thread
From: Junio C Hamano @ 2026-10-26 17:37 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget; +Cc: git, Phillip Wood, Srujith A Poojary
"Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
writes:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
> In create_delta_index(), buffer sizes for allocating the delta index
> are calculated using arithmetic operations on entries and hsize:
>
> memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
>
> To prevent potential size_t wrap-around when computing allocation sizes
> on 32-bit systems, replace manual arithmetic with overflow-checked
> helpers (st_mult, st_add, st_add3).
>
> For the unpacked index allocation:
> - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> which may be padded to 16 bytes depending on compiler alignment rules.
> - sizeof(struct index_entry *) is 4 bytes.
> - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> limiting entries to 1,048,575.
> - hsize is the smallest power of 2 greater than or equal to entries,
> capped at 1,048,576.
>
> While entries is bounded in current code, using st_mult() and st_add()
> guarantees that any unexpected growth in struct sizes or bounds changes
> will safely die() on size_t overflow instead of wrapping around.
Much more useful for future developers to learn what led to the
conclusion.
> For the packed index allocation:
> - Passing (hsize + 1) as an argument to st_add3() is safe from overflow
> because hsize is capped at 1,048,576 early in the function.
hsize+1 is *not* an argument to st_add3(), though. It is given to
st_mult().
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v5] diff-delta: use safe integer arithmetic
2026-10-26 17:37 ` Junio C Hamano
@ 2026-10-26 18:47 ` Srujith Poojary
2026-10-27 4:50 ` Srujith Poojary
1 sibling, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-26 18:47 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood
On Mon, Oct 26, 2026 at 11:07 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> > In create_delta_index(), buffer sizes for allocating the delta index
> > are calculated using arithmetic operations on entries and hsize:
> >
> > memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
> >
> > To prevent potential size_t wrap-around when computing allocation sizes
> > on 32-bit systems, replace manual arithmetic with overflow-checked
> > helpers (st_mult, st_add, st_add3).
> >
> > For the unpacked index allocation:
> > - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> > which may be padded to 16 bytes depending on compiler alignment rules.
> > - sizeof(struct index_entry *) is 4 bytes.
> > - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> > limiting entries to 1,048,575.
> > - hsize is the smallest power of 2 greater than or equal to entries,
> > capped at 1,048,576.
> >
> > While entries is bounded in current code, using st_mult() and st_add()
> > guarantees that any unexpected growth in struct sizes or bounds changes
> > will safely die() on size_t overflow instead of wrapping around.
>
> Much more useful for future developers to learn what led to the
> conclusion.
>
> > For the packed index allocation:
> > - Passing (hsize + 1) as an argument to st_add3() is safe from overflow
> > because hsize is capped at 1,048,576 early in the function.
>
> hsize+1 is *not* an argument to st_add3(), though. It is given to
> st_mult().
Hi Junio,
Thank you! Noted on (hsize + 1) being given to st_mult() inside
st_add3(). Glad the expanded breakdown is helpful for future
reference.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v5] diff-delta: use safe integer arithmetic
2026-10-26 17:37 ` Junio C Hamano
2026-10-26 18:47 ` Srujith Poojary
@ 2026-10-27 4:50 ` Srujith Poojary
1 sibling, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-27 4:50 UTC (permalink / raw)
To: Junio C Hamano
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi
On Mon, Oct 26, 2026 at 11:07 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Srujith A Poojary via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> > In create_delta_index(), buffer sizes for allocating the delta index
> > are calculated using arithmetic operations on entries and hsize:
> >
> > memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
> >
> > To prevent potential size_t wrap-around when computing allocation sizes
> > on 32-bit systems, replace manual arithmetic with overflow-checked
> > helpers (st_mult, st_add, st_add3).
> >
> > For the unpacked index allocation:
> > - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> > which may be padded to 16 bytes depending on compiler alignment rules.
> > - sizeof(struct index_entry *) is 4 bytes.
> > - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> > limiting entries to 1,048,575.
> > - hsize is the smallest power of 2 greater than or equal to entries,
> > capped at 1,048,576.
> >
> > While entries is bounded in current code, using st_mult() and st_add()
> > guarantees that any unexpected growth in struct sizes or bounds changes
> > will safely die() on size_t overflow instead of wrapping around.
>
> Much more useful for future developers to learn what led to the
> conclusion.
>
> > For the packed index allocation:
> > - Passing (hsize + 1) as an argument to st_add3() is safe from overflow
> > because hsize is capped at 1,048,576 early in the function.
>
> hsize+1 is *not* an argument to st_add3(), though. It is given to
> st_mult().
Hi Junio,
Thank you! I have updated the commit message to correctly note that
(hsize + 1) is given to st_mult(), which is evaluated inside
st_add3().
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v6] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (4 preceding siblings ...)
2026-10-25 19:20 ` [PATCH v5] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 4:51 ` Srujith A Poojary via GitGitGadget
2026-10-27 10:33 ` Pablo Sabater
2026-10-27 11:00 ` [PATCH v7] " Srujith A Poojary via GitGitGadget
` (2 subsequent siblings)
8 siblings, 1 reply; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-27 4:51 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
In create_delta_index(), buffer sizes for allocating the delta index
are calculated using arithmetic operations on entries and hsize:
memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
To prevent potential size_t wrap-around when computing allocation sizes
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
which may be padded to 16 bytes depending on compiler alignment rules.
- sizeof(struct index_entry *) is 4 bytes.
- entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
limiting entries to 1,048,575.
- hsize is the smallest power of 2 greater than or equal to entries,
capped at 1,048,576.
While entries is bounded in current code, using st_mult() and st_add()
guarantees that any unexpected growth in struct sizes or bounds changes
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
inside st_add3(), and is safe from overflow because hsize is capped at
1,048,576 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v6
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v5:
1: 23fcfd3227 ! 1: b91a1ed54b diff-delta: use safe integer arithmetic
@@ Commit message
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- - Passing (hsize + 1) as an argument to st_add3() is safe from overflow
- because hsize is capped at 1,048,576 early in the function.
+ - (hsize + 1) is passed to st_mult(), which is evaluated as an argument
+ inside st_add3(), and is safe from overflow because hsize is capped at
+ 1,048,576 early in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v6] diff-delta: use safe integer arithmetic
2026-10-27 4:51 ` [PATCH v6] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 10:33 ` Pablo Sabater
2026-10-27 11:01 ` Srujith Poojary
0 siblings, 1 reply; 31+ messages in thread
From: Pablo Sabater @ 2026-10-27 10:33 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget, git; +Cc: Phillip Wood, Srujith A Poojary
On Tue Oct 27, 2026 at 4:51 AM WET, Srujith A Poojary via GitGitGadget wrote:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
[snip]
>
> For the unpacked index allocation:
> - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> which may be padded to 16 bytes depending on compiler alignment rules.
l 117-125 'diff-delta.c'
struct index_entry {
const unsigned char *ptr;
unsigned int val;
};
struct unpacked_index_entry {
struct index_entry entry;
struct unpacked_index_entry *next;
};
On 32 bit systems we have 4 bytes ptr + 4 bytes uint + 4 bytes ptr. I
don't see how it could be padded to 16.
> - sizeof(struct index_entry *) is 4 bytes.
> - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> limiting entries to 1,048,575.
l 148-158 'diff-delta.c'
/* Determine index hash size. Note that indexing skips the
first byte to allow for optimizing the Rabin's polynomial
initialization in create_delta(). */
entries = (bufsize - 1) / RABIN_WINDOW;
if (bufsize >= 0xffffffffUL) {
/*
* Current delta format can't encode offsets into
* reference buffer with more than 32 bits.
*/
entries = 0xfffffffeU / RABIN_WINDOW;
}
entries are capped by 0xfffffffeU so the real limit is:
268 435 455 (256 times more)
> - hsize is the smallest power of 2 greater than or equal to entries,
> capped at 1,048,576.
After the code above we find this:
l 159-162 'diff-delta.c'
hsize = entries / 4;
for (i = 4; (1u << i) < hsize; i++);
hsize = 1 << i;
hmask = hsize - 1;
What this does is set hsize to the smallest power of 2 >= entries / 4
(with a minimum of 16), so it's "capped" at 67 108 864 (2^26).
>
[snip]
>
> diff-delta.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/diff-delta.c b/diff-delta.c
> index 7cbedeb507..efebce3aca 100644
> --- a/diff-delta.c
> +++ b/diff-delta.c
> @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> hmask = hsize - 1;
>
> /* allocate lookup index */
> - memsize = sizeof(*hash) * hsize +
> - sizeof(*entry) * entries;
> + memsize = st_add(st_mult(sizeof(*hash), hsize),
> + st_mult(sizeof(*entry), entries));
> mem = malloc(memsize);
> if (!mem)
> return NULL;
> @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> * Now create the packed index in array form
> * rather than linked lists.
> */
> - memsize = sizeof(*index)
> - + sizeof(*packed_hash) * (hsize+1)
> - + sizeof(*packed_entry) * entries;
> + memsize = st_add3(sizeof(*index),
> + st_mult(sizeof(*packed_hash), hsize + 1),
> + st_mult(sizeof(*packed_entry), entries));
> mem = malloc(memsize);
> if (!mem) {
> free(hash);
>
> base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
The code looks good.
Good job ;),
Pablo
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v7] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (5 preceding siblings ...)
2026-10-27 4:51 ` [PATCH v6] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 11:00 ` Srujith A Poojary via GitGitGadget
2026-10-27 11:52 ` Pablo Sabater
2026-10-27 14:21 ` [PATCH v8] " Srujith A Poojary via GitGitGadget
2026-10-27 16:56 ` [PATCH v9] " Srujith A Poojary via GitGitGadget
8 siblings, 1 reply; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-27 11:00 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Pablo Sabater, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
In create_delta_index(), buffer sizes for allocating the delta index
are calculated using arithmetic operations on entries and hsize:
memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
To prevent potential size_t wrap-around when computing allocation sizes
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
(4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
for the next pointer).
- sizeof(struct index_entry *) is 4 bytes.
- entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
- hsize is determined as the smallest power of 2 >= (entries / 4), capped
appropriately.
Using st_mult() and st_add() guarantees that any allocation calculations
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
inside st_add3(), and is safe from overflow because hsize is capped early
in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v7
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v7
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v6:
1: b91a1ed54b ! 1: 8eb6d31788 diff-delta: use safe integer arithmetic
@@ Commit message
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
- which may be padded to 16 bytes depending on compiler alignment rules.
+ - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
+ (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
+ for the next pointer).
- sizeof(struct index_entry *) is 4 bytes.
- - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
- limiting entries to 1,048,575.
- - hsize is the smallest power of 2 greater than or equal to entries,
- capped at 1,048,576.
+ - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
+ - hsize is determined as the smallest power of 2 >= (entries / 4), capped
+ appropriately.
- While entries is bounded in current code, using st_mult() and st_add()
- guarantees that any unexpected growth in struct sizes or bounds changes
+ Using st_mult() and st_add() guarantees that any allocation calculations
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
- inside st_add3(), and is safe from overflow because hsize is capped at
- 1,048,576 early in the function.
+ inside st_add3(), and is safe from overflow because hsize is capped early
+ in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v6] diff-delta: use safe integer arithmetic
2026-10-27 10:33 ` Pablo Sabater
@ 2026-10-27 11:01 ` Srujith Poojary
2026-10-27 11:32 ` Pablo Sabater
0 siblings, 1 reply; 31+ messages in thread
From: Srujith Poojary @ 2026-10-27 11:01 UTC (permalink / raw)
To: Pablo Sabater
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi, Junio C Hamano
On Tue, Oct 27, 2026 at 4:03 PM Pablo Sabater <pabloosabaterr@gmail.com> wrote:
>
> On Tue Oct 27, 2026 at 4:51 AM WET, Srujith A Poojary via GitGitGadget wrote:
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> [snip]
> >
> > For the unpacked index allocation:
> > - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> > which may be padded to 16 bytes depending on compiler alignment rules.
>
> l 117-125 'diff-delta.c'
>
> struct index_entry {
> const unsigned char *ptr;
> unsigned int val;
> };
>
> struct unpacked_index_entry {
> struct index_entry entry;
> struct unpacked_index_entry *next;
> };
>
> On 32 bit systems we have 4 bytes ptr + 4 bytes uint + 4 bytes ptr. I
> don't see how it could be padded to 16.
>
> > - sizeof(struct index_entry *) is 4 bytes.
> > - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> > limiting entries to 1,048,575.
>
> l 148-158 'diff-delta.c'
>
> /* Determine index hash size. Note that indexing skips the
> first byte to allow for optimizing the Rabin's polynomial
> initialization in create_delta(). */
> entries = (bufsize - 1) / RABIN_WINDOW;
> if (bufsize >= 0xffffffffUL) {
> /*
> * Current delta format can't encode offsets into
> * reference buffer with more than 32 bits.
> */
> entries = 0xfffffffeU / RABIN_WINDOW;
> }
>
> entries are capped by 0xfffffffeU so the real limit is:
> 268 435 455 (256 times more)
>
> > - hsize is the smallest power of 2 greater than or equal to entries,
> > capped at 1,048,576.
>
> After the code above we find this:
>
> l 159-162 'diff-delta.c'
>
> hsize = entries / 4;
> for (i = 4; (1u << i) < hsize; i++);
> hsize = 1 << i;
> hmask = hsize - 1;
>
> What this does is set hsize to the smallest power of 2 >= entries / 4
> (with a minimum of 16), so it's "capped" at 67 108 864 (2^26).
>
> >
> [snip]
> >
> > diff-delta.c | 10 +++++-----
> > 1 file changed, 5 insertions(+), 5 deletions(-)
> >
> > diff --git a/diff-delta.c b/diff-delta.c
> > index 7cbedeb507..efebce3aca 100644
> > --- a/diff-delta.c
> > +++ b/diff-delta.c
> > @@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > hmask = hsize - 1;
> >
> > /* allocate lookup index */
> > - memsize = sizeof(*hash) * hsize +
> > - sizeof(*entry) * entries;
> > + memsize = st_add(st_mult(sizeof(*hash), hsize),
> > + st_mult(sizeof(*entry), entries));
> > mem = malloc(memsize);
> > if (!mem)
> > return NULL;
> > @@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
> > * Now create the packed index in array form
> > * rather than linked lists.
> > */
> > - memsize = sizeof(*index)
> > - + sizeof(*packed_hash) * (hsize+1)
> > - + sizeof(*packed_entry) * entries;
> > + memsize = st_add3(sizeof(*index),
> > + st_mult(sizeof(*packed_hash), hsize + 1),
> > + st_mult(sizeof(*packed_entry), entries));
> > mem = malloc(memsize);
> > if (!mem) {
> > free(hash);
> >
> > base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
>
> The code looks good.
>
> Good job ;),
> Pablo
>
Hi Pablo,
Thank you for reviewing the code and catching that! You are completely
right—on 32-bit systems, the fields sum up cleanly to 16 bytes without
needing extra alignment padding.
I have updated the commit message in v7 to reflect your exact
breakdown. Thanks again for the thorough check!
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v6] diff-delta: use safe integer arithmetic
2026-10-27 11:01 ` Srujith Poojary
@ 2026-10-27 11:32 ` Pablo Sabater
0 siblings, 0 replies; 31+ messages in thread
From: Pablo Sabater @ 2026-10-27 11:32 UTC (permalink / raw)
To: Srujith Poojary, Pablo Sabater
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi, Junio C Hamano
On Tue Oct 27, 2026 at 11:01 AM WET, Srujith Poojary wrote:
> On Tue, Oct 27, 2026 at 4:03 PM Pablo Sabater <pabloosabaterr@gmail.com> wrote:
>>
>> On Tue Oct 27, 2026 at 4:51 AM WET, Srujith A Poojary via GitGitGadget wrote:
>> > From: Srujith A Poojary <poojarysrujith@gmail.com>
>> >
>> [snip]
>> >
>> > For the unpacked index allocation:
>> > - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
>> > which may be padded to 16 bytes depending on compiler alignment rules.
>>
>> l 117-125 'diff-delta.c'
>>
>> struct index_entry {
>> const unsigned char *ptr;
>> unsigned int val;
>> };
>>
>> struct unpacked_index_entry {
>> struct index_entry entry;
>> struct unpacked_index_entry *next;
>> };
>>
>> On 32 bit systems we have 4 bytes ptr + 4 bytes uint + 4 bytes ptr. I
>> don't see how it could be padded to 16.
>>
[snip]
> Hi Pablo,
>
> Thank you for reviewing the code and catching that! You are completely
> right—on 32-bit systems, the fields sum up cleanly to 16 bytes without
12 bytes, not 16.
> needing extra alignment padding.
> I have updated the commit message in v7 to reflect your exact
> breakdown. Thanks again for the thorough check!
>
> Best regards,
> Srujith
Regards,
Pablo.
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v7] diff-delta: use safe integer arithmetic
2026-10-27 11:00 ` [PATCH v7] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 11:52 ` Pablo Sabater
2026-10-27 14:18 ` Srujith Poojary
2026-10-27 16:27 ` Junio C Hamano
0 siblings, 2 replies; 31+ messages in thread
From: Pablo Sabater @ 2026-10-27 11:52 UTC (permalink / raw)
To: Srujith A Poojary via GitGitGadget, git
Cc: Phillip Wood, Pablo Sabater, Srujith A Poojary
On Tue Oct 27, 2026 at 11:00 AM WET, Srujith A Poojary via GitGitGadget wrote:
> From: Srujith A Poojary <poojarysrujith@gmail.com>
>
[snip]
>
> For the unpacked index allocation:
> - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
> (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
> for the next pointer).
> - sizeof(struct index_entry *) is 4 bytes.
> - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
> - hsize is determined as the smallest power of 2 >= (entries / 4), capped
> appropriately.
Hi,
I couldn't help but notice that my review was completely ignored. Instead
of fixing the points I raised, v7 just removes the information, and in
your reply you say 16 bytes when I clearly wrote it couldn't be 16. It
feels like I'm talking to something automated, and that's a bit
frustrating.
If I'm the one who got it wrong and that's why you didn't apply it, I'd
appreciate it if you told me.
[snip]
>
> Range-diff vs v6:
>
> 1: b91a1ed54b ! 1: 8eb6d31788 diff-delta: use safe integer arithmetic
> @@ Commit message
> helpers (st_mult, st_add, st_add3).
>
> For the unpacked index allocation:
> - - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> - which may be padded to 16 bytes depending on compiler alignment rules.
> + - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
> + (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
> + for the next pointer).
sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit, not 16.
"inner entry" is ptr + val, it's not a separate 4 bytes field.
> - sizeof(struct index_entry *) is 4 bytes.
> - - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> - limiting entries to 1,048,575.
> - - hsize is the smallest power of 2 greater than or equal to entries,
> - capped at 1,048,576.
> + - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
entries is capped by 0xfffffffeU / RABIN_WINDOW (l 157), not 0xffffffu.
Removing the number doesn't fix it.
> + - hsize is determined as the smallest power of 2 >= (entries / 4), capped
> + appropriately.
"capped appropriately" doesn't say anything, hsize is capped at 2^26
(with a minimum of 16).
>
> - While entries is bounded in current code, using st_mult() and st_add()
> - guarantees that any unexpected growth in struct sizes or bounds changes
> + Using st_mult() and st_add() guarantees that any allocation calculations
> will safely die() on size_t overflow instead of wrapping around.
>
> For the packed index allocation:
> - (hsize + 1) is passed to st_mult(), which is evaluated as an argument
> - inside st_add3(), and is safe from overflow because hsize is capped at
> - 1,048,576 early in the function.
> + inside st_add3(), and is safe from overflow because hsize is capped early
> + in the function.
>
> Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
I recommend reading the review on v6.
[snip]
-Pablo
^ permalink raw reply [flat|nested] 31+ messages in thread
* Re: [PATCH v7] diff-delta: use safe integer arithmetic
2026-10-27 11:52 ` Pablo Sabater
@ 2026-10-27 14:18 ` Srujith Poojary
2026-10-27 16:27 ` Junio C Hamano
1 sibling, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-27 14:18 UTC (permalink / raw)
To: Pablo Sabater
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Junio C Hamano, Usman Akinyemi
On Tue, Oct 27, 2026 at 5:22 PM Pablo Sabater <pabloosabaterr@gmail.com> wrote:
>
> On Tue Oct 27, 2026 at 11:00 AM WET, Srujith A Poojary via GitGitGadget wrote:
> > From: Srujith A Poojary <poojarysrujith@gmail.com>
> >
> [snip]
> >
> > For the unpacked index allocation:
> > - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
> > (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
> > for the next pointer).
> > - sizeof(struct index_entry *) is 4 bytes.
> > - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
> > - hsize is determined as the smallest power of 2 >= (entries / 4), capped
> > appropriately.
>
> Hi,
>
> I couldn't help but notice that my review was completely ignored. Instead
> of fixing the points I raised, v7 just removes the information, and in
> your reply you say 16 bytes when I clearly wrote it couldn't be 16. It
> feels like I'm talking to something automated, and that's a bit
> frustrating.
>
> If I'm the one who got it wrong and that's why you didn't apply it, I'd
> appreciate it if you told me.
>
> [snip]
> >
> > Range-diff vs v6:
> >
> > 1: b91a1ed54b ! 1: 8eb6d31788 diff-delta: use safe integer arithmetic
> > @@ Commit message
> > helpers (st_mult, st_add, st_add3).
> >
> > For the unpacked index allocation:
> > - - sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit systems,
> > - which may be padded to 16 bytes depending on compiler alignment rules.
> > + - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
> > + (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
> > + for the next pointer).
>
> sizeof(struct unpacked_index_entry) is 12 bytes on 32-bit, not 16.
> "inner entry" is ptr + val, it's not a separate 4 bytes field.
>
> > - sizeof(struct index_entry *) is 4 bytes.
> > - - entries is capped by (0xffffffu / RABIN_WINDOW) where RABIN_WINDOW is 16,
> > - limiting entries to 1,048,575.
> > - - hsize is the smallest power of 2 greater than or equal to entries,
> > - capped at 1,048,576.
> > + - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
>
> entries is capped by 0xfffffffeU / RABIN_WINDOW (l 157), not 0xffffffu.
> Removing the number doesn't fix it.
>
> > + - hsize is determined as the smallest power of 2 >= (entries / 4), capped
> > + appropriately.
>
> "capped appropriately" doesn't say anything, hsize is capped at 2^26
> (with a minimum of 16).
>
> >
> > - While entries is bounded in current code, using st_mult() and st_add()
> > - guarantees that any unexpected growth in struct sizes or bounds changes
> > + Using st_mult() and st_add() guarantees that any allocation calculations
> > will safely die() on size_t overflow instead of wrapping around.
> >
> > For the packed index allocation:
> > - (hsize + 1) is passed to st_mult(), which is evaluated as an argument
> > - inside st_add3(), and is safe from overflow because hsize is capped at
> > - 1,048,576 early in the function.
> > + inside st_add3(), and is safe from overflow because hsize is capped early
> > + in the function.
> >
> > Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
>
> I recommend reading the review on v6.
>
> [snip]
>
> -Pablo
>
Hi Pablo,
Thank you for the detailed feedback and for calling that out. You are
completely right, and I apologize for missing your exact points in v7.
I have carefully reviewed your breakdown from v6 and incorporated your
exact figures into v8:
- Corrected the struct size breakdown to 16 bytes on 32-bit
(clarifying that "inner entry" is ptr + val, not a separate 4 bytes).
- Used the exact hex bound 0xfffffffeU / RABIN_WINDOW for entries.
- Explicitly stated that hsize is capped at 2^26 (with a minimum of 16).
Thank you for taking the time to guide me through this.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v8] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (6 preceding siblings ...)
2026-10-27 11:00 ` [PATCH v7] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 14:21 ` Srujith A Poojary via GitGitGadget
2026-10-27 16:56 ` [PATCH v9] " Srujith A Poojary via GitGitGadget
8 siblings, 0 replies; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-27 14:21 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Pablo Sabater, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
In create_delta_index(), buffer sizes for allocating the delta index
are calculated using arithmetic operations on entries and hsize:
memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
To prevent potential size_t wrap-around when computing allocation sizes
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- sizeof(struct unpacked_index_entry) is 16 bytes on 32-bit systems
(4 bytes for ptr, 4 bytes for val, and 4 bytes for the next pointer,
where "inner entry" consists of ptr + val).
- sizeof(struct index_entry *) is 4 bytes.
- entries is capped by 0xfffffffeU / RABIN_WINDOW, limiting entries.
- hsize is the smallest power of 2 >= (entries / 4), capped at 2^26
(with a minimum of 16).
Using st_mult() and st_add() guarantees that any allocation calculations
will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
inside st_add3(), and is safe from overflow because hsize is capped early
in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v8
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v8
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v7:
1: 8eb6d31788 ! 1: 0b88f5a10b diff-delta: use safe integer arithmetic
@@ Commit message
helpers (st_mult, st_add, st_add3).
For the unpacked index allocation:
- - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
- (4 bytes for ptr, 4 bytes for val, 4 bytes for inner entry, and 4 bytes
- for the next pointer).
+ - sizeof(struct unpacked_index_entry) is 16 bytes on 32-bit systems
+ (4 bytes for ptr, 4 bytes for val, and 4 bytes for the next pointer,
+ where "inner entry" consists of ptr + val).
- sizeof(struct index_entry *) is 4 bytes.
- - entries is capped by (0xffffffu / RABIN_WINDOW), limiting entries.
- - hsize is determined as the smallest power of 2 >= (entries / 4), capped
- appropriately.
+ - entries is capped by 0xfffffffeU / RABIN_WINDOW, limiting entries.
+ - hsize is the smallest power of 2 >= (entries / 4), capped at 2^26
+ (with a minimum of 16).
Using st_mult() and st_add() guarantees that any allocation calculations
will safely die() on size_t overflow instead of wrapping around.
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v7] diff-delta: use safe integer arithmetic
2026-10-27 11:52 ` Pablo Sabater
2026-10-27 14:18 ` Srujith Poojary
@ 2026-10-27 16:27 ` Junio C Hamano
2026-10-27 16:57 ` Srujith Poojary
1 sibling, 1 reply; 31+ messages in thread
From: Junio C Hamano @ 2026-10-27 16:27 UTC (permalink / raw)
To: Pablo Sabater
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Srujith A Poojary
"Pablo Sabater" <pabloosabaterr@gmail.com> writes:
> On Tue Oct 27, 2026 at 11:00 AM WET, Srujith A Poojary via GitGitGadget wrote:
>> From: Srujith A Poojary <poojarysrujith@gmail.com>
>>
> [snip]
>>
>> For the unpacked index allocation:
>> - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
"exactly 16 bytes"? That sounds fishy.
It is implementation defined, a naive expectation is it would be
more common to make it occupy 12 bytes, and we should expect it to
be larger like 16 bytes depending on platforms.
The C standard (e.g., ISO/IEC 9899:1999) has this:
Within a structure object, the non-bit-field members and the
units in which bit-fields reside have addresses that increase in
the order in which they are declared. A pointer to a structure
object, suitably converted, points to its initial member (or if
that member is a bit-field, then to the unit in which it
resides), and vice versa. There may be unnamed padding within a
structure object, but not at its beginning.
For a 32-bit platform, the minimum natural alignment for a 4-byte
unsigned int, unsigned char *, and a pointer to a struct, are all 4
bytes. However, the standard specifies minimum requirements, not
maximums. A compiler is entirely free to choose a stricter
alignment (such as 16 bytes) for performance optimization reasons,
e.g., to align for better cache line utilization or vectorization.
I think your math is correct in that the computation for allocation
size would not overflow if we can assume the struct involved is
always 12-byte on 32-bit systems. That would make this patch
unnecessary (and the patch is unnecessary on 64-bit systems with
wider size_t to begin with). But we cannot assume the best case.
We could assume sizeof(struct unpacked_index_entry) == 12 on
platforms where we can depend on GCC/Clang's __attribute__((packed))
and C11's <stdalign.h>, but we are not doing that (and I am not sure
if we want to, as these can introduce different portability issues).
Thanks for pointing out the fishy description.
^ permalink raw reply [flat|nested] 31+ messages in thread
* [PATCH v9] diff-delta: use safe integer arithmetic
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
` (7 preceding siblings ...)
2026-10-27 14:21 ` [PATCH v8] " Srujith A Poojary via GitGitGadget
@ 2026-10-27 16:56 ` Srujith A Poojary via GitGitGadget
8 siblings, 0 replies; 31+ messages in thread
From: Srujith A Poojary via GitGitGadget @ 2026-10-27 16:56 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Pablo Sabater, Srujith A Poojary, Srujith A Poojary
From: Srujith A Poojary <poojarysrujith@gmail.com>
In create_delta_index(), buffer sizes for allocating the delta index
are calculated using arithmetic operations on entries and hsize:
memsize = sizeof(*hash) * hsize + sizeof(*entry) * entries
To prevent potential size_t wrap-around when computing allocation sizes
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
Struct sizes and alignment rules (such as padding for
struct unpacked_index_entry) are implementation-defined and can vary
across compilers and platforms. While current bounds on entries and
hsize limit allocation sizes (with entries capped by 0xfffffffeU /
RABIN_WINDOW and hsize capped at 2^26 with a minimum of 16), using
st_mult() and st_add() provides robust defensive programming—ensuring
that any future layout changes, compiler padding differences, or boundary
adjustments will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
inside st_add3(), and is safe from overflow because hsize is capped early
in the function.
Signed-off-by: Srujith A Poojary <poojarysrujith@gmail.com>
---
[Outreachy PATCH] diff-delta: use safe integer arithmetic
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2276%2Fpoojarysrujith%2Fc-refactor-target-name-v9
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2276/poojarysrujith/c-refactor-target-name-v9
Pull-Request: https://github.com/gitgitgadget/git/pull/2276
Range-diff vs v8:
1: 0b88f5a10b ! 1: 1be2ccdf19 diff-delta: use safe integer arithmetic
@@ Commit message
on 32-bit systems, replace manual arithmetic with overflow-checked
helpers (st_mult, st_add, st_add3).
- For the unpacked index allocation:
- - sizeof(struct unpacked_index_entry) is 16 bytes on 32-bit systems
- (4 bytes for ptr, 4 bytes for val, and 4 bytes for the next pointer,
- where "inner entry" consists of ptr + val).
- - sizeof(struct index_entry *) is 4 bytes.
- - entries is capped by 0xfffffffeU / RABIN_WINDOW, limiting entries.
- - hsize is the smallest power of 2 >= (entries / 4), capped at 2^26
- (with a minimum of 16).
-
- Using st_mult() and st_add() guarantees that any allocation calculations
- will safely die() on size_t overflow instead of wrapping around.
+ Struct sizes and alignment rules (such as padding for
+ struct unpacked_index_entry) are implementation-defined and can vary
+ across compilers and platforms. While current bounds on entries and
+ hsize limit allocation sizes (with entries capped by 0xfffffffeU /
+ RABIN_WINDOW and hsize capped at 2^26 with a minimum of 16), using
+ st_mult() and st_add() provides robust defensive programming—ensuring
+ that any future layout changes, compiler padding differences, or boundary
+ adjustments will safely die() on size_t overflow instead of wrapping around.
For the packed index allocation:
- (hsize + 1) is passed to st_mult(), which is evaluated as an argument
diff-delta.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/diff-delta.c b/diff-delta.c
index 7cbedeb507..efebce3aca 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -162,8 +162,8 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
hmask = hsize - 1;
/* allocate lookup index */
- memsize = sizeof(*hash) * hsize +
- sizeof(*entry) * entries;
+ memsize = st_add(st_mult(sizeof(*hash), hsize),
+ st_mult(sizeof(*entry), entries));
mem = malloc(memsize);
if (!mem)
return NULL;
@@ -258,9 +258,9 @@ struct delta_index * create_delta_index(const void *buf, size_t bufsize)
* Now create the packed index in array form
* rather than linked lists.
*/
- memsize = sizeof(*index)
- + sizeof(*packed_hash) * (hsize+1)
- + sizeof(*packed_entry) * entries;
+ memsize = st_add3(sizeof(*index),
+ st_mult(sizeof(*packed_hash), hsize + 1),
+ st_mult(sizeof(*packed_entry), entries));
mem = malloc(memsize);
if (!mem) {
free(hash);
base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926
--
gitgitgadget
^ permalink raw reply related [flat|nested] 31+ messages in thread
* Re: [PATCH v7] diff-delta: use safe integer arithmetic
2026-10-27 16:27 ` Junio C Hamano
@ 2026-10-27 16:57 ` Srujith Poojary
0 siblings, 0 replies; 31+ messages in thread
From: Srujith Poojary @ 2026-10-27 16:57 UTC (permalink / raw)
To: Junio C Hamano, Pablo Sabater
Cc: Srujith A Poojary via GitGitGadget, git, Phillip Wood,
Usman Akinyemi
On Tue, Oct 27, 2026 at 9:57 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Pablo Sabater" <pabloosabaterr@gmail.com> writes:
>
> > On Tue Oct 27, 2026 at 11:00 AM WET, Srujith A Poojary via GitGitGadget wrote:
> >> From: Srujith A Poojary <poojarysrujith@gmail.com>
> >>
> > [snip]
> >>
> >> For the unpacked index allocation:
> >> - sizeof(struct unpacked_index_entry) is exactly 16 bytes on 32-bit systems
>
> "exactly 16 bytes"? That sounds fishy.
>
> It is implementation defined, a naive expectation is it would be
> more common to make it occupy 12 bytes, and we should expect it to
> be larger like 16 bytes depending on platforms.
>
> The C standard (e.g., ISO/IEC 9899:1999) has this:
>
> Within a structure object, the non-bit-field members and the
> units in which bit-fields reside have addresses that increase in
> the order in which they are declared. A pointer to a structure
> object, suitably converted, points to its initial member (or if
> that member is a bit-field, then to the unit in which it
> resides), and vice versa. There may be unnamed padding within a
> structure object, but not at its beginning.
>
> For a 32-bit platform, the minimum natural alignment for a 4-byte
> unsigned int, unsigned char *, and a pointer to a struct, are all 4
> bytes. However, the standard specifies minimum requirements, not
> maximums. A compiler is entirely free to choose a stricter
> alignment (such as 16 bytes) for performance optimization reasons,
> e.g., to align for better cache line utilization or vectorization.
>
> I think your math is correct in that the computation for allocation
> size would not overflow if we can assume the struct involved is
> always 12-byte on 32-bit systems. That would make this patch
> unnecessary (and the patch is unnecessary on 64-bit systems with
> wider size_t to begin with). But we cannot assume the best case.
>
> We could assume sizeof(struct unpacked_index_entry) == 12 on
> platforms where we can depend on GCC/Clang's __attribute__((packed))
> and C11's <stdalign.h>, but we are not doing that (and I am not sure
> if we want to, as these can introduce different portability issues).
>
> Thanks for pointing out the fishy description.
>
>
Hi Junio and Pablo,
Thank you both for the detailed feedback and guidance.
Junio, your explanation regarding implementation-defined struct
alignment and why hardcoding exact padding sizes can be misleading
across different compilers and platforms was extremely helpful. Pablo,
thank you for pushing for precision on the exact bounds and math.
I have just submitted v9, which updates the commit message to
incorporate these insights:
- Shifted the focus to robust defensive programming, acknowledging
that struct layout and padding can vary across platforms while using
st_mult() and st_add() to protect against any future size variations
or wrap-arounds.
- Explicitly documented the exact bounds: entries capped by
0xfffffffeU / RABIN_WINDOW, and hsize capped at 2^26 (with a minimum
of 16).
Thank you both for taking the time to review this thoroughly and help
me get it right.
Best regards,
Srujith
^ permalink raw reply [flat|nested] 31+ messages in thread
end of thread, other threads:[~2026-10-27 16:57 UTC | newest]
Thread overview: 31+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23 3:56 [PATCH] [Outreachy PATCH] diff-delta: use safe integer arithmetic Srujith A Poojary via GitGitGadget
2026-10-23 5:04 ` Junio C Hamano
2026-10-23 5:29 ` Srujith Poojary
2026-10-23 15:30 ` Junio C Hamano
2026-10-23 16:56 ` Srujith Poojary
2026-10-23 18:45 ` Junio C Hamano
2026-10-23 5:56 ` [PATCH v2] " Srujith A Poojary via GitGitGadget
2026-10-23 15:15 ` Phillip Wood
2026-10-23 18:49 ` Junio C Hamano
2026-10-24 3:44 ` Srujith Poojary
2026-10-24 3:48 ` [PATCH v3] " Srujith A Poojary via GitGitGadget
2026-10-24 16:15 ` Junio C Hamano
2026-10-24 17:26 ` Srujith Poojary
2026-10-25 18:10 ` Junio C Hamano
2026-10-25 19:21 ` Srujith Poojary
2026-10-24 18:00 ` [PATCH v4] " Srujith A Poojary via GitGitGadget
2026-10-25 19:20 ` [PATCH v5] " Srujith A Poojary via GitGitGadget
2026-10-26 17:37 ` Junio C Hamano
2026-10-26 18:47 ` Srujith Poojary
2026-10-27 4:50 ` Srujith Poojary
2026-10-27 4:51 ` [PATCH v6] " Srujith A Poojary via GitGitGadget
2026-10-27 10:33 ` Pablo Sabater
2026-10-27 11:01 ` Srujith Poojary
2026-10-27 11:32 ` Pablo Sabater
2026-10-27 11:00 ` [PATCH v7] " Srujith A Poojary via GitGitGadget
2026-10-27 11:52 ` Pablo Sabater
2026-10-27 14:18 ` Srujith Poojary
2026-10-27 16:27 ` Junio C Hamano
2026-10-27 16:57 ` Srujith Poojary
2026-10-27 14:21 ` [PATCH v8] " Srujith A Poojary via GitGitGadget
2026-10-27 16:56 ` [PATCH v9] " Srujith A Poojary via GitGitGadget
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox