Article View: linux.kernel.git
Article #1893[PATCH v2 2/4] diff: use size_t for the allocation size of word-diff buffers
From: Elia Pinto
Date: Fri, 30 Oct 2026 09:58
Date: Fri, 30 Oct 2026 09:58
52 lines
12500 bytes
12500 bytes
Since 3c7a31204f (xdiff: use size_t for buffer sizes, 2026-09-30) the
size of an mmfile_t is a size_t, but the word-diff code still grows
these buffers with ALLOC_GROW() using an "unsigned long" (in struct
diff_words_buffer) or a "long" (in diff_words_fill()) for the allocated
size.
On platforms where "long" is 32 bits wide this is not just cosmetic.
diff_words_fill() writes every word followed by a newline, so its
output can be up to twice as large as its input, which is limited by
MAX_XDIFF_SIZE to just under 1GiB. alloc_nr() computes (alloc + 16) * 3
in the type of "alloc", which overflows a signed 32-bit "long" as soon
as the buffer grows past about 700MiB. The result is undefined behavior,
in practice a negative size that REALLOC_ARRAY() turns into a huge
allocation request and a die().
The buffer in diff_words_append() is bounded by MAX_XDIFF_SIZE, so
there it is only a matter of consistency, but if that limit ever went
away, ALLOC_GROW() would truncate the requested size, and the memcpy()
right after it would overflow the buffer.
Use size_t for both.
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
diff.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/diff.c b/diff.c
index b4ac17f8ef..9483931c05 100644
--- a/diff.c
+++ b/diff.c
@@ -1959,7 +1959,7 @@ static void emit_rewrite_diff(const char *name_a,
struct diff_words_buffer {
mmfile_t text;
- unsigned long alloc;
+ size_t alloc;
struct diff_words_orig {
const char *begin, *end;
} *orig;
@@ -2192,7 +2192,7 @@ static void diff_words_fill(struct diff_words_buffer *buffer, mmfile_t *out,
regex_t *word_regex)
{
int i, j;
- long alloc = 0;
+ size_t alloc = 0;
out->size = 0;
out->ptr = NULL;
--
2.53.0
Technical Details
Message-ID:
<42d4eae98540028cbb1deba469d82d676642ca17.1793352943.git.gitter.spiros@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-73861-linux-kernel-git=dewy.ruReferences:
<20261021101640.329990-1-gitter.spiros@gmail.com> <cover.1793352943.git.gitter.spiros@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-73861-linux-kernel-git=dewy.ru
From: gitter.spiros@gmail.com (Elia Pinto)
Newsgroups: linux.kernel.git
Subject: [PATCH v2 2/4] diff: use size_t for the allocation size of word-diff buffers
Date: Fri, 30 Oct 2026 10:58:51 +0100
Organization: Usenet Network
Message-ID: <42d4eae98540028cbb1deba469d82d676642ca17.1793352943.git.gitter.spiros@gmail.com>
References: <20261021101640.329990-1-gitter.spiros@gmail.com> <cover.1793352943.git.gitter.spiros@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2207601"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:IH/p+uZvwRXpyeK55wbyhJA7l1E= sha256:f645tlX5T/YRX6xlnQvfhBcoC6Fn5Xpt74DlpLAje9A=
sha1:12orlFuDuOMsqNHEyyI/fUc8OZg= sha256:sWQ+/Qq7m5HBTB6KDrIqwETYDLQPvG3nvhzVmkud/jQ=
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 B77EC125C8D; Fri, 30 Oct 2026 13:04:14 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.948
X-Spam-Status: No, score=-1.948 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.1, 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 90Ul-E3MZgHF;
Fri, 30 Oct 2026 13:04:12 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=git+bounces-73861-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 4AF5B125C8B; Fri, 30 Oct 2026 13:04:03 +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 2618F2BDCC; Fri, 30 Oct 2026 09:59:15 +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=1793354355;
b=nuERsX3GvvjO/3q794OYTYXhdWdeMuaVrOGfCzV2xZNazw9cdBCfp9wTwq42I8FE4CLf
m9Uu+PAC7r2zdnux9FuXvzR0ENfYKn9E9ijInaImS7+q5LiocPfne95lUa84rlPzXDCyL
ITZGNL2SLetsoaRsITmIZA+uCvxqFnHLVinWSXcANvmrptfr0p749RHF32YrAIQbzO6V8
X5msZpDRZGEus+3Qs5BlUS1s/t6jU5+iDMp3rv+B+0y0Fjhz824HzA6SEASJQtHCaJJvY
mYA38PONzgwB/GRk7dbxSEFJ6Fdn3P/7D3a97WJt+Re04Sg1sQsqAsU6AyNb7Rpq4Pg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793354355;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
List-Unsubscribe:List-Owner:Precedence:MIME-Version:
Content-Transfer-Encoding;
bh=dPkKzaHYkJhh6wa7QFoKyPu7Q3qgqtI2wNOLP5sPG0o=;
b=DUL18/byYa3MGeD//pYqsKBqnWici46i3SFKyLrGh88FUZqG8JJ2Ii2vKG7HSJbZ2a3q
Ie/NwVyxNRKhWYlv77KDEv2PhojQvMeGo0lr38/36jsOv+Q9HPkOu5OG7Cy4uDfPcoVDY
avANfJm0/DjcG7/c9jevb+uEb9dEpGlpRR4UEgPC+gzRZj43qeEILIswzCAWWNIaNQ94d
elp73P89vgzhbs26VbF8mRqukTtJYnPzloJuhqRW+emXMqXHblSKIoffvoW+4jLN6kbk7
k3e8Js4vO8ujF+1INQog3b4d58njKRFsrm6gP5GUJiAGZ6n17S68idpsiz5ZM/BUL8Q==
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=20260707 header.b=cvvieoqL;
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 DDEC1606A322;
Fri, 30 Oct 2026 09:59:14 +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=20260707 header.b=cvvieoqL
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.50
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793354344;
b=hPWhmdi8iHkLGu1P0d9BtWQs3m0NyUZChvuP3byahqbApi69uDNsKrgeI2QzUrxJUi2W
iwCSafyl5OqBsZ+ch+fXRSkHjs1L7nNZ8iH5eJvLoiQ+leonhQMjP2L55qHk59aILt89V
8X7imDU1bCBlegZGzsYHhFuyDuV+NWtXGhwtRsUVD8ay4uLujNGjGoyMWN8OSDdUlvysQ
ZYEcxIGLN5DfIsnwt7b70l0yjw6WxJS/vO4Nb+u2jL3IpHxFNuxoJGNeSorZ7AMGYE1Vj
gC1ULabYAx5SUQ68kbAl8Fd/0iYWbQ/4uM8RlPfwhvPhDBXqBQHK8r8fA7zYUyGk0Zw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793354344;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
Content-Transfer-Encoding;
bh=dPkKzaHYkJhh6wa7QFoKyPu7Q3qgqtI2wNOLP5sPG0o=;
b=Nn/JJd74FgxvO2VqweSSMvzMRt5OtmJlJMFNi8EJ9N9hOP236zq4zvfdkESqoC0u2xFn
hhD0qjA5DGNO+xnKeCVknnnB15pN7KphrCNOT1Dilg9q/Ro8vfxR0yq8x/eDbOJliB9tD
gB/u3oEa6g3o72lsSt9P8asbXNnrFK4co9+2BZEPUosH/nGeGf6Qw0Xhmq3lkJ8mtgQpl
L5ZFomNqiz8kzBSkb8ZuUlOCjQ6E4x56lbVHH8va5WIy5z6j+emD5Q7rnY3XMdqmctqnF
8X9dngLUjlvEpTQ6HEg+qYrYDOU95JVCXjqzzeuAbnZL276B/MzPk5YJkhI19EQcR+Q==
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=20260707 header.b=cvvieoqL;
arc=none smtp.remote-ip=209.85.221.50
X-Received: from mail-wr1-f50.google.com (mail-wr1-f50.google.com [209.85.221.50])
(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 A410A606A32D
for <git@vger.kernel.org>; Fri, 30 Oct 2026 09:59:03 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A410A606A32D
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-wr1-f50.google.com with SMTP id ffacd0b85a97d-48df0fa43c5so273781f8f.0
for <git@vger.kernel.org>; Fri, 30 Oct 2026 02:59:03 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1793354342; x=1793959142; darn=vger.kernel.org;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=dPkKzaHYkJhh6wa7QFoKyPu7Q3qgqtI2wNOLP5sPG0o=;
b=cvvieoqLQQ7e7QgsIWN0arVsfCEajft2XKVYcUX2Q4BNE3kog8dKkrpUcz+uc2B1A3
q14jGD2Vnn8SsdvM2EefjYjEueqB7tZ4VBvuPofaFoQxDDHGovmPSvhKWUQ6V3RIOzc0
bNTlSgB2xUbHyotsuF77OgJ+zpfYtKO3Ja4LCve3UQq9aOlWIibEvWPDQ7HWvqxVekWv
lLHfQrT6sYEzwmpstzYwLVYrGIwpR+O3xXNFlxsfvopXbPOKTccnEkbUXLM/AgNR8xyM
udN9WqGLrL589kA4KkT921f7dl1kR4lMXjA/hcMRVEtoaRte0Ph7UsmvaQVtGJU5LpBs
vMpg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793354342; x=1793959142;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh=dPkKzaHYkJhh6wa7QFoKyPu7Q3qgqtI2wNOLP5sPG0o=;
b=PkxyG48drWuU5b4BSN3VzKl25JB0NLmVSh+YFyhHx/AToFynUxlTfOpp/88224ii6N
jHos6W02I7/Hm8P2zZeYFgCvixg5/rG8jQWCD2yW8TeOuZLRgpbENoInplvGsOR4WxC4
WwIYaqj+H3cbW3oeXoc8AAmcm+w5efSngfmGKJIFC8PW6gQVN2RzbdXfazTCDqf4lmy5
0kxWY7Vha/8LvtQGvfjx6fPAhxF0Qksyz4VlgnEJAHXFL87zJ+QHo7BVbKnJc+sm73Xt
09Z+EQwQCnhcGQEoVsKMLiwOq2T/5++sOoiL5l+DxMCN3IcARRro8ntsYXpbHRIRezAS
iDYw==
X-Gm-Message-State: AFq9FYJiDLwIzULfagkdyylBmRAGiO1qpu04J7SN/EapIa7MzIQbLfCK
VV3xVXFE4vnjdYwLpOwOQwVoWZ78Cpf7lblU5R+USXto2+f8Icv5nKHBcwNUujOS
X-Gm-Gg: AYBFou0ZkmyYrgOXvuEg+3YAGnmtsTteyU+29ZWYZpJlrwFlJKUH1+dwfeqtS6b93/Q
uH4Uf5igMcQT1ej6y+Np/qJTgCadsznXHRVdjFFSNJ6n9Wer/peX+IFN3HY63YdPbsDqt2kW3xI
VcOye0wBV8ScbQ54OrAINT+RQBqgBlxMfMdCVRuI3bx7d4PkAx9D6jI6gXvot2mCzhxavC5YmKh
nch+E/eLIEhc2IGhuXVY2XTeKtdLkrkXtBvnQJIsdfDSqqNVocJWYBKAOfkakh15Z/1oHu2+Zom
O88WWZMI3ulzyG4UaJJZ5nwERG2dqP/wkbuaRpLn6XbrMAkjDZ8xxKI6hFBiVVyEavZFEERhJ43
YUQY2R9pj9W3E4fu7ErkWGUWyEW/NNgd1xD4Q2XkJ/5pMXREQakixFReOqDvfhpRqlp7Vk3xKlf
dJi88fgHl/mW9RXz/eyss0q6Xs2bXu0hYNoiUwtJADnIMd0quDW/qOMYTJ4FnxV8nT9Khz8M8ph
0Ql5D0lbDhzq/sOj8/ciiwCnVUsfIwsOiK7Vfi0E4iHetcWTwCpbdDUCynTXZwuYrBTdwzg/l27
YIomT9+Mw2tnNQ122Law2duDVKFpNuH+KfvAhJNALfjWj/twZA==
X-Received: by 2002:a05:6000:2007:b0:48e:5e63:27fb with SMTP id ffacd0b85a97d-48e6e2dd32fmr4711947f8f.20.1793354342000;
Fri, 30 Oct 2026 02:59:02 -0700 (PDT)
X-Received: from ubuntu26lts1.example.com ([205.147.30.18])
by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e71b9b54dsm2359421f8f.47.2026.10.30.02.59.01
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 30 Oct 2026 02:59:01 -0700 (PDT)
X-Cc: Patrick Steinhardt <ps@pks.im>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Jeff King <peff@peff.net>,
Elia Pinto <gitter.spiros@gmail.com>,
Junio C Hamano <gitster@pobox.com>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <cover.1793352943.git.gitter.spiros@gmail.com>
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