Article View: linux.kernel.git
Article #1599Re: [PATCH v6] diff-delta: use safe integer arithmetic
From: "Pablo Sabater"
Date: Tue, 27 Oct 2026 10:33
Date: Tue, 27 Oct 2026 10:33
106 lines
13971 bytes
13971 bytes
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
Technical Details
Message-ID:
<DMFJX3GJOXI0.1US2IH6P4LRUR@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-73448-linux-kernel-git=dewy.ruReferences:
<pull.2276.git.1792727808109.gitgitgadget@gmail.com> <pull.2276.v6.git.1793076683043.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-73448-linux-kernel-git=dewy.ru
From: pabloosabaterr@gmail.com ("Pablo Sabater")
Newsgroups: linux.kernel.git
Subject: Re: [PATCH v6] diff-delta: use safe integer arithmetic
Date: Tue, 27 Oct 2026 10:33:49 +0000
Organization: Usenet Network
Message-ID: <DMFJX3GJOXI0.1US2IH6P4LRUR@gmail.com>
References: <pull.2276.git.1792727808109.gitgitgadget@gmail.com>
<pull.2276.v6.git.1793076683043.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
logging-data="1513450"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:pNBV2xGuVglgkxPtjJ2q/2iReuw= sha256:iTYrqe7UnaYr1XvcPh+1XyPvc5EuHdrw1qNKOrodSKM=
sha1:5CU8R/8lSz4f97a/klrHYZkicf8= sha256:igzNnbpPaLdoRA0UokuZREDJX7EwkhLC/TgT0cCzI6Y=
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 2853D125BE8; Tue, 27 Oct 2026 13:34:13 +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 CrHqUhJbgdEH;
Tue, 27 Oct 2026 13:34:09 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-73448-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 sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
by mail.dewy.ru (Postfix) with ESMTPS id BB94A125BE6; Tue, 27 Oct 2026 13:34:00 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sin.lore.kernel.org (Postfix) with ESMTP id D5D3F24289; Tue, 27 Oct 2026 10:33:56 +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=1793097234;
b=wJuBQn+/mLoe6FcrYYcI6KpAdQBrTXzajCdyjRLtrHJqNz5TMGHC2ijkKCrXIARZUIjw
FCD/X82qVVKeCWkqiRApkVIIA071P1SM7Xh4RfqkQheKwZT2AfhHjm0WqD35zBtmuLmeq
6XwRXjbhhzrd2RdJjfb9SFLumqS0wHuwn32DpTw782sbufREyzmBKzxCsgoE74z3W/Hb7
x8hxtAeQIo10TrvQTqypOjoul1DQDkMG6MiPhrLdVl1YXRD11dFBpLCFqZV+vrb+VN1kQ
GWR2/zp1xCHpIgHZwZUCsVmT7NHoSlyauvexnkTB38DSw6viNrjFs2U2ZRmmWtwHtIg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793097234;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:X-Mailing-List:List-Id:List-Post:
List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
Mime-Version:Content-Transfer-Encoding:Content-Type:Date:Message-Id:
To:Cc:Subject:From:X-Mailer:References:In-Reply-To;
bh=jWLUFMWK2uBPpg6WENify7MfWel7uxZRHVsqytU1NcA=;
b=4/Y1t7kT+gCKSNM5ZwsPuvXk3sn/z1wfcdf5etIG/VRCngi+VFGkFA36GQfYLMEvkTmO
sDer2Bm7WofOW4C8vWy1/wxw4w4bP4Jt7vFdNeGfb6m5swhhBxyTssrZFLJS4btwgRQbP
v3vKI8iY4mc9RlRofJb6BLVzRgycHIQym4mmOkPbktMPSdPq448yZB62Ax47kRofamWo6
blhKV3jJo386yby9bBo+0u+ao/SBn0jqh9gjT3BRBORVenkBKovhS9dZnd+DmUEC3pwoi
OP7QyUq5Tun8Loygwwwn/MtjXIDSnQBqIsPfYiffqJaTqHegMGXYoT0BMh83cxdBd0w==
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=sQwrDlPO;
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 582A4606A322;
Tue, 27 Oct 2026 10:33:54 +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=sQwrDlPO
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.41
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793097233;
b=qBOxrJ2cYyRrGhZ+okT28awy/cgx2lwBudO1gaDeE7vlOdDuGdGS/YuAnVpPX9ecJPKN
1cWaEpGeWwC/aLMwz1MZy89IJOld0uhCG0yAw/KgFBM2F9jJz63MfxJcEc8jvLiAqlsPj
mTk9HfXENgeI/6udB+skHr+6746mh5h61lfIlS9rM835DEgGzQfaW9+bsmgCEhZE/an19
EpWo0CcqVBVHvUnFx3zVLVVjAatPArB55Sv/n7sDtvWRZp+rOAmhMZ091TQeNuegvrlzn
kA2uskYoDxDRFEqWCc/QOTBqd8LQaZ7TkJBQ4fHjj8DrAoimV3j/nCMhE20+gw7r7CQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793097233;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
Mime-Version:Content-Transfer-Encoding:Content-Type:Date:Message-Id:
To:Cc:Subject:From:X-Mailer:References:In-Reply-To;
bh=jWLUFMWK2uBPpg6WENify7MfWel7uxZRHVsqytU1NcA=;
b=d81wX2krgIxc0ULcMafz/sk7kZ25linIEwcY1PcaokLxHDCq7uygSTydg8xn80dna5o/
v4RonK6kF2KQmfKT761olueapQbp3ah8fJQVV0NE31dDl+qwUETyg61ELGiNn4f/WZk1p
jY3xk+IOjHSna+DN77WylhecuFdxp9scDFNBb+YFdR0dnZ9F+CVHzUb/pJmNhd4r23Fvo
XfjDuloaltVPnSlXGlfg8zQkEj0foFIsEga7IAs6UaFu/LQcKd+5n1uTh42XcehjY/fmx
+ki48mr6tDFeus729Y6BWWY43uIGVV7zb+CUqOOuWIAmuCTJvfWIZkqgGqPPO1lBQ6g==
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=sQwrDlPO;
arc=none smtp.remote-ip=209.85.128.41
X-Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41])
(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 1CE35606A31A
for <git@vger.kernel.org>; Tue, 27 Oct 2026 10:33:52 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1CE35606A31A
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-wm1-f41.google.com with SMTP id 5b1f17b1804b1-4a35ac4475dso21490745e9.2
for <git@vger.kernel.org>; Tue, 27 Oct 2026 03:33:52 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1793097231; x=1793702031; darn=vger.kernel.org;
h=in-reply-to:references:from:subject:cc:to:message-id:date
:content-type:content-transfer-encoding:mime-version:from:to:cc
:subject:date:message-id:reply-to:content-type;
bh=jWLUFMWK2uBPpg6WENify7MfWel7uxZRHVsqytU1NcA=;
b=sQwrDlPOCpjk03Tx81L5AJ67Eag4qhs9jwICwe744XI1leVXBfzq1vACRp2aFmICEo
Z+frfGID/gGGeDmQI3XKI5B249MptxeraS3QATDqsqryLyVSjHiODaRErN3WyQYEiHFI
aHGXAltAYwHCzipjJozWkVYASxQOuQAUabbrZoE/WxsetD/qmAZvOgr9UClCAnrJMkh+
7ncxjD/Y+0MzBAF4tK+Cc4/hWw2AFyTrOWfHeopMeNMvtV+L9xttjuttLz34r1XJKnEQ
9AOXZzJLVtoHmgVBKfgiftqZEs32ompO57jNvUs1geA5bIr1qrkpUkJcA8unJDtOySGN
DD0w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793097231; x=1793702031;
h=in-reply-to:references:from:subject:cc:to:message-id:date
:content-type:content-transfer-encoding:mime-version:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=jWLUFMWK2uBPpg6WENify7MfWel7uxZRHVsqytU1NcA=;
b=p38kG+5yf1GdDYbJ3xge52MhallmD/Qmfg1JDpouq8GFASgsV0lhnT/kWMUtwWqH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b/Iy0x0EZJ7W7T9iriXbnTvj6EJ1AsYInn4xKxlozBxthrH7ee8LjNj41WXepze5h
zF9Rc/BIygplKMrHiwc2iF5j8qvLLintgxZTVXXbLP0eoFlfcsv0KtAtEmyh9jvapbjte6gwf9u
uQvpyis/+mpXp7lzBbNUnJvJZjXLRf5NikWdmHLYmWUGXUk+iQkV+wi5Wyg6WkaR4Bp+VPUqr/H
wuF//p
X-Received: by 2002:a05:600c:3491:b0:4a2:40:8d2c with SMTP id 5b1f17b1804b1-4a35acbd31amr180539755e9.26.1793097231114;
Tue, 27 Oct 2026 03:33:51 -0700 (PDT)
X-Received: from localhost ([2001:818:c665:a700:9b4:dea8:96dd:9a68])
by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4a35a0bae01sm317856355e9.7.2026.10.27.03.33.50
(version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
Tue, 27 Oct 2026 03:33:50 -0700 (PDT)
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>
X-Mailer: aerc 0.21.0
X-In-Reply-To: <pull.2276.v6.git.1793076683043.gitgitgadget@gmail.com>