Article View: linux.kernel.git
Article #1585[PATCH v6] diff-delta: use safe integer arithmetic
From: "Srujith A Poojary via GitGitGad...
Date: Tue, 27 Oct 2026 04:51
Date: Tue, 27 Oct 2026 04:51
90 lines
14132 bytes
14132 bytes
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
Technical Details
Message-ID:
<pull.2276.v6.git.1793076683043.gitgitgadget@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73414-linux-kernel-git=dewy.ruReferences:
<pull.2276.git.1792727808109.gitgitgadget@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-73414-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Srujith A Poojary via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v6] diff-delta: use safe integer arithmetic
Date: Tue, 27 Oct 2026 04:51:23 +0000
Organization: Usenet Network
Message-ID: <pull.2276.v6.git.1793076683043.gitgitgadget@gmail.com>
References: <pull.2276.git.1792727808109.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="1466653"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:mQXzrMtysQ5u8ROFxvar/WkEZZE= sha256:tIO3mQZBSO7PDLH/Pe810n1mHti1YLz8izClrKwKNJ0=
sha1:APAtalTyrhbMcYj+WhJrCJFn4lE= sha256:EzJGavBG8XMFUOzJGVtnttvgPmgdm3BnD6OgLT6e08E=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 6BAC9125BDC; Tue, 27 Oct 2026 07:52:00 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id IZeN-1VxfjJX;
Tue, 27 Oct 2026 07:51:56 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-73414-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 847CB125BDA; Tue, 27 Oct 2026 07:51:48 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id AC38729CAD; Tue, 27 Oct 2026 04:51:27 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1793076687;
b=jxvSjLUDEHXx8AQQzZ+GYN6vHHxgnAERVU1jhO+N9j+aVr8CdIbSpILH2rWvqLCYPu95
rVKdzSLXalVq4PUDZ1ELwuXMQkg1sVDwVRRkmpoC2PF0+adOnpDEsZEQttgNyHM9x7+cP
HlXIOSuDCh2lCq9sW6+TySvRrEI5NVDAuLSOvxKO8gVDFVH28tPUQcoNl2K77keVdMN7T
u6YkZwDYa/28dVvJjb6DLExO7G9Dwteqa0tZwZ0SKAdwaS30ngxx/45bOzpfP5fxMNblV
dPvaqZShA/OVa8IHWyp9NiUqE5Z8uNLBN38eZDYDFbaULfzuLx27TLyQqlEFs0hhfRw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793076687;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:Message-Id:In-Reply-To:References:From:Date:Subject:Fcc:
Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
Precedence:MIME-Version:To:Cc;
bh=tKw9U+TJSwq0IiqYD/HJ55xyxaRrsN4emYoCAcVCGJg=;
b=gO96zMgVtNDCRIKUMaf9wUWn+ID2K9EcGdeRZGKmzt8UEiV6p1EpLwvx2Q+Jto1FWpHp
STsQWP0SbfSn2wTF6PSF7uUNhekTmi6ODNQnp1WI0sAA/1W2Fkp8fv5HHdlOb5dnMSvN9
mfQGG5ctSfn4EpK4UnGziI2LhzMlHw88WowNibWq7ILYJkKdx3TUDO7FnQD3cLIEPrT9i
Z2URpFHbSt+JzCI4c50piWjjsod2kP1XIY6ukC+8N6PPuzjtwRMEjLMI5ZN+kodB2tWvL
hDghYHAdaK3PMdH7vQWvZsdAnbnmEI9lJBo/AsSMaJrqPaq1FEy8yvzNT9Not2pu55Q==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=SrO11gNG;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 82678606A321;
Tue, 27 Oct 2026 04:51:27 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=SrO11gNG
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.173
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793076686;
b=OzgdqKbikxctdncHb537EinCtUvuL29TYBK3Q1wLW1bYolcGdX5+2s3ImXGIH+EeYwKo
asTKmnSiVjUZ0WuxQpWfVNOmq9OGCR+Bt7RFfwhzedjIE2GhhssyI/TDggEkyozvZA5ZL
9PCTYzIblbzkpiFYpJx10TU2iVdVjDpTtYCO9x254/OeBIucDT8m8cRrcJJHHEuP1RfPZ
PyWH6lBL3o7+W1aZ5YLeds7x0onhnXLiQDO8GxRquafD3/08H9TPGNYqcQFox1ImHHtEJ
RAPy4VkT64CSSev1zoimV7hBjHASuZ9PfS3bHdnFlbOerkaWuZzwznB7kE/wMjNOf5g==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793076686;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To:
References:From:Date:Subject:Fcc:Content-Type:
Content-Transfer-Encoding:MIME-Version:To:Cc;
bh=tKw9U+TJSwq0IiqYD/HJ55xyxaRrsN4emYoCAcVCGJg=;
b=gDZa0XSuPfu3fie+cHHIl4s9HEXx0mHIT9rY2HQ7XMB/xtyrXfl+asYIASQyKv3YUf/X
jtIVqjbx0cAxfLqRPbAuKLV2EquLzDMsv7RH5YN5iTvQaGhZAH2t1l6JE4jj7j3LhSAU5
Qgk9dIpOf3HnX9pXUGy6FKM6Uo1qB5YSmJz8y7j8sU/XoY+vZPvIvMngPi4EvUbXBmd8R
wt6MUIghIi/7eKDAzgd5yDQko/oHA3xGmbFf7jSUJ48giTLok75mFJYVvxGd3pLAGdKit
+GPexARcCKztEKLT/kn/yuGaxvYVZA0bVKwQu59b8cfOh32fjlW2Ifl2DBxhKBBDOSw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=SrO11gNG;
arc=none smtp.remote-ip=74.125.82.173
X-Received: from mail-dy1-f173.google.com (mail-dy1-f173.google.com [74.125.82.173])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45D18606A31A
for <git@vger.kernel.org>; Tue, 27 Oct 2026 04:51:26 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45D18606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-dy1-f173.google.com with SMTP id 5a478bee46e88-35829219da8so776736eec.0
for <git@vger.kernel.org>; Mon, 26 Oct 2026 21:51:26 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1793076685; x=1793681485; darn=vger.kernel.org;
h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
:subject:date:from:references:in-reply-to:message-id:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=tKw9U+TJSwq0IiqYD/HJ55xyxaRrsN4emYoCAcVCGJg=;
b=SrO11gNGIAwt2ym5Fzt8TbYtZDWczg3Kv/M+fYjYKBJr+fO4e1pMpJ6koyYp35B9MD
U+6Ihsc4vOHbLglPKXeGUu4+3S+EtSMeVXr4sbaqeoCzNsLo67aRpeL4/OYlOlCA4yES
mS4PGwLIRQvGFlh+TtEB3n2t1AW3Ds21QUvL1OQLXdrSfZLFZqvnj0Gwi5+oPlnh42Pc
C9HYSTJCRylY0pwa/mgd7MWQb93yKFuQBFLvYJSE3K+HjckHweji7kkAzRLjuGeigW/i
CYEIpw1lOD+HdpOauzPQ71+5GccLEUy/O6yeETTk7jVIgQKod4SrnO3clLtWBYIbzoaK
SxEQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793076685; x=1793681485;
h=cc:to:mime-version:content-transfer-encoding:content-type:fcc
:subject:date:from:references:in-reply-to:message-id:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id: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HfFXT+049kkOjp4pjB1I+YJz9ZrWBw1srzgONNbQUe4ZVweJLzaM
X-Received: by 2002:a05:693c:6384:20b0:339:851c:4c1 with SMTP id 5a478bee46e88-36a0f887a20mr2051928eec.18.1793076684679;
Mon, 26 Oct 2026 21:51:24 -0700 (PDT)
X-Received: from [127.0.0.1] ([20.171.39.88])
by smtp.gmail.com with ESMTPSA id 5a478bee46e88-369e8ee72c0sm45684188eec.29.2026.10.26.21.51.23
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Mon, 26 Oct 2026 21:51:23 -0700 (PDT)
X-In-Reply-To: <pull.2276.git.1792727808109.gitgitgadget@gmail.com>
X-Fcc: Sent
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-Cc: Phillip Wood <phillip.wood123@gmail.com>,
Srujith A Poojary <poojarysrujith@gmail.com>,
Srujith A Poojary <poojarysrujith@gmail.com>