🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #1441

[PATCH v5] diff-delta: use safe integer arithmetic

#1441
From: "Srujith A Poojary via GitGitGad...
Date: Sun, 25 Oct 2026 19:20
127 lines
16182 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:
- 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
Technical Details
Message-ID: <pull.2276.v5.git.1792956031588.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-73213-linux-kernel-git=dewy.ru
References: <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-73213-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Srujith A Poojary via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v5] diff-delta: use safe integer arithmetic
Date: Sun, 25 Oct 2026 19:20:31 +0000
Organization: Usenet Network
Message-ID: <pull.2276.v5.git.1792956031588.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="1109205"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:sEm54zxpJRibJP93hr7zGFzPXmU= sha256:BT4Gpk3+FqWDRoe8J4MzKuIW0WvtV/quLeVIBOj9cDM=
	sha1:bgIKZPhxLhl04/jqJPbK46gPJ9M= sha256:uJ9JUGXbA7tRWzMdMNN0z++jvbRjMqlzCBW8d61SJXM=
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 B59EA125B44; Sun, 25 Oct 2026 22:21:35 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: 0.51
X-Spam-Status: No, score=0.51 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, RCVD_IN_SBL_CSS=3.558,
	SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=no 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 adrUKZMKo4or;
	Sun, 25 Oct 2026 22:21:30 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-73213-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 0DF6A125B42; Sun, 25 Oct 2026 22:21:21 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id D5B8D2B04F; Sun, 25 Oct 2026 19:20:35 +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=1792956035;
	b=w6bZL17zm5Cyz/BhE7yLrsUDgs1kOWlaMgqrncM8c9S5ipzKEGzrXrdgpUeSOMLxnJU/
	 tWDTkDxc4bcM9rqNOe9NDehoPWBdVPG078mSFi3Z6FIuk2V2Mo23/h+23YkYpd3E91SgF
	 lNf/jzcnuhGAVxY4P2IK0OnwtO6I6J4MrFdvecqioug14B9OUtgYJg0bbruUITB+w7hPJ
	 S9+2ggk6DD6+dz1TX6cCkk285+2bwQVwvIKnW5/uWgL+kdRWhlfTn6+6PscYvK0AtRa4K
	 Y+jysEdI+niPgOIgAk56y3FWwFqpvPuOixbUHeYVHcDrdkwwhVfIoRsfJpKPeniB+wQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792956035;
	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=b80acukHVdMLzZTTka6IesVtT9UFyhKUNuNF0s+dA+o=;
	b=sqMftgLlhUSEJKpKAT/eFZ2ssY2dd7OAfvHLXgnm4UTkCUhBmG5vdyZkAh1zx4/a6Ik4
	 w5jxY5z5zY0rNBI9/vp41KpzXIKkuChW7i2s/6nqJy0TOEyOgD8MXaPCCK336p/k6DDYH
	 kPD1m2eXY1fyFDhd9QQ8Qv1c9cC1H8g0AR7fOMQTfhZJkPHs9588B0MjNBRevVZIf/rLx
	 T2e/DvAJ14B/n2fCqfbUDBDgPHz+5OXVhFmSdKXhxq6VcuzE+fVK62uG//OLy7DcqsioN
	 psVnflZ0e8nCh7ZixqvXxDNylZFzS1iolp2ZA+F5CPbPn+Gt0+uEj5xwA0qgA71IEhA==
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=ATgxuxA1;
	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 15B3A606A321;
	Sun, 25 Oct 2026 19:20:35 +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=ATgxuxA1
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=1792956034;
	b=0aNrzo046upXHfvYKTfJ4y8IJBcZ0Y+BZU7jwIGJake7zgp11fb78A3JJZRgDBnvzsGq
	 OZhDKS6SrlsAOg4NT26VHa3wOzW7CBbK2z+lmPduJYjF/0k5MHot9lFSICmfez9Agn7BY
	 49L42XNnXNc5dxeJaEyRxaXn2kMzGVvdBQza8vd8JH/6AD6JY4lbW1ayfppCxgl7UJCud
	 ukUphTDEPiqLmscBUQJsXbFDzq8Gsqvjm/APmy6PXG7QEB0IiSz1z3bHATqh78u98aHlt
	 jr2jfI1ILwzAwIwLiiRW6ukzS+yTFWfie4eE5Khsytp/4ACSNF1SF7KjPj3IhB0d62Q==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792956034;
	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=b80acukHVdMLzZTTka6IesVtT9UFyhKUNuNF0s+dA+o=;
	b=YwIg68Gf3EgjHlUNEJH+k028dVNO5Oqmrby/i8TcUBy2cEOuk54SX8XC1pvzZ3+XHXfK
	 hQf1pw6Vm8B7z3ynE2M5q+V2lE8gad5j4vMlb4SC4noAuI8wpOyBQpMvjEkUwRNO4NaEg
	 y8VbJsStK8nR50sXTbKZY2JtlUp6UfRu8JrALHQimj/MCF9PbEoPSGHDjurmjAHVePAeI
	 tNujtC4OWdLlzEyS5kTXl8LZuxsRkzGQh5b20NntrCO/LxlzRXaRxEcKmpt1OV27P4IW4
	 B6IaZHeA6rr5So4I4QseMJyyMYvk/gwkpZcz6Tczf77B0ASgMynUsrh14S/0XJo9dBA==
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=ATgxuxA1;
	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 CC356606A31A
	for <git@vger.kernel.org>; Sun, 25 Oct 2026 19:20:33 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CC356606A31A
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-35f036b17easo6193782eec.1
        for <git@vger.kernel.org>; Sun, 25 Oct 2026 12:20:33 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1792956033; x=1793560833; 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=b80acukHVdMLzZTTka6IesVtT9UFyhKUNuNF0s+dA+o=;
        b=ATgxuxA19vz+fdTSXH2hhO6saRH8o+nlwhObwBszxhbeRF+k+xc8Nnosi8x454D7AH
         4gL8U0IuJbvTBXaIxjbWpGM7bMEQO+3dh8l8mGy548l4MjFKQOd6D0SihABiV2sXCOT4
         AkFNqr77EufXenn3ZDu87LTM5+eyApWtvEd7VTP9k1a2QjR015C5oKKLFBNVumEo4IEi
         U4WRa9I3tYt+ywq+NVjAL2/yhYFAPjvuqsRi+rVejydIywl0wygCDFoTwKFuEY4cC0t0
         nRII7uUmLC2B9kQCgqB8Fpho/TVix/zPsVQugDkPvqgK4uPypucjPZhlpwKJ0i6c7vjD
         qF2w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1792956033; x=1793560833;
        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:reply-to
         :content-type;
        bh=b80acukHVdMLzZTTka6IesVtT9UFyhKUNuNF0s+dA+o=;
        b=plCt3bb8sHccwdYBF7gKz3QwoldA1iOBiVkR5IMD1xjyeEc+3cUVfl5Eq8lyzuWXLG
         bNWDWHCAMmTtyMPWpap9xuePVS5Jp6crVKP+Gt06aQLbwgLGleau4xTNz3Scx+hHUXcr
         qaXr2zW5U8FILHMgST2CVSoBcuuIqVJtHNu1sUgjgII/evKSJgpfw/+QsQRpIU68rq+t
         ODr8CuyL3tSH5iMsqg26/vkHCR9WI/mdd6xcM097quTQWREGiLfe8UHOmZc5V4EVYi2B
         empnX2JQ8oUH12PHECbxEmvkhROkKGK7ukhgdy3sfaz+g2o1klzjcD6TiwrHuK46XPp2
         f04A==
X-Gm-Message-State: AFq9FYJWWN6mUaAmZnw1uSMNb24Quik/DNaqdRGV8eIrjRG8D2w5CKwH
	PeG6dhjxqWd9e0A0dTxqUixAzcfjSJaytif3KX13kKxvT6NJLJVxkB/Cp2grN+yN
X-Gm-Gg: AYBFou37O132UublXmX0dfHepwNnQuINllrQUm+WttjQJcXgC9gbFJ7KngGrSN6HNDv
	PR1iPUskU30/Vc+9C43O3jYzFVVuoeOnvuKHfnf/H7b2HoC4rJH9K1dI2KR7F+HZSK+oXQV+i2p
	1CSIHbxy9iFlZx4SIXfnF5VuBl5mesQ7ekcBwKPfoAuxtmPyMeqSO+NDsD28ogKNzMugmiKrWW8
	JWjnIRq7Q6Xlfw50xK4oooKmWMUXcrgUtg4HNLh+JwbuXpOAjpHsIjp6rG88+20qYMeV3c5qX49
	6GgZY6W8PVIuYWgeWOowTXNPUOv2u1waEq5SeKwKjheTogaXL0QXNLNSRRpzaCUg+OsBPq1cktI
	BEJxEHcUBG7XzW87CKNtCk2JHB7xDF2mt8Y5xIgQIf7iFfD2e9z5+oPrD/IKttYwbXGTEWzC25n
	Qi891+G1bJJQ8hS7FkkLH7n83joPiD8EjyxKis77ise9hNO1heRAvE4DajJ/G7Ef3pYBkEs3BaV
	O3YZ1GYD5k44JtZ9GXC/XDgC+UMQgCMXxhQr+bWRbHxevJqOqUZTbn/9iE=
X-Received: by 2002:a05:7300:f497:b0:369:f3b8:2c48 with SMTP id 5a478bee46e88-369f3b8473bmr4599060eec.34.1792956032713;
        Sun, 25 Oct 2026 12:20:32 -0700 (PDT)
X-Received: from [127.0.0.1] ([172.182.226.21])
        by smtp.gmail.com with ESMTPSA id 5a478bee46e88-369e8d578casm21015137eec.6.2026.10.25.12.20.31
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Sun, 25 Oct 2026 12:20:32 -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>