Article View: linux.kernel.git
Article #3232[PATCH v2 2/5] object-file: use size_t for loose object sizes
From: Elia Pinto
Date: Tue, 17 Nov 2026 11:38
Date: Tue, 17 Nov 2026 11:38
157 lines
16285 bytes
16285 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 it as an "unsigned long".
On 64-bit Windows, where "long" has 32 bits while size_t has 64, the
size is truncated on the way. 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 5d03ae94c7..779167f789 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;
@@ -1136,7 +1136,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)
@@ -1144,7 +1144,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);
@@ -1204,7 +1204,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:
<eca5d3ddb88e03ae647a66d861a4c89a4c490fd7.1794914210.git.gitter.spiros@gmail.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!git+bounces-75599-linux-kernel-git=dewy.ruReferences:
<cover.1793355390.git.gitter.spiros@gmail.com> <cover.1794914210.git.gitter.spiros@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!git+bounces-75599-linux-kernel-git=dewy.ru
From: gitter.spiros@gmail.com (Elia Pinto)
Newsgroups: linux.kernel.git
Subject: [PATCH v2 2/5] object-file: use size_t for loose object sizes
Date: Tue, 17 Nov 2026 12:38:55 +0100
Organization: Usenet Network
Message-ID: <eca5d3ddb88e03ae647a66d861a4c89a4c490fd7.1794914210.git.gitter.spiros@gmail.com>
References: <cover.1793355390.git.gitter.spiros@gmail.com> <cover.1794914210.git.gitter.spiros@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="2317261"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:ofDOOi3LbERzOb2rxZc7yNi0Fzc= sha256:JAKbdJPP3keDS+xZ25pWAr7NRHgZlhM4IhqpKWuA8Os=
sha1:YAoNe60EwL4htBnRh0k1nc/uxEo= sha256:29xPALzdr57tW077nZr1D9jOMQjFEEUk2nRpsVfhel8=
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 807F3114F27; Tue, 17 Nov 2026 14:39:39 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 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.249,
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 GqGybgXxrtr7;
Tue, 17 Nov 2026 14:39:35 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-75599-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 4F12E114F26; Tue, 17 Nov 2026 14:39:25 +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 E626737912; Tue, 17 Nov 2026 11:39:12 +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=1794915552;
b=f5H8Ts+ad5VPpzjCk/AaGO1q/MDkhptAyKVduERuOdRrwE9I5I9XV+yZabhgng5wnEj8
XYs5sFaB9BdCjviuGCanOFSSz09mUQbm1aOK1kt6EBVfbiGKoiouvpENQ64Yq2gvX76rn
BQSnX9CuS8Uu0qd8a6MsJnWGTq/t7466H/AWmMehfEuBWAhde71OHMrvcuyv3C+Y6TjFV
90MlOjyVSUTVEPPfraTzA4+OoK7+vuLEsKCY4h6CQLIOkRMaCqtWeggV47w1p3cPtDQfW
kjlmPnn6gbfe/49UHrlP1KA4elQVs+Owm4Oxo77IIZ56LHYBjqHNWDnMhWxkRCGz/Tg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794915552;
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=wmob4ynlkdHJ2SuDD5rMprDSiranZR8ZgIZfiGX/OtE=;
b=HRiQTTgnRfUVZ9jIyNswdkENqAW3wWLPDTTFHbyQszf6xXcBZpfsyio7zOO5X3CYkNRn
rjJcPa7weX5CuurjRQzZXOtE/U0AcMT3sBbyOPoQlo0Fl2Imaf4AZjFBn3Zl4LSrw+gGz
E7ckbkWAuYkERIKDqmhRN69dMbcYj5JFeB9VNliOA0QYNiXyr+O/ulZYPjol8LIvnJjNe
lzrb2hizMCcHtvYltXsDbLrEDazwOkK29QMrlohMBpe9SPGgQ+ArX71/tHRm03TQPRKyr
pm5tx2zDlJ1GLRQQTiItzo+xmCc3Ai3OP2cL22/Xv2jFSw2JXqdEsD+QnkyiyahPazg==
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=HxfyLVO3;
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 BD66B606A330;
Tue, 17 Nov 2026 11:39:12 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=HxfyLVO3
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.44
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1794915551;
b=I22o0XGOou6EYKyrNpVDkHibTia95y0dNKHCZGbQiIowBABlotXGls7iTQ2ZgRtZSJOz
u1kya2OKHFpPRDjJnxfKNIkvYh2eIhjtls6pVZRVQoPBuYKsvlSmbtTp0Og0PW67L4HG/
+Btt1P22y/CoIvbRMxI0Vvsy2W7NqB5m2UYEPFvmW+3JxJ2uwrihLosE/EMNDPEnQwlKs
LO8PK6qjPo1N8PWEJxJPOt3NbXKkeThbiHqVzkYfNVSisJtQn5pvVWBd+1bUwepESu5ZU
VxsxEGGWOCdNSf7lJc87V3AburfpnfQi+X5JNcLOGZ5sjJd5a4A0mfRkGNKY3Z3Oi0w==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1794915551;
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=wmob4ynlkdHJ2SuDD5rMprDSiranZR8ZgIZfiGX/OtE=;
b=peqXui/mGtWV3ILKbL1AkBYuDBTycf2auUIQ1F8FHdRxidLpCgFw0gX3zWClShvj/6jl
Ut5FCGnkQSHhSB/LXJ+8XK9gNaWmO7JccvYqE7bLI2ns7iyci52Bg9aZzu+M7lkM1KIHY
tabuF2Nt7LlSnlrW7E/e/lW21NDhvtQxvRljjvPauKP4Q526A6R+OtyTfTRGsJGFAU7em
pIRpxibZAAw3vB7CyO4ivFyyw9lwyWiAiyvU06lV2oWkG2ZnA8JwDxc6d7JsFLZErB55e
R2yDf82iOyoC0cKbVY5IbhmLRzcjC4765PtDRJWO7o0FDpUSYtVl1oo6C0pKeehGpZg==
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=HxfyLVO3;
arc=none smtp.remote-ip=209.85.128.44
X-Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44])
(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 885C3606A332
for <git@vger.kernel.org>; Tue, 17 Nov 2026 11:39:11 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 885C3606A332
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-f44.google.com with SMTP id 5b1f17b1804b1-4bb55cda775so5865795e9.0
for <git@vger.kernel.org>; Tue, 17 Nov 2026 03:39:11 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1794915550; x=1795520350; 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=wmob4ynlkdHJ2SuDD5rMprDSiranZR8ZgIZfiGX/OtE=;
b=HxfyLVO3PK+e6GQ+SDj5okY35sQH9bRyRPa2mcaiZtsMxtXRgAqiRmrxIGUtvuzfZI
I+WNAOxDRuYBzvjIadJlRRlFQfmCO+fIyy030SLJqqEgvAkdomBUF8g+L+KjUoTjnaxB
Beiv4xgUAZs8TfmN3dSGQiZpQF+NnjucLHXlofKEP1TZD9FDm481HZqzo6YSe+ItxcwL
6qeS3SBVzbpuaw44bh9rcr63pJ55CupS03jkDTIvPSCbC3aGwfVvM8yP/ow6j+uArgID
ArTnthOhl9K+0kyulppYAQ8eEgLaDimSlWSGwp1PoXisiw8k/Gg6z8ZXw5/3li1gEbyf
LsBg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1794915550; x=1795520350;
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=wmob4ynlkdHJ2SuDD5rMprDSiranZR8ZgIZfiGX/OtE=;
b=QsbDS3JYD858v0WSRiD/BadFIqKoV1EDH9Wng7U140SRhnx8JotKhX0r49hKHH89aO
Xqjmi/xPRJUdBDtVTZxJoSlggWuB7GHGeJtjq4D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600c:384e:b0:4a1:c6f5:2450 with SMTP id 5b1f17b1804b1-4bb78399c0bmr51027425e9.17.1794915549655;
Tue, 17 Nov 2026 03:39:09 -0800 (PST)
X-Received: from ubuntu26lts1.example.com ([151.31.213.44])
by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb78420edasm93555925e9.9.2026.11.17.03.39.07
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Tue, 17 Nov 2026 03:39:08 -0800 (PST)
X-Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Elia Pinto <gitter.spiros@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>,
Jonathan Tan <jonathantanmy@fastmail.com>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <cover.1794914210.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