Article View: linux.kernel.git
Article #1906[PATCH 2/6] object-file: use size_t for loose object sizes
From: Elia Pinto
Date: Fri, 30 Oct 2026 10:28
Date: Fri, 30 Oct 2026 10:28
155 lines
16131 bytes
16131 bytes
Since c6a4629e32 (odb: use size_t for object_info.sizep and the size
APIs, 2026-06-15), parse_loose_header() reports the object size as a
size_t. But unpack_loose_rest() still takes an "unsigned long", which
is only 32 bits on Windows. A header claiming 4GiB + 5 bytes thus
makes us allocate a 6-byte buffer, the size check added by the previous
commit is happy because the stream really has 5 bytes, and the caller
gets that buffer back together with *oi->sizep set to 4GiB + 5. Any
user of the object then reads far beyond the end of the buffer;
read_loose_object() already does so when it hashes the contents.
Use size_t for the size in unpack_loose_rest() and check_stream_oid(),
and while at it for the size of the mapped file in map_fd(),
odb_source_loose_map_object() and unpack_loose_header(), so that the
whole loose object reading path agrees on the type.
There is no test, as the reader allocates a buffer of the size claimed
in the header before it can notice that the stream is short, so a test
would need 4GiB of memory. The problem can be reproduced on Windows
with the loose_obj() helper from the previous commit:
printf short |
loose_obj .git/objects commit $((4 * 1024 * 1024 * 1024 + 5))
followed by "git cat-file commit" on the resulting object ID.
Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
object-file.c | 14 +++++++-------
object-file.h | 6 +++---
odb/source-loose.c | 8 ++++----
3 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/object-file.c b/object-file.c
index 6d39138511..0fc240b1e8 100644
--- a/object-file.c
+++ b/object-file.c
@@ -155,7 +155,7 @@ int stream_object_signature(struct repository *r,
* Map and close the given loose object fd. The path argument is used for
* error reporting.
*/
-static void *map_fd(int fd, const char *path, unsigned long *size)
+static void *map_fd(int fd, const char *path, size_t *size)
{
void *map = NULL;
struct stat st;
@@ -176,9 +176,9 @@ static void *map_fd(int fd, const char *path, unsigned long *size)
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
unsigned char *map,
- unsigned long mapsize,
+ size_t mapsize,
void *buffer,
- unsigned long bufsiz)
+ size_t bufsiz)
{
int status;
@@ -209,7 +209,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
}
void *unpack_loose_rest(git_zstream *stream,
- void *buffer, unsigned long size,
+ void *buffer, size_t size,
const struct object_id *oid)
{
size_t hdrlen = strlen(buffer) + 1, bytes, n;
@@ -1135,7 +1135,7 @@ int for_each_loose_file_in_source(struct odb_source *source,
static int check_stream_oid(git_zstream *stream,
const char *hdr,
- unsigned long size,
+ size_t size,
const char *path,
const struct object_id *expected_oid,
const struct git_hash_algo *algop)
@@ -1143,7 +1143,7 @@ static int check_stream_oid(git_zstream *stream,
struct git_hash_ctx c;
struct object_id real_oid;
unsigned char buf[4096];
- unsigned long total_read;
+ size_t total_read;
int status = Z_OK;
git_hash_init(&c, algop);
@@ -1203,7 +1203,7 @@ int read_loose_object(struct repository *repo,
int ret = -1;
int fd;
void *map = NULL;
- unsigned long mapsize;
+ size_t mapsize;
git_zstream stream;
char hdr[MAX_HEADER_LEN];
size_t *size = oi->sizep;
diff --git a/object-file.h b/object-file.h
index f44758c4f8..a8da9dcd22 100644
--- a/object-file.h
+++ b/object-file.h
@@ -157,11 +157,11 @@ enum unpack_loose_header_result {
*/
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
unsigned char *map,
- unsigned long mapsize,
+ size_t mapsize,
void *buffer,
- unsigned long bufsiz);
+ size_t bufsiz);
void *unpack_loose_rest(git_zstream *stream,
- void *buffer, unsigned long size,
+ void *buffer, size_t size,
const struct object_id *oid);
int parse_loose_header(const char *hdr, struct object_info *oi);
diff --git a/odb/source-loose.c b/odb/source-loose.c
index 50cb388c35..d77e93e7c4 100644
--- a/odb/source-loose.c
+++ b/odb/source-loose.c
@@ -73,7 +73,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
{
int ret;
int fd;
- unsigned long mapsize;
+ size_t mapsize;
void *map = NULL;
git_zstream stream, *stream_to_end = NULL;
char hdr[MAX_HEADER_LEN];
@@ -250,7 +250,7 @@ static int open_loose_object(struct odb_source_loose *loose,
static void *odb_source_loose_map_object(struct odb_source_loose *loose,
const struct object_id *oid,
- unsigned long *size)
+ size_t *size)
{
const char *p;
int fd = open_loose_object(loose, oid, &p);
@@ -285,7 +285,7 @@ struct odb_loose_read_stream {
ODB_LOOSE_READ_STREAM_ERROR,
} z_state;
void *mapped;
- unsigned long mapsize;
+ size_t mapsize;
char hdr[32];
int hdr_avail;
int hdr_used;
@@ -356,7 +356,7 @@ static int odb_source_loose_read_object_stream(struct odb_stream **out,
struct odb_source_loose *loose = odb_source_loose_downcast(source);
struct object_info oi = OBJECT_INFO_INIT;
struct odb_loose_read_stream *st;
- unsigned long mapsize;
+ size_t mapsize;
void *mapped;
mapped = odb_source_loose_map_object(loose, oid, &mapsize);
--
2.53.0
Technical Details
Message-ID:
<ee4439354afda7637f935293d5accff91cc0ad71.1793355390.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-73873-linux-kernel-git=dewy.ruReferences:
<cover.1793355390.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-73873-linux-kernel-git=dewy.ru
From: gitter.spiros@gmail.com (Elia Pinto)
Newsgroups: linux.kernel.git
Subject: [PATCH 2/6] object-file: use size_t for loose object sizes
Date: Fri, 30 Oct 2026 11:28:35 +0100
Organization: Usenet Network
Message-ID: <ee4439354afda7637f935293d5accff91cc0ad71.1793355390.git.gitter.spiros@gmail.com>
References: <cover.1793355390.git.gitter.spiros@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2214123"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:b2vniwWS8u9+szfHU1sNxlEES7s= sha256:EMqeE8mENlHxAoXtwPEE29IHKfNPicxWxrRZ9QPi3b0=
sha1:sarEOez0u5k3tFUxDMcxpnIlfTE= sha256:8KEe78Tsshd7nrOib6wvFJZSzNm+wv55z0vWhUHN1SU=
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 AE8BF125C91; Fri, 30 Oct 2026 13:40:36 +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 2DM9azzesf_q;
Fri, 30 Oct 2026 13:40:30 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-73873-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 B8008125C8F; Fri, 30 Oct 2026 13:40:21 +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 2933421435; Fri, 30 Oct 2026 10:30:10 +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=1793356135;
b=wKDRtLal0FDu8Kf8l44sdhpa8ZzPNgL9AwNE1dQG54fsFjoChO6DNXfM3P4kF3K4TDjy
+rYl7HB785Edzb/y3wlJE1RnhkudQlfg0XnGwqgJ0X34rpr7bsX0vUst31vyVFSFVG60b
hUqU84G/9h4n0yQiWRiAhzD4u4lJ1DDlKcLo1ejqgreLkHihc8n5tiy0OtBmutzXOHNaQ
940x4Wxzc73ad0fGLK9Ctw6TLTKSNkyHQc5+iLnJKR4VFZVxilg8/8uGSJAJbCufVptKY
zuFhsYC8z3O5cwZ+UQhcDRC5VDs37yRF3Oc/acG4F13AhbY4IonoFms5cD6rK8mTozQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793356135;
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=62LH6tXgHkNXNQFKVS3eAj0D0bjF39cfuYXq447uUT0=;
b=cdK5sT6o+/i5zS4uqp+R/1pG1+9G4gns1YH307NiM+fJEUwxtIYRKXSG09pQtWpkv4Tt
MDjuPbpBi/X49psMHUOUSKGgMRUtLiUhDIndsgGV2aXjl8ZbC4SxyrdQwYDCjSZZpURje
cNsmRSjjMBpCvOl5V5EVUuhutJHqbB0pmfCpi8kQBkGVWs3VwJO5qyF2efonMRxffGKJl
edG9+wV5DnlxOWsIcn/0UbXQtI8fAuFGrjx6DlrNHIOel0NubKjEjJbAinyF44ps00pDt
8d2l6yxe0oiPRz5La/Rkf3l584Gi0tIHtELke0mSVprkUs9+7QLa/9CNUK1dUS5miIw==
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=T+njIDDB;
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 57EB3606B80E;
Fri, 30 Oct 2026 10:28:55 +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=T+njIDDB
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.45
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793356129;
b=u7FV6+YmtTZVNhQQTQOM18JvxUWgvl7QWYK1OiMGhkm2RnsC3RA+TRN7u/dBs30TLwle
6NoXUSFrenGNBHoFA/Tb+DEN1R7xrAsn3Ct4qybikUNLswr5JdTAaevSPLmchpNto+zCH
23aZd/su1rvmuG3CFY3//kuV0mrax46voSdaAxhp15d5HNOYs7iqAF5Qys21l4WLJ06WN
q9fk7ZPMGI4VOrNs3UndKmsRkM1+CAioy1biowvYD87LbIZCko0U5Ns0tzoI/AJVrCgt5
0CxPsckRdo7hYiaC/dNh5bMpdNIRudnrVcj5Q9h4CbI3532NQRrTlsRMcPmlnya3JDQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793356129;
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=62LH6tXgHkNXNQFKVS3eAj0D0bjF39cfuYXq447uUT0=;
b=44+87qsTc3JYTbaNy1T7Lo7rbdNCTeuBkhgfEm3OYt4nLC+9Dd4nf9vS7xspsBDYXbTy
Eid4aJ2x1eb8Y7qUSMklNFJcMQyVeAgX1Lu3Y7E2r/WaR/259YbXWQkD4Qe2StNRZ59e/
Calr3WTL2MuZZJWiaI/W1oiFKUSGQhY7WvipRF/vW8fmc6CukV/x7G1D9MXAWK1qAv4M9
5tIvekhd2ys6KQVycTeLPeRhqCq8m+oSIJnoAaWaV0ECpzWMdFvwNeGghacUpSX4l9e3x
59+gTq/NdvEiozhp8k4hOfgGkqaxFqjdtiGnjRoUlafWLTSVKjE3PePX/3H3hET7ZmA==
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=T+njIDDB;
arc=none smtp.remote-ip=209.85.221.45
X-Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45])
(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 28B1460A6F4E
for <git@vger.kernel.org>; Fri, 30 Oct 2026 10:28:48 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 28B1460A6F4E
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-f45.google.com with SMTP id ffacd0b85a97d-48b042c0728so123914f8f.1
for <git@vger.kernel.org>; Fri, 30 Oct 2026 03:28:48 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1793356127; x=1793960927; 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=62LH6tXgHkNXNQFKVS3eAj0D0bjF39cfuYXq447uUT0=;
b=T+njIDDB+SGYMYlgpComfSE5xEWmWFrT83Zk7eZQh08pP9ocXV8XYZs0qG0h5AURhp
nviIkthpG6eHdhbqCkOjWxiiwT9Qm5g5LVI6XE1MNY1P7IfLFtJjOi5kSucpDL3qLwAa
WRQLnl8ai+qHdzsOdiwt2SIITB5W2VKvRP/BEmbBiX0q5TcRoafS87S4HBmKCkxs7WTk
+LoyE28ul18B1D2vCRPClTT69vldoKv6szfPR5yAkiFdq2QhvSsYj1/AOyiQhlD1AeN/
1LQ17/zOEvsLUJy2uEfzrVXkRMdW1KgWWTNUkVQhlCMbvcE3zQeYvreMGiLoiJSdbsnM
Yv/Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793356127; x=1793960927;
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=62LH6tXgHkNXNQFKVS3eAj0D0bjF39cfuYXq447uUT0=;
b=czZX7kXugP3VglF9fz+naImTWGBiF/Mnjo8x4YQ5KsmbIv8VYiSPW/5bN6Y82J0LTo
pR80MTi2/hOtttROPz+qL/hCy1dAzqXlmhMZiluZpouanYL4HnBjVjS0e9J7YqO2Scet
+yUgJW/O3DHa1+H2fJsc/d6OR4edLsF8XRqvpCt+alf+xvW4i9C4b56Uh1TLdWUDO3Ch
vksaWIAyZ0Y01f+UXoV+wHURRDmnDG5N4UDSKYZLo71ByScBcBttUQXsypZhUSaOVMk9
RGDXUCO+bGgc5nQ1LXaE7LNQnxst7rMwljNAH/3M5AzzZn/J2/hWulfI2R+Hu1LlXWuq
layw==
X-Gm-Message-State: AFq9FYL7QeR2p4FHByYlKUEECT53hKiRlbHOExMfu/t+8CcEbG8NVWSP
NcgZMZ2jfw0Y0HRXtfhE+rf6D1DuhWvRmvparFbLjzcUc1q7oRgG/tnOLIDAkA==
X-Gm-Gg: AYBFou2pw/Q35M39T3znEPdkfhJIjNsS3spRW/Hq6Nv/MJ3q8jeewxIIJU0lBIQv3xY
fLlVc/QTJvZHwT12st9YOHFe4LevNu9Xk56cZLwZZ/4lZe6sXKZZpI+AnABnPO5g+wA0lkalRhb
vtfpr2Brz7B+RSvfCHg/ooSkAeDfBP6sqOYyYcQX9zQtSPA69T4tL/jW1ybTtIvZZgCtl1rTkbe
Q+HrvGmeB94sQ08C1X2Iu7wBlsqzlnktnGcrOysjyoU6oly8mrlVnRotKr+G1nIU94NgODv11Pg
bCLnvrIgOco56BWqnS6H6cZnw2FQx42rcKZ2NC/hbctxvzKNHS6DVezKiHHrURbZ6WIn+9M6o5c
HuLVr5qJVjRJrN+nrx+So2bIorI7Ps6mBGkVcNU+MKQXPA5BFBwrP/A8WLIHtHFXmYuNPn2KKma
TcFyZuB5lnpL+KGURGGJFnZM5pn1h+nGM0/Ut9bBz/nZMlIH96ciwo3R1UYERsdMpMBaN7FD9vz
a7aCH8PBGa+0imbSpFgZ7rd6pVVAVW+tKXbv52vsh6BZZZymgijGTEiHrM/Z9oObZGc/dX78dNZ
odzswLz16vtFFtHjmyXeiyGZZreWarZh+l46yIWR7RPGTvMvXQ==
X-Received: by 2002:a05:6000:4311:b0:48e:20ed:d5e7 with SMTP id ffacd0b85a97d-48e6e2ff167mr4035848f8f.34.1793356127363;
Fri, 30 Oct 2026 03:28:47 -0700 (PDT)
X-Received: from ubuntu26lts1.example.com ([205.147.30.18])
by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e71b964desm2449445f8f.46.2026.10.30.03.28.46
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 30 Oct 2026 03:28:47 -0700 (PDT)
X-Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Elia Pinto <gitter.spiros@gmail.com>,
Patrick Steinhardt <ps@pks.im>,
Jonathan Tan <jonathantanmy@fastmail.com>,
Junio C Hamano <gitster@pobox.com>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <cover.1793355390.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