Re: [PATCH v2] diff-delta: use safe integer arithmetic
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.
Show 11 quoted lines
> 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);
Show 11 quoted lines
> 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
Show 5 quoted lines
> + st_mult(sizeof(*packed_entry), entries));
> mem = malloc(memsize);
> if (!mem) {
> free(hash);
> > base-commit: 47fdf2208c5b94c8cc01c106b8f7c85efb91c926