From patchwork Wed Oct 2 10:55:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819609 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A2FC195803 for ; Wed, 2 Oct 2024 10:55:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866532; cv=none; b=hd4XD+Lpt9NIVrsCeLTcAll3RwJyiThyV8UqyxJO2Tdwga3cvl6HB2EYiFSlZLyEUGqua6mjsFCPNhATownv3zpVsTvldsbVI6WVHftivCzv/qfUEowJdEJKlFXQQRkG862eW+SMfj4gwd3KYKMNyecnvsavT4H7aKoOzoQXDSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866532; c=relaxed/simple; bh=r/wNwOqsurCUQapI5uCBonlpAYPKOJda2FJDht6aSDg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BWYM0atHLtwg5OUcLcyO33ViVKdXSZJyHKlX8MQ2bwCRDkEBKd2pTimjMrb7QoWRMGLzOheVtpzHIpmvs+YXWxPJpDgtzvKVGH4BG4g/N/v1TmMG9MrExaiGVO9sMx46HrT6Vj6RJDZOUd2qlWf9JmWtOUZoS9O0Qm6P8XmK2tE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=JMtUSl4A; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=jJSEandR; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="JMtUSl4A"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="jJSEandR" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id 9965A1380253; Wed, 2 Oct 2024 06:55:29 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Wed, 02 Oct 2024 06:55:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866529; x=1727952929; bh=KfSspmcjDG ZrR6/wFCt+5RT2Kk+oo/sOeOkgThDD4pI=; b=JMtUSl4A4DoVJE4E/ACDLcutyK SRGcI/XPWSfhrFZ4VK+YKKnP4/2qsATXgGVfOSetsL1Up8vKGsYtEA9TzkQAVtmo 402tw9DOIa2SXXsUKph7I//DBDWF+n3JmThWsrr/6BUdazbXk1ClDAmK1JAcim0N 00Pd+JQWeERffHvEix+0XiY+bmiByvH1BUk5xPDiSEFFxdx+nb5AA7/WcakbYHbc 24kB5q4jJREXfX+kw0aqNF22mizt3QhF59r/S5MWPnqvPUOve1OBStwOQmI5JmtR 5QH9Vkajv2bysNIcpdi+bXogHsn5m4oeTCVTWT8KSKqO/RqtQ1UClr4MYMJQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866529; x=1727952929; bh=KfSspmcjDGZrR6/wFCt+5RT2Kk+o o/sOeOkgThDD4pI=; b=jJSEandR44pn3XvriSu2b0hSoju7sq56MPJxX/hFsRA2 H3j3jtfT777iYugzykxVWjaabg7ivbMbqph20UD7zr7RT1BMYleAyn/NI8HMeG4z vG/3CdyRHTMRnCVfdlwOdoEI4uRQAE3cdHS//YdRgYNMqs0THkzKrVIoWdXOIg/A qnIBzZIDSPGaP+vt21h3JtPsYi2TzedIo1OV2h/X1IB5nORmBSiuRE77b+O0qxOy M5laPkEcQk1zHOIo9YfbfGN76beBHThoOFEZ3XvGPRYczHZxNmmob0cQuvVUXMwE fbbmMAQ6Y/0/PC4HimYFfb69AAofHGo0qrrFpvduOQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomhdprhgtph htthhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopegvthhhohhmshhonhesvggu figrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:28 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id ad4c4f30 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:37 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:24 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 01/25] reftable/error: introduce out-of-memory error code Message-ID: <94eaef3ae5ee008b5140272e4ce572c041f9c5a2.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The reftable library does not use the same memory allocation functions as the rest of the Git codebase. Instead, as the reftable library is supposed to be usable as a standalone library without Git, it provides a set of pluggable memory allocators. Compared to `xmalloc()` and friends these allocators are _not_ expected to die when an allocation fails. This design choice is concious, as a library should leave it to its caller to handle any kind of error. While it is very likely that the caller cannot really do much in the case of an out-of-memory situation anyway, we are not the ones to make that decision. Curiously though, we never handle allocation errors even though memory allocation functions are allowed to fail. And as we do not plug in Git's memory allocator via `reftable_set_alloc()` either the consequence is that we'd instead segfault as soon as we run out of memory. While the easy fix would be to wire up `xmalloc()` and friends, it would only fix the usage of the reftable library in Git itself. Other users like libgit2, which is about to revive its efforts to land a backend for reftables, wouldn't be able to benefit from this solution. Instead, we are about to do it the hard way: adapt all allocation sites to perform error checking. Introduce a new error code for out-of-memory errors that we will wire up in subsequent steps. This commit also serves as the motivator for all the remaining steps in this series such that we do not have to repeat the same arguments in every single subsequent commit. Signed-off-by: Patrick Steinhardt --- reftable/error.c | 2 ++ reftable/reftable-error.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/reftable/error.c b/reftable/error.c index a25f28a43e..660d029617 100644 --- a/reftable/error.c +++ b/reftable/error.c @@ -35,6 +35,8 @@ const char *reftable_error_str(int err) return "entry too large"; case REFTABLE_OUTDATED_ERROR: return "data concurrently modified"; + case REFTABLE_OUT_OF_MEMORY_ERROR: + return "out of memory"; case -1: return "general error"; default: diff --git a/reftable/reftable-error.h b/reftable/reftable-error.h index 6368cd9ed9..f404826562 100644 --- a/reftable/reftable-error.h +++ b/reftable/reftable-error.h @@ -57,6 +57,9 @@ enum reftable_error { /* Trying to write out-of-date data. */ REFTABLE_OUTDATED_ERROR = -12, + + /* An allocation has failed due to an out-of-memory situation. */ + REFTABLE_OUT_OF_MEMORY_ERROR = -13, }; /* convert the numeric error code to a string. The string should not be From patchwork Wed Oct 2 10:55:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819610 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB29E1E767D for ; Wed, 2 Oct 2024 10:55:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866533; cv=none; b=JSLbDiHyipdCWV5OOBeJkjJzSWku4K6golp+qCJE7Co8JwxrpXlyuzSFZfyozkBiOmyH+qcRs4wN5MWPXuSg64VdzqmbdqHfxh1k76vapPzcqF5E3+0nNhf0ca+QNIVXVYGkA7T3KwEoK+rP0Sgj9HzAl6QkZrxjtwvJUSEMSPA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866533; c=relaxed/simple; bh=FLTImrbvvvFgAkSveQ7xVh//0HjBGYKPh1lsSNOHEow=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=d6yqAgjPoDOqV5TMR58/l+FFzp0n7K6DkUVRidweGZXWibrDiS2qtGZ7NX2iuT1YC8tB/HfArlqK3ABHgvTptaank8N9vUVotL3wJfoXlnLLNi8xcvOSFHGXRquqSe7UMndN29O/DLWDHpd+rBHDY1tAdU/euDiOpzlC/ipmO28= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=VcILhMk2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=CFae8vwp; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="VcILhMk2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="CFae8vwp" Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.phl.internal (Postfix) with ESMTP id 0BC191140223; Wed, 2 Oct 2024 06:55:31 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 02 Oct 2024 06:55:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866531; x=1727952931; bh=dV4fHV40tW wTaQ2ycbPOM+RJxkvB0dTi+16vDpN+s1c=; b=VcILhMk2jBNPE0S9nliFVH9WUy xj8XOHyHJdUdS85cGdHdSQh62E5UltRv7kE4LUCGBqH/3Q+ora9hTGmmp4W6Oi++ QZ9urnDiGB8dOJwaJ1An8fzYpvb66mW3jPGlEr75OH1HQoJMEpv/Z+g/SOXF1r/6 XQwb7JqimyubTCq+9W0CwYIk13JHp7UcidqFQZe7f4Pa9LUI25sp4C9REwAxQcB4 AkLCFTsPdNgop2PNhNziPaL1SIaUna21yTilhn2naVodcHiQTro2JN9NOGxyvUoO lwGissHjdt74wc8FU42jFAOZIzqfvsKJYzC+Rb0KnROAySxu8pWgjXqhGPSA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866531; x=1727952931; bh=dV4fHV40tWwTaQ2ycbPOM+RJxkvB 0dTi+16vDpN+s1c=; b=CFae8vwpoPF/OXBB0NMA1FQwCbOISezor15ewN5RJ6fB FenT/u0w3vm2ap+DOtTTg4b5KTfoy2RkZ62EidcLcvthmbMxBG5Muik1pz1e89Uy xENddBom5fiErDIM84jCCkbmikt3IpsSKnfUX0U6gFLORHYMX8wSWq+NBTAqc89Q fCkm3JOuL+F+Y0wAMt+A7QAKdNgyas9qy9KoczcWdF+6C5mQIZedzmB/p4QTLvKU 2WU3pbH2/XOZODDR4yloVlpZEZtrd7fc8L9cFq1aNb/4CFtQm42j16ZuGmCOXZw9 MpKMErq5poIReVHEnHFlXsjSrVWSSQY4yGCazjr6qg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepueeiueduhfevhfekiefgkefggeeljedtffetheehtefh hfdvledukeekfffhffdvnecuffhomhgrihhnpehgohhoghhlvgdrtghomhenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm pdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegvth hhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihht sehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhdrshdrrhesfigvsgdrug gvpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:29 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id c5a57499 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:40 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:27 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 02/25] reftable/basics: merge "publicbasics" into "basics" Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The split between "basics" and "publicbasics" is somewhat arbitrary and not in line with how we typically structure code in the reftable library. While we do indeed split up headers into a public and internal part, we don't do that for the compilation unit itself. Furthermore, the declarations for "publicbasics.c" are in "reftable-malloc.h", which isn't in line with our naming schema, either. Fix these inconsistencies by: - Merging "publicbasics.c" into "basics.c". - Renaming "reftable-malloc.h" to "reftable-basics.h" as the public header. Signed-off-by: Patrick Steinhardt --- Makefile | 1 - reftable/basics.c | 55 +++++++++++++++++++++++++++++++ reftable/basics.h | 3 ++ reftable/publicbasics.c | 66 -------------------------------------- reftable/reftable-basics.h | 18 +++++++++++ reftable/reftable-malloc.h | 18 ----------- 6 files changed, 76 insertions(+), 85 deletions(-) delete mode 100644 reftable/publicbasics.c create mode 100644 reftable/reftable-basics.h delete mode 100644 reftable/reftable-malloc.h diff --git a/Makefile b/Makefile index e3abf0ba83..39b10923d4 100644 --- a/Makefile +++ b/Makefile @@ -2683,7 +2683,6 @@ REFTABLE_OBJS += reftable/error.o REFTABLE_OBJS += reftable/block.o REFTABLE_OBJS += reftable/blocksource.o REFTABLE_OBJS += reftable/iter.o -REFTABLE_OBJS += reftable/publicbasics.o REFTABLE_OBJS += reftable/merged.o REFTABLE_OBJS += reftable/pq.o REFTABLE_OBJS += reftable/reader.o diff --git a/reftable/basics.c b/reftable/basics.c index 0058619ca6..cf072935c8 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -7,6 +7,49 @@ license that can be found in the LICENSE file or at */ #include "basics.h" +#include "reftable-basics.h" + +static void *(*reftable_malloc_ptr)(size_t sz); +static void *(*reftable_realloc_ptr)(void *, size_t); +static void (*reftable_free_ptr)(void *); + +void *reftable_malloc(size_t sz) +{ + if (reftable_malloc_ptr) + return (*reftable_malloc_ptr)(sz); + return malloc(sz); +} + +void *reftable_realloc(void *p, size_t sz) +{ + if (reftable_realloc_ptr) + return (*reftable_realloc_ptr)(p, sz); + return realloc(p, sz); +} + +void reftable_free(void *p) +{ + if (reftable_free_ptr) + reftable_free_ptr(p); + else + free(p); +} + +void *reftable_calloc(size_t nelem, size_t elsize) +{ + size_t sz = st_mult(nelem, elsize); + void *p = reftable_malloc(sz); + memset(p, 0, sz); + return p; +} + +void reftable_set_alloc(void *(*malloc)(size_t), + void *(*realloc)(void *, size_t), void (*free)(void *)) +{ + reftable_malloc_ptr = malloc; + reftable_realloc_ptr = realloc; + reftable_free_ptr = free; +} void put_be24(uint8_t *out, uint32_t i) { @@ -121,3 +164,15 @@ int common_prefix_size(struct strbuf *a, struct strbuf *b) return p; } + +int hash_size(uint32_t id) +{ + switch (id) { + case 0: + case GIT_SHA1_FORMAT_ID: + return GIT_SHA1_RAWSZ; + case GIT_SHA256_FORMAT_ID: + return GIT_SHA256_RAWSZ; + } + abort(); +} diff --git a/reftable/basics.h b/reftable/basics.h index c8fec68d4e..4e2e76014a 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -14,6 +14,7 @@ license that can be found in the LICENSE file or at */ #include "system.h" +#include "reftable-basics.h" /* Bigendian en/decoding of integers */ @@ -71,4 +72,6 @@ void *reftable_calloc(size_t nelem, size_t elsize); struct strbuf; int common_prefix_size(struct strbuf *a, struct strbuf *b); +int hash_size(uint32_t id); + #endif diff --git a/reftable/publicbasics.c b/reftable/publicbasics.c deleted file mode 100644 index 44b84a125e..0000000000 --- a/reftable/publicbasics.c +++ /dev/null @@ -1,66 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#include "system.h" -#include "reftable-malloc.h" - -#include "basics.h" - -static void *(*reftable_malloc_ptr)(size_t sz); -static void *(*reftable_realloc_ptr)(void *, size_t); -static void (*reftable_free_ptr)(void *); - -void *reftable_malloc(size_t sz) -{ - if (reftable_malloc_ptr) - return (*reftable_malloc_ptr)(sz); - return malloc(sz); -} - -void *reftable_realloc(void *p, size_t sz) -{ - if (reftable_realloc_ptr) - return (*reftable_realloc_ptr)(p, sz); - return realloc(p, sz); -} - -void reftable_free(void *p) -{ - if (reftable_free_ptr) - reftable_free_ptr(p); - else - free(p); -} - -void *reftable_calloc(size_t nelem, size_t elsize) -{ - size_t sz = st_mult(nelem, elsize); - void *p = reftable_malloc(sz); - memset(p, 0, sz); - return p; -} - -void reftable_set_alloc(void *(*malloc)(size_t), - void *(*realloc)(void *, size_t), void (*free)(void *)) -{ - reftable_malloc_ptr = malloc; - reftable_realloc_ptr = realloc; - reftable_free_ptr = free; -} - -int hash_size(uint32_t id) -{ - switch (id) { - case 0: - case GIT_SHA1_FORMAT_ID: - return GIT_SHA1_RAWSZ; - case GIT_SHA256_FORMAT_ID: - return GIT_SHA256_RAWSZ; - } - abort(); -} diff --git a/reftable/reftable-basics.h b/reftable/reftable-basics.h new file mode 100644 index 0000000000..6e8e636b71 --- /dev/null +++ b/reftable/reftable-basics.h @@ -0,0 +1,18 @@ +/* + * Copyright 2020 Google LLC + * + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file or at + * https://developers.google.com/open-source/licenses/bsd +*/ + +#ifndef REFTABLE_BASICS_H +#define REFTABLE_BASICS_H + +#include + +/* Overrides the functions to use for memory management. */ +void reftable_set_alloc(void *(*malloc)(size_t), + void *(*realloc)(void *, size_t), void (*free)(void *)); + +#endif diff --git a/reftable/reftable-malloc.h b/reftable/reftable-malloc.h deleted file mode 100644 index 5f2185f1f3..0000000000 --- a/reftable/reftable-malloc.h +++ /dev/null @@ -1,18 +0,0 @@ -/* -Copyright 2020 Google LLC - -Use of this source code is governed by a BSD-style -license that can be found in the LICENSE file or at -https://developers.google.com/open-source/licenses/bsd -*/ - -#ifndef REFTABLE_H -#define REFTABLE_H - -#include - -/* Overrides the functions to use for memory management. */ -void reftable_set_alloc(void *(*malloc)(size_t), - void *(*realloc)(void *, size_t), void (*free)(void *)); - -#endif From patchwork Wed Oct 2 10:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819611 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7426E1EC00A for ; Wed, 2 Oct 2024 10:55:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866539; cv=none; b=UKpOQixd0rP3HuHlrT/r6CWTo0QP2qyN/jp/f9XJ96tiHEMm3NpOThs3kPt9xiG2JS8YcgmnLOZberwaSjt6kiCBtgdPybN4vUhJJTBFEkxq2O6xQz6Wwszh81kzknVp1a2C5AROGpLRjrw87k5Godg0nQ8JHdDni4rIdwsKZRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866539; c=relaxed/simple; bh=b2gm5e6VuNcFw4M9Tw2VQu8j+T/hUd4g9wcPToddLhg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Fo/WpbZdU0vYzBJMKt5LfCfAiA9AeDzNG7wvJSXEFgKg9j6PkRf0x/rcriJpq5uvWKKEqLaQQZIegx8J08GeyknyWyKXSa/Wz6JAihRduEVwUrmVLcNq08obdbzqszOBScNTdwweYIyuZzpaxwieyWeN0YmT2Xq+m8vMA1+RQn4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=Lu+DjZjk; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=hDisJIi3; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="Lu+DjZjk"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="hDisJIi3" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 874171140236; Wed, 2 Oct 2024 06:55:36 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Wed, 02 Oct 2024 06:55:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866536; x=1727952936; bh=kaL379fZ/R biuQABPX9C936DoOsFwVDWSc6IHeiN1qE=; b=Lu+DjZjkVCfTaTTdG1mH8H0ihF AuUcrQy54syp+rkgxJS3rjAWIm84buAEXMpqfQweziy8xOWVIR56tkji0nPz1g4j pa0zn5BzBj9rplUPnBQURxVqtXJ+Na7C0067Wl2ASCTP/Fgg7AAsmSkv6ibpG0uZ DAEx2+hDGLdGx0JdqH0q+M5NkPhaaW+GM6T9L7ESaDe+PMnbru3pHoN+XITn7PwK 1l7lOLL7YF6+Z1SJwSLv75ai0w637ApPw0aDcLX4SHqPQJMbj4/I+gOO5wCgDtcq DwP6QGhwTiM6hFE/3XJF/cxJLx/VVouM6s6VilSSfNEggmT3gr03GCL1zPeQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866536; x=1727952936; bh=kaL379fZ/RbiuQABPX9C936DoOsF wVDWSc6IHeiN1qE=; b=hDisJIi3uMWRcI40IPXLMWACpO/6P7J0neLG5b0LDnFX k5fF1Yxl2tjhXa4r2FHvVCJLJvsHQ0Ck3VtjV3ua1+I1R5CpJ/EFaWJGc9u88kFS L8y5BkL7zNnKQqLVav9XgMZAxCb6KbVtkeDV5vQ4OAH3AO2Za95Q9orYWYeNmj7Q 2Q0towD8cc3tmVoXDYc1/6yufnYCcUuPJfWdkWCHBR8XUZawzPy3/odM+KpleY/S aNbXlAwrwNEbjGaesZrunN1PyMko77IhKE8/c7l1jb3WZqpmdP7YYT8MH5teY2/W yqdQD/bZHnm/TwGes99bCv7KWkB51qfWkrJKcSJT7Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomhdprhgtphht thhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:35 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 3e1b3e3f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:45 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:30 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 03/25] reftable: introduce `reftable_strdup()` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The reftable library provides the ability to swap out allocators. There is a gap here though, because we continue to use `xstrdup()` even in the case where all the other allocators have been swapped out. Introduce `reftable_strdup()` that uses `reftable_malloc()` to do the allocation. Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 10 ++++++++++ reftable/basics.h | 1 + 2 files changed, 11 insertions(+) diff --git a/reftable/basics.c b/reftable/basics.c index cf072935c8..4adc98cf5d 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -43,6 +43,16 @@ void *reftable_calloc(size_t nelem, size_t elsize) return p; } +char *reftable_strdup(const char *str) +{ + size_t len = strlen(str); + char *result = reftable_malloc(len + 1); + if (!result) + return NULL; + memcpy(result, str, len + 1); + return result; +} + void reftable_set_alloc(void *(*malloc)(size_t), void *(*realloc)(void *, size_t), void (*free)(void *)) { diff --git a/reftable/basics.h b/reftable/basics.h index 4e2e76014a..f107e14860 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -54,6 +54,7 @@ void *reftable_malloc(size_t sz); void *reftable_realloc(void *p, size_t sz); void reftable_free(void *p); void *reftable_calloc(size_t nelem, size_t elsize); +char *reftable_strdup(const char *str); #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc))) #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x))) From patchwork Wed Oct 2 10:55:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819612 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D54A71EC010 for ; Wed, 2 Oct 2024 10:55:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866541; cv=none; b=nCCur2s+lrGyPliJ8QFHbUhEmJbDFbIyORrJ74wdQnCw2fIAAvC/QTI05ETZSvECjmhSjp+2aCprNm9EKqlF75XHxDHtauSqNeOExRQJSw1ZweAEc8tU4oK3W93Cd1FBQLEGGuBlw4Ui8Vf88lHTxJn0OBnULO+gIQ212pP8ES4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866541; c=relaxed/simple; bh=DdozzQ67/6CahAwn1lWGRpV7LYsH0IYwH8QW8YJUXcg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=pdspvuaYj5amJRUKFcqZXRQzG8f8dArxu5E8D3Jh5paNKni/u2R2JpU8W6u6I+j00a7ZuEC7IrAyrif8z85vrmS8PK5LixN3ZcgpvkbYp6phMJ8GaV5Fr2KpK2uqvG/FDGmewwNeE39Ny8zH5DJ7iHZ7FLwNs79mci0Yaeh6JeQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=TGMdHXqe; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=hgvP4AKi; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="TGMdHXqe"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="hgvP4AKi" Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfout.phl.internal (Postfix) with ESMTP id 2ACD71380601; Wed, 2 Oct 2024 06:55:39 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-10.internal (MEProxy); Wed, 02 Oct 2024 06:55:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866539; x=1727952939; bh=4wnjwnoBmB Fx7OgfS04fXzrxXw2NZlfv92BUkTkLdGI=; b=TGMdHXqeVEq3xDIyX1A/s52+/6 kkI6JjfrUAyO+RP5mcz4u5cScozZxqZkO90kxckRWtOuSdGTCKWB5/yqlAu1b5NE Fa3p3JIQWpPwOZT3866bPv9wZpx1P1ex8kTdmuk7Qqe3qI1T1HzkWxAZFHS9OM63 j4SwihwgdhdSkvFIOja7F9otCyhOX81vS/eXjGoNM4RlT3bqL44t0SLf2I7EXMiN EiVcmmxUy0nrT64GcwzVC9jchjF0acvP7HUkMuaVjD4rbAbojbtLcDSE1PaOJB8U 2vAnQTxVSlNxiouQHP+KoE1dFijgQXAUK4rB9cQbkZ72NJnjLKzXphqpeHHw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866539; x=1727952939; bh=4wnjwnoBmBFx7OgfS04fXzrxXw2N Zlfv92BUkTkLdGI=; b=hgvP4AKiX2Cl0Hg2HB/y+XYm1HBo5LTJOBSUb8x7MoBK Tyv82EUjZsvWxmh4nO1ic8n/PInAjSDMjlYyJ7esJ77Q4Aww9cA01yW4VcRl8MSX vGcvrHNTNg0gsP1a22nl8hGmi7YpHhDPCfmkgC48US1IpXeGcbbyZuINX52z4GFR lPCuoJ+N1DPCk3jZj3TDbN3a9EHmFfzDUvUwIefjAQiO64s9kGxNbcY0h/NTpBNo pXte6w+dt++C5x17sN/iIh/gFCE2Ed+m2txzqytUrGd6sIGWAQkEsvOrhZ2noqVl nK84gofvvFoLmM7EoH1YQcsogbeCcpkTTGQWVBuRfg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepgh hithhsthgvrhesphhosghogidrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgv rhhnvghlrdhorhhgpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomh hsohhnrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:37 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id e5c375ab (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:48 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:35 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 04/25] reftable/basics: handle allocation failures in `reftable_calloc()` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reftable_calloc()`. While at it, remove our use of `st_mult()` that would cause us to die on an overflow. From the caller's point of view there is not much of a difference between arguments that are too large to be multiplied and a request that is too big to handle by the allocator: in both cases the allocation cannot be fulfilled. And in neither of these cases do we want the reftable library to die. While we could use `unsigned_mult_overflows()` to handle the overflow gracefully, we instead open-code it to further our goal of converting the reftable codebase to become a standalone library that can be reused by external projects. Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/reftable/basics.c b/reftable/basics.c index 4adc98cf5d..3350bbffa2 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -37,9 +37,16 @@ void reftable_free(void *p) void *reftable_calloc(size_t nelem, size_t elsize) { - size_t sz = st_mult(nelem, elsize); - void *p = reftable_malloc(sz); - memset(p, 0, sz); + void *p; + + if (nelem && elsize > SIZE_MAX / nelem) + return NULL; + + p = reftable_malloc(nelem * elsize); + if (!p) + return NULL; + + memset(p, 0, nelem * elsize); return p; } From patchwork Wed Oct 2 10:55:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819613 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 607F3200105 for ; Wed, 2 Oct 2024 10:55:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866544; cv=none; b=JL/JqaFPj49JzO+XWKymaLUeydqUlO2Mik06Mg9Z5WMdlLDznrj+Jh9ClB4tO51zZ7R7S2Ao10MUW3P9+8ZzFDSkQUG7QHBzqCmuuZiIOyhoI0pdOChKHH8Vyja4CbuC3bXVIIdI7swNN4Vc7RNTVSJDITbfD7I7DpPxywmZuFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866544; c=relaxed/simple; bh=HAZn7FKJtYx9iNL7XcynSsh5SddqPpNN/am2RaaFCOE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=HruGqY2iFsB9QG+9KRkKPDBgDXAqeL1vM6/uR7Iz140JsfyfkndS45jFNy8YHxTl5wgIbtqnL2YkLbSOltLn4YDqyzaizp6wmRRpd8L7cexU6JsVD0yyCtcvofHErMGHD+Ip5X9Y3UGtbTANo6bHYkkFNzDc3AcwbHHtP/1KiNc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=LB9DGtBF; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=EGJRqgk9; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="LB9DGtBF"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="EGJRqgk9" Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id BCFF313805FE; Wed, 2 Oct 2024 06:55:41 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Wed, 02 Oct 2024 06:55:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866541; x=1727952941; bh=G4xisZdc3k gCBCaRMJypxCwjzpimxxwWG9OL8OEph+A=; b=LB9DGtBFY/WEeyFu/uQGGkpr5t Pqxs5TSb+WKRkezRmYFQ6t17fmDZ4mkJgJz+39b+gFgW3OQH67f2fglakx0GC/rN eyk4y20vwDUittyGjYXuiXOxf+0JLkMyyG/kOs1nkm04HhSlA23Z/eLJlLyDKpv/ WRJc0lFMCP5eGMC+yQ6uhI9M+VPLuADC2bKaX9/P/NBiBW91XmOh1F1lANTBRAc1 vV2pE3d42kzprlTSmPgPV/DfShJMOytplMwMVq4CjUkoRPdTjLsFaM143K1BZVm+ 0sOZ7acdoeOBbLQOCsmZ/2Qzw+0ZByrqbldD/pz/pKVhmfyRCpM1TU6bHjqQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866541; x=1727952941; bh=G4xisZdc3kgCBCaRMJypxCwjzpim xxwWG9OL8OEph+A=; b=EGJRqgk9Ls5hX+YiW/V9edDaImFUuv7SyPKZxZzZZZsc TQAZve6duCytE+pq08nSJR3rbyvYfeYbpsX9nSbKN+pjC1G9pc+HZSD80zYl+oVE gXyaXopx7ZSwlThQD6Omp7ndTEJ6zmcMOrbb6hWEJ01rWcQwNIUPoE6KIPNUt3Ed wntNeT7MOJFM6vzv/Bxc3KSvpaHh1Y9TO+D50gy8V9el/qZ+erEzn7+s+DIsBmcw R6wSJVDAYbS7VOCa+BzDrA6HRoMPdVuExrWHgvi2qDVHPSB32Px1IwchqAa0mlPj ZgNtAaFAn/UtFwT5P4ezTbZvgQAvTrhYGXoXOBRqhA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepgh hithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehgihhtshhtvghrsehp ohgsohigrdgtohhmpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomh hsohhnrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:40 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 9923bc15 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:50 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:38 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 05/25] reftable/basics: handle allocation failures in `parse_names()` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `parse_names()` by returning `NULL` in case any allocation fails. While at it, refactor the function to return the array directly instead of assigning it to an out-pointer. Signed-off-by: Patrick Steinhardt Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 20 ++++++++++++++++---- reftable/basics.h | 9 ++++++--- reftable/stack.c | 6 +++++- t/unit-tests/t-reftable-basics.c | 11 ++++++----- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/reftable/basics.c b/reftable/basics.c index 3350bbffa2..ea53cf102a 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -135,14 +135,14 @@ size_t names_length(const char **names) return p - names; } -void parse_names(char *buf, int size, char ***namesp) +char **parse_names(char *buf, int size) { char **names = NULL; size_t names_cap = 0; size_t names_len = 0; - char *p = buf; char *end = buf + size; + while (p < end) { char *next = strchr(p, '\n'); if (next && next < end) { @@ -152,14 +152,26 @@ void parse_names(char *buf, int size, char ***namesp) } if (p < next) { REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap); - names[names_len++] = xstrdup(p); + if (!names) + goto err; + + names[names_len] = reftable_strdup(p); + if (!names[names_len++]) + goto err; } p = next + 1; } REFTABLE_REALLOC_ARRAY(names, names_len + 1); names[names_len] = NULL; - *namesp = names; + + return names; + +err: + for (size_t i = 0; i < names_len; i++) + reftable_free(names[i]); + reftable_free(names); + return NULL; } int names_equal(const char **a, const char **b) diff --git a/reftable/basics.h b/reftable/basics.h index f107e14860..69adeab2e4 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -38,9 +38,12 @@ size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args); */ void free_names(char **a); -/* parse a newline separated list of names. `size` is the length of the buffer, - * without terminating '\0'. Empty names are discarded. */ -void parse_names(char *buf, int size, char ***namesp); +/* + * Parse a newline separated list of names. `size` is the length of the buffer, + * without terminating '\0'. Empty names are discarded. Returns a `NULL` + * pointer when allocations fail. + */ +char **parse_names(char *buf, int size); /* compares two NULL-terminated arrays of strings. */ int names_equal(const char **a, const char **b); diff --git a/reftable/stack.c b/reftable/stack.c index ce0a35216b..498fae846d 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -108,7 +108,11 @@ static int fd_read_lines(int fd, char ***namesp) } buf[size] = 0; - parse_names(buf, size, namesp); + *namesp = parse_names(buf, size); + if (!*namesp) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } done: reftable_free(buf); diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c index e5556ebf52..1fa77b6faf 100644 --- a/t/unit-tests/t-reftable-basics.c +++ b/t/unit-tests/t-reftable-basics.c @@ -72,13 +72,14 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) if_test ("parse_names works for basic input") { char in1[] = "line\n"; char in2[] = "a\nb\nc"; - char **out = NULL; - parse_names(in1, strlen(in1), &out); + char **out = parse_names(in1, strlen(in1)); + check(out != NULL); check_str(out[0], "line"); check(!out[1]); free_names(out); - parse_names(in2, strlen(in2), &out); + out = parse_names(in2, strlen(in2)); + check(out != NULL); check_str(out[0], "a"); check_str(out[1], "b"); check_str(out[2], "c"); @@ -88,8 +89,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED) if_test ("parse_names drops empty string") { char in[] = "a\n\nb\n"; - char **out = NULL; - parse_names(in, strlen(in), &out); + char **out = parse_names(in, strlen(in)); + check(out != NULL); check_str(out[0], "a"); /* simply '\n' should be dropped as empty string */ check_str(out[1], "b"); From patchwork Wed Oct 2 10:55:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819614 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30CDC200114 for ; Wed, 2 Oct 2024 10:55:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866546; cv=none; b=a10+/KD6sJ27nDq/y88Uzki37zjjORpfgEA1eKI6kKZn6Ou/G6lhtA0E04GtGA8kR0aTIuNQbF9gY+YqJFX1oNArzIHhG9OJIbdVnnvACi5LrS0VAysozr0uRCG1JkprbSGdGemFFAIxCOlA2y5ZDB9RMe+xs9Uw4cvD3pxAloQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866546; c=relaxed/simple; bh=qxU0Xmm3CMMGs5WTlWr/DpDEk6fwVujl5k+Xkzvwp3c=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tlqRxEw/hYyUfxdx+feiUmjzKoW73eTf2mYw9nINgyD8snzzSHiKrEcRL3orBPSm+1/S5sqDAzgy6JrMHxlk1B/OmqeURQdN+OfJdIvKfqH5jR3/r7g4sf1KuRqBqmG1m/gzm78HmenR3Ru6Gjluxs7FtYHkTOpnAdeX74iS8bI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=SUOvMA2F; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=D5/GzZhz; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="SUOvMA2F"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="D5/GzZhz" Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id 3F2961380253; Wed, 2 Oct 2024 06:55:44 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 02 Oct 2024 06:55:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866544; x=1727952944; bh=ntLRfyLoj/ jtFPMVcZ5YzVO9pb1iRg2Bwsm5QmFS4gw=; b=SUOvMA2Ft5TUzl6MYYY85F5Q1R GL/zKHBrdwHCI/CGdbDyfVWr7Xxo7YbMJVDLwyLGTdAYzrrIo7OarM3VbzPA9uvl sqaZTt5sEcX/T0zoQGlq9whLfITqXBsz58WiyXIr690CHL9E6AlNM9cEPxgZBZ5E tOlXdA93AixOUxoxhisjHlrSa3tp71ypSd5O1WQM0MBNKaJKtH9hnkNlqrz6Uv6p T1dZgFdTw5WTIEP6yU4PpXI1NOf9Gp7IouEsOodXdvQWoGdax6XP9k82sFizDvyl xwWGCnXk1Yx7m5sIKSGDPuCEic/yWcvmjl5hZRrJshhG7ZZ4F6cP54FQgm/g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866544; x=1727952944; bh=ntLRfyLoj/jtFPMVcZ5YzVO9pb1i Rg2Bwsm5QmFS4gw=; b=D5/GzZhzB8uuRPlTo/aWrykvuHK4UGhTZXsGKS4nk72Z iETb4bvZyLnRsQe0hMBVquTpAXZGeG6PSlgAPmlhabwBR18bwZosJTks2bHXrgrb 7gHTKppBYapZAyMHcn3nDX51cFq7T/UCekdpdMJGbMjm1g6tfuD0Fytfo9CL8Jok K5cne7bXX0bTLqdX4WdYUHe7qtGPnrrRGK/Q4xmmmh2+dYuYfvJjrK0UodwmuKCx H7ujtzbKrYPNEKAMDaOYnrAr6i3FzD4hJYte6tPARZ8I0Zajo4uNFDb90gz76T3Y zKtDWVWnNKt3x/As0YPqHKZN5MTcYJ6lOslPYQlAWw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepvdehvdegledtvdekkedtieekjeduteekuddvhffgleff gfdtieekgefgffeihfdtnecuffhomhgrihhnpehvrghlvddrthgrrhhgvghtpdhuphgurg htvgdrvghmrghilhdpuhhpuggrthgvrdhnrghmvgdpuhhpuggrthgvrdhnvgifnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrd himhdpnhgspghrtghpthhtohepgedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohep ghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegvthhhohhmshhonh esvggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehlrdhsrdhrseifvggs rdguvgdprhgtphhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:42 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id c6093879 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:53 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:40 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 06/25] reftable/record: handle allocation failures on copy Message-ID: <756a32c28558bfaa360304712110443d643fc303.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures when copying records. While at it, convert from `xstrdup()` to `reftable_strdup()`. Adapt callsites to check for error codes. Signed-off-by: Patrick Steinhardt --- reftable/record.c | 84 +++++++++++++++++++++++++++++++++-------------- reftable/record.h | 6 ++-- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 6b5a075b92..60fd33c9c9 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -215,13 +215,14 @@ static void reftable_ref_record_key(const void *r, struct strbuf *dest) strbuf_addstr(dest, rec->refname); } -static void reftable_ref_record_copy_from(void *rec, const void *src_rec, - int hash_size) +static int reftable_ref_record_copy_from(void *rec, const void *src_rec, + int hash_size) { struct reftable_ref_record *ref = rec; const struct reftable_ref_record *src = src_rec; char *refname = NULL; size_t refname_cap = 0; + int err; assert(hash_size > 0); @@ -236,6 +237,11 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec, REFTABLE_ALLOC_GROW(ref->refname, refname_len + 1, ref->refname_cap); + if (!ref->refname) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + memcpy(ref->refname, src->refname, refname_len); ref->refname[refname_len] = 0; } @@ -254,9 +260,17 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec, src->value.val2.target_value, hash_size); break; case REFTABLE_REF_SYMREF: - ref->value.symref = xstrdup(src->value.symref); + ref->value.symref = reftable_strdup(src->value.symref); + if (!ref->value.symref) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } break; } + + err = 0; +out: + return err; } static void reftable_ref_record_release_void(void *rec) @@ -457,23 +471,28 @@ static void reftable_obj_record_release(void *rec) memset(obj, 0, sizeof(struct reftable_obj_record)); } -static void reftable_obj_record_copy_from(void *rec, const void *src_rec, - int hash_size UNUSED) +static int reftable_obj_record_copy_from(void *rec, const void *src_rec, + int hash_size UNUSED) { struct reftable_obj_record *obj = rec; - const struct reftable_obj_record *src = - (const struct reftable_obj_record *)src_rec; + const struct reftable_obj_record *src = src_rec; reftable_obj_record_release(obj); REFTABLE_ALLOC_ARRAY(obj->hash_prefix, src->hash_prefix_len); + if (!obj->hash_prefix) + return REFTABLE_OUT_OF_MEMORY_ERROR; obj->hash_prefix_len = src->hash_prefix_len; if (src->hash_prefix_len) memcpy(obj->hash_prefix, src->hash_prefix, obj->hash_prefix_len); REFTABLE_ALLOC_ARRAY(obj->offsets, src->offset_len); + if (!obj->offsets) + return REFTABLE_OUT_OF_MEMORY_ERROR; obj->offset_len = src->offset_len; COPY_ARRAY(obj->offsets, src->offsets, src->offset_len); + + return 0; } static uint8_t reftable_obj_record_val_type(const void *rec) @@ -646,33 +665,44 @@ static void reftable_log_record_key(const void *r, struct strbuf *dest) strbuf_add(dest, i64, sizeof(i64)); } -static void reftable_log_record_copy_from(void *rec, const void *src_rec, - int hash_size) +static int reftable_log_record_copy_from(void *rec, const void *src_rec, + int hash_size) { struct reftable_log_record *dst = rec; const struct reftable_log_record *src = (const struct reftable_log_record *)src_rec; + int ret; reftable_log_record_release(dst); *dst = *src; + if (dst->refname) { - dst->refname = xstrdup(dst->refname); + dst->refname = reftable_strdup(dst->refname); + if (!dst->refname) { + ret = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } } + switch (dst->value_type) { case REFTABLE_LOG_DELETION: break; case REFTABLE_LOG_UPDATE: - if (dst->value.update.email) { + if (dst->value.update.email) dst->value.update.email = - xstrdup(dst->value.update.email); - } - if (dst->value.update.name) { + reftable_strdup(dst->value.update.email); + if (dst->value.update.name) dst->value.update.name = - xstrdup(dst->value.update.name); - } - if (dst->value.update.message) { + reftable_strdup(dst->value.update.name); + if (dst->value.update.message) dst->value.update.message = - xstrdup(dst->value.update.message); + reftable_strdup(dst->value.update.message); + + if (!dst->value.update.email || + !dst->value.update.name || + !dst->value.update.message) { + ret = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; } memcpy(dst->value.update.new_hash, @@ -681,6 +711,10 @@ static void reftable_log_record_copy_from(void *rec, const void *src_rec, src->value.update.old_hash, hash_size); break; } + + ret = 0; +out: + return ret; } static void reftable_log_record_release_void(void *rec) @@ -954,8 +988,8 @@ static void reftable_index_record_key(const void *r, struct strbuf *dest) strbuf_addbuf(dest, &rec->last_key); } -static void reftable_index_record_copy_from(void *rec, const void *src_rec, - int hash_size UNUSED) +static int reftable_index_record_copy_from(void *rec, const void *src_rec, + int hash_size UNUSED) { struct reftable_index_record *dst = rec; const struct reftable_index_record *src = src_rec; @@ -963,6 +997,8 @@ static void reftable_index_record_copy_from(void *rec, const void *src_rec, strbuf_reset(&dst->last_key); strbuf_addbuf(&dst->last_key, &src->last_key); dst->offset = src->offset; + + return 0; } static void reftable_index_record_release(void *rec) @@ -1054,14 +1090,14 @@ int reftable_record_encode(struct reftable_record *rec, struct string_view dest, dest, hash_size); } -void reftable_record_copy_from(struct reftable_record *rec, +int reftable_record_copy_from(struct reftable_record *rec, struct reftable_record *src, int hash_size) { assert(src->type == rec->type); - reftable_record_vtable(rec)->copy_from(reftable_record_data(rec), - reftable_record_data(src), - hash_size); + return reftable_record_vtable(rec)->copy_from(reftable_record_data(rec), + reftable_record_data(src), + hash_size); } uint8_t reftable_record_val_type(struct reftable_record *rec) diff --git a/reftable/record.h b/reftable/record.h index 5003bacdb0..0f53ba5443 100644 --- a/reftable/record.h +++ b/reftable/record.h @@ -44,7 +44,7 @@ struct reftable_record_vtable { /* The record type of ('r' for ref). */ uint8_t type; - void (*copy_from)(void *dest, const void *src, int hash_size); + int (*copy_from)(void *dest, const void *src, int hash_size); /* a value of [0..7], indicating record subvariants (eg. ref vs. symref * vs ref deletion) */ @@ -137,8 +137,8 @@ void reftable_record_init(struct reftable_record *rec, uint8_t typ); int reftable_record_cmp(struct reftable_record *a, struct reftable_record *b); int reftable_record_equal(struct reftable_record *a, struct reftable_record *b, int hash_size); void reftable_record_key(struct reftable_record *rec, struct strbuf *dest); -void reftable_record_copy_from(struct reftable_record *rec, - struct reftable_record *src, int hash_size); +int reftable_record_copy_from(struct reftable_record *rec, + struct reftable_record *src, int hash_size); uint8_t reftable_record_val_type(struct reftable_record *rec); int reftable_record_encode(struct reftable_record *rec, struct string_view dest, int hash_size); From patchwork Wed Oct 2 10:55:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819615 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE492200117 for ; Wed, 2 Oct 2024 10:55:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866549; cv=none; b=O/kl8buITC/WwTDAenoPIQ5vc+6BJSoDVPzVlX3XA4+nTvKMXZiC9SBYSBaE6KqxjEZAXHQqI8zzpjKgQ3bCBEkbctvUEexxheT68nhugMvTsPZ1qdPOq785RcaO6k8Ehjdz9cRETAKx5sOpNKoaMUwQuaDuy1wTf469D3o81Js= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866549; c=relaxed/simple; bh=ot6B0w+28e/TupIx92ZkFojzo9W9HHxyu8OY/qL+g6w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ADtaFZAlqNTbbhVHZSLpAv9V2AW8lmkNU8liXJBffokeF8oc/5Um56n5wwhCV9K7gPjtzivahBsTl1Sutpa4mR7CXyq9tqvqqrgDgoGFRIK/WRTRgKG7Aab+nTIQUxybOEf181Plakfw82iGPMMiritE03Qnek9KOfRcVE5I78Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=pStvdjog; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=A9CVjgzU; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="pStvdjog"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="A9CVjgzU" Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id EA7421140239; Wed, 2 Oct 2024 06:55:46 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Wed, 02 Oct 2024 06:55:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866546; x=1727952946; bh=KxA1xL6cHw NNyBOTShE8qLiR46Dx8bSgoiF1YRu9mb4=; b=pStvdjogGZK9CNMGwKDB713N2r Vhjf5kXv5KMuB9NEf8FN5XDDivPCtIs+tDEnBMAB9SVHXZeq6MkuJoab+m0YUtQ4 /JiLsSB5uZdDCxwr4o9iEbxGR2ZpG2jWN6H1lNPofuagWKOx+MFb+i33cY04v6Gq 0va8pFAbCkq/oYNVQhQXT7zUW6SOGyXAUOkF7PfvbduS/s+osEobFkOdK0BEbCkl oIWs9oHrnkye1FAzAmNBbav+jzEPouvyZqR51K2Kpv31pYMfdN0gm0qp03MKmHl+ G4s5JlI72W8GtjP8G9qPprzUHd43TPiwtRaVdua3PdUIZvQSCbKR+MyM9WgA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866546; x=1727952946; bh=KxA1xL6cHwNNyBOTShE8qLiR46Dx 8bSgoiF1YRu9mb4=; b=A9CVjgzUBojRDqLz3E3yBYoiZZbQR2/i1aSoaa7AaaTW SwMFgWYG91Y1EikqGQkIOO8kS5rx93vNte7iNjwh1Mf8TBo+lJUvGAsY/p696Bgl kgcz2X0+HD0Hodj/dNaFXgNktdCG0clcaSkBNKVvvh7xnF5vwOJIaocVSovUNB8G hI4FbFzk9IeSv2vXBmg2Ret3oaR3l+niuPRB6XO/7R94ENq5d/hvU26lVyv7qlti coK51saSsF/Z0I0rGbLOH6IvNM7C9ctOl5XXaLszKUZzP2gRRNiJt+gNMHQAfjUx ifrCxFqw0KqAjm38wZAU2VyIvKs6HM2xn2q/Os5Yhg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveffudeuuddugfehuedutedtjeefgffhtefguddtieeh ieetkefffedvteduhfeknecuffhomhgrihhnpehuphgurghtvgdrnhgvfidpuhhpuggrth gvrdhnrghmvgdpuhhpuggrthgvrdgvmhgrihhlpdhuphgurghtvgdrthiinecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhssehpkhhsrdhimh dpnhgspghrtghpthhtohepgedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepghhi thesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegvthhhohhmshhonhesvg gufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihhtshhtvghrsehpohgs ohigrdgtohhmpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:45 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id f3158ba9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:55 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:43 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 07/25] reftable/record: handle allocation failures when decoding records Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures when decoding records. While at it, fix some error codes to be `REFTABLE_FORMAT_ERROR`. Signed-off-by: Patrick Steinhardt --- reftable/record.c | 80 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/reftable/record.c b/reftable/record.c index 60fd33c9c9..787e134c9a 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -359,7 +359,7 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, uint64_t update_index = 0; const char *refname = NULL; size_t refname_cap = 0; - int n; + int n, err; assert(hash_size > 0); @@ -375,6 +375,10 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, SWAP(r->refname_cap, refname_cap); REFTABLE_ALLOC_GROW(r->refname, key.len + 1, r->refname_cap); + if (!r->refname) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } memcpy(r->refname, key.buf, key.len); r->refname[key.len] = 0; @@ -383,7 +387,8 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, switch (val_type) { case REFTABLE_REF_VAL1: if (in.len < hash_size) { - return -1; + err = REFTABLE_FORMAT_ERROR; + goto done; } memcpy(r->value.val1, in.buf, hash_size); @@ -392,7 +397,8 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, case REFTABLE_REF_VAL2: if (in.len < 2 * hash_size) { - return -1; + err = REFTABLE_FORMAT_ERROR; + goto done; } memcpy(r->value.val2.value, in.buf, hash_size); @@ -405,7 +411,8 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, case REFTABLE_REF_SYMREF: { int n = decode_string(scratch, in); if (n < 0) { - return -1; + err = REFTABLE_FORMAT_ERROR; + goto done; } string_view_consume(&in, n); r->value.symref = strbuf_detach(scratch, NULL); @@ -419,6 +426,9 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key, } return start.len - in.len; + +done: + return err; } static int reftable_ref_record_is_deletion_void(const void *p) @@ -552,6 +562,8 @@ static int reftable_obj_record_decode(void *rec, struct strbuf key, reftable_obj_record_release(r); REFTABLE_ALLOC_ARRAY(r->hash_prefix, key.len); + if (!r->hash_prefix) + return REFTABLE_OUT_OF_MEMORY_ERROR; memcpy(r->hash_prefix, key.buf, key.len); r->hash_prefix_len = key.len; @@ -570,6 +582,8 @@ static int reftable_obj_record_decode(void *rec, struct strbuf key, return start.len - in.len; REFTABLE_ALLOC_ARRAY(r->offsets, count); + if (!r->offsets) + return REFTABLE_OUT_OF_MEMORY_ERROR; r->offset_len = count; n = get_var_int(&r->offsets[0], &in); @@ -801,12 +815,17 @@ static int reftable_log_record_decode(void *rec, struct strbuf key, struct reftable_log_record *r = rec; uint64_t max = 0; uint64_t ts = 0; - int n; + int err, n; if (key.len <= 9 || key.buf[key.len - 9] != 0) return REFTABLE_FORMAT_ERROR; REFTABLE_ALLOC_GROW(r->refname, key.len - 8, r->refname_cap); + if (!r->refname) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + memcpy(r->refname, key.buf, key.len - 8); ts = get_be64(key.buf + key.len - 8); @@ -829,8 +848,10 @@ static int reftable_log_record_decode(void *rec, struct strbuf key, if (val_type == REFTABLE_LOG_DELETION) return 0; - if (in.len < 2 * hash_size) - return REFTABLE_FORMAT_ERROR; + if (in.len < 2 * hash_size) { + err = REFTABLE_FORMAT_ERROR; + goto done; + } memcpy(r->value.update.old_hash, in.buf, hash_size); memcpy(r->value.update.new_hash, in.buf + hash_size, hash_size); @@ -838,8 +859,10 @@ static int reftable_log_record_decode(void *rec, struct strbuf key, string_view_consume(&in, 2 * hash_size); n = decode_string(scratch, in); - if (n < 0) + if (n < 0) { + err = REFTABLE_FORMAT_ERROR; goto done; + } string_view_consume(&in, n); /* @@ -850,52 +873,75 @@ static int reftable_log_record_decode(void *rec, struct strbuf key, */ if (!r->value.update.name || strcmp(r->value.update.name, scratch->buf)) { - r->value.update.name = - reftable_realloc(r->value.update.name, scratch->len + 1); + char *name = reftable_realloc(r->value.update.name, scratch->len + 1); + if (!name) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + + r->value.update.name = name; memcpy(r->value.update.name, scratch->buf, scratch->len); r->value.update.name[scratch->len] = 0; } n = decode_string(scratch, in); - if (n < 0) + if (n < 0) { + err = REFTABLE_FORMAT_ERROR; goto done; + } string_view_consume(&in, n); /* Same as above, but for the reflog email. */ if (!r->value.update.email || strcmp(r->value.update.email, scratch->buf)) { - r->value.update.email = - reftable_realloc(r->value.update.email, scratch->len + 1); + char *email = reftable_realloc(r->value.update.email, scratch->len + 1); + if (!email) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + + r->value.update.email = email; memcpy(r->value.update.email, scratch->buf, scratch->len); r->value.update.email[scratch->len] = 0; } ts = 0; n = get_var_int(&ts, &in); - if (n < 0) + if (n < 0) { + err = REFTABLE_FORMAT_ERROR; goto done; + } string_view_consume(&in, n); r->value.update.time = ts; - if (in.len < 2) + if (in.len < 2) { + err = REFTABLE_FORMAT_ERROR; goto done; + } r->value.update.tz_offset = get_be16(in.buf); string_view_consume(&in, 2); n = decode_string(scratch, in); - if (n < 0) + if (n < 0) { + err = REFTABLE_FORMAT_ERROR; goto done; + } string_view_consume(&in, n); REFTABLE_ALLOC_GROW(r->value.update.message, scratch->len + 1, r->value.update.message_cap); + if (!r->value.update.message) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + memcpy(r->value.update.message, scratch->buf, scratch->len); r->value.update.message[scratch->len] = 0; return start.len - in.len; done: - return REFTABLE_FORMAT_ERROR; + return err; } static int null_streq(const char *a, const char *b) From patchwork Wed Oct 2 10:55:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819616 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CE14200121 for ; Wed, 2 Oct 2024 10:55:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866551; cv=none; b=AsXzdDq337cwv6xXEhQIVx/lq3aGwXoQZNyNyvnpEILahRwrxxc2gizUpEty4pAJMEZRortSFbZ/s3Da506Iq6C+wWHq9aC1MhqvSK/0Qft3YPfsayb7LnEHKivjamT4jiBsU1wmy6jQX2PRME5KLuJQswk34aLCQGxYk5EzK3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866551; c=relaxed/simple; bh=nfHg5NPZ610+3Gjkno3QKoUz8ezohvaUdZWAMu/Srrg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nt5LMbkmJSnjbqRlss58pdxVoQCOUPxEfUdxVqpyLpup7iVjnpkikIqkIhguUJUTiTclDNLMzDNVXjvnsJo5ZwiAmQQQw8bw0E7U4r/EUPyUaNTQGDScuBa0pO/VXpehge8Ilfl2HPfCSk9HT6Dz9Q1CEmGuW8/oJdDxd5L2jiE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=RDxssU4H; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=XxV2rYSG; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="RDxssU4H"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="XxV2rYSG" Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfout.phl.internal (Postfix) with ESMTP id 6143E1380601; Wed, 2 Oct 2024 06:55:49 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-10.internal (MEProxy); Wed, 02 Oct 2024 06:55:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866549; x=1727952949; bh=ZTNepBofGS V4l6zStTlqqGSM4JHp2/ODcUBG5HhB9cA=; b=RDxssU4HFF2daMdlhidBbWX1o3 cPcUc/mOzTOuH4iwdwJYE7ADqKULpwuHv9fbKNUYvL8i4mPQWo4vifpghk6dJq+M cSytYQHK2CGQ2x3lB3UaVqEnPPopzF3K/8H2Ppp60Hx5J690AYKR914DVQide9Wc 8YcyCrVd78XZGh+7bqnz6jEHR/Td/bIJ5YNooysBvAdtaSTtIl1QfwBaQRXI4nTq dTCcOX0dfQ1HqwKu5jREKB/HhGGcuXFVivUkeyzK5M06A77qfQyZqw+psB9a7ADo xfBc6jk4//FDp1DIIIiwn8MC1ig86vFpZRMnGuX7hA6vQqwYMIHVx+Btu3ZA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866549; x=1727952949; bh=ZTNepBofGSV4l6zStTlqqGSM4JHp 2/ODcUBG5HhB9cA=; b=XxV2rYSG2YiLiamGLf7mUxvFjZ5q/GaONfrd3Ug3h8yR X6/8AhkEQsRsb81qWTwWsPyIsqS7yWkON+YSEnHlldPZvct4Ur9XmkZbu+/yqeof U48FRExz+WRnuEzs9xVHafLO9+iqLu5KX2AFSIs4zttl+mnNIyqvnVNswwsN3l6j pSzr0Ilxiu9gVBuj98b+8+Wlik7+vLGnelhwM9EullW8bnW72F6wnz8LI7jSyYhF htZJP6/77cCvr6Mys2UUdj12FYcvJtxq048N3Sjknq014NA2KzWrkyoOGcL7rMe1 Dhxx5lCvFNSLO501q4HJlcak0jeWy5RixTCad00xLQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepvg hthhhomhhsohhnsegvugifrghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtohepghhi thhsthgvrhesphhosghogidrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:48 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id d6ea1aa1 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:54:58 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:45 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 08/25] reftable/writer: handle allocation failures in `writer_index_hash()` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation errors in `writer_index_hash()`. Adjust its only caller in `reftable_writer_add_ref()` accordingly. Signed-off-by: Patrick Steinhardt --- reftable/writer.c | 61 +++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/reftable/writer.c b/reftable/writer.c index 9d5e6072bc..ed61aaf59c 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -186,18 +186,22 @@ static int obj_index_tree_node_compare(const void *a, const void *b) &((const struct obj_index_tree_node *)b)->hash); } -static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash) +static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) { uint64_t off = w->next; - struct obj_index_tree_node want = { .hash = *hash }; + struct obj_index_tree_node *key; + struct tree_node *node; - struct tree_node *node = tree_search(&want, &w->obj_index_tree, - &obj_index_tree_node_compare, 0); - struct obj_index_tree_node *key = NULL; + node = tree_search(&want, &w->obj_index_tree, + &obj_index_tree_node_compare, 0); if (!node) { struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT; - key = reftable_malloc(sizeof(struct obj_index_tree_node)); + + key = reftable_malloc(sizeof(*key)); + if (!key) + return REFTABLE_OUT_OF_MEMORY_ERROR; + *key = empty; strbuf_reset(&key->hash); @@ -208,12 +212,15 @@ static void writer_index_hash(struct reftable_writer *w, struct strbuf *hash) key = node->key; } - if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off) { - return; - } + if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off) + return 0; REFTABLE_ALLOC_GROW(key->offsets, key->offset_len + 1, key->offset_cap); + if (!key->offsets) + return REFTABLE_OUT_OF_MEMORY_ERROR; key->offsets[key->offset_len++] = off; + + return 0; } static int writer_add_record(struct reftable_writer *w, @@ -284,11 +291,11 @@ int reftable_writer_add_ref(struct reftable_writer *w, .ref = *ref }, }; - int err = 0; + struct strbuf buf = STRBUF_INIT; + int err; - if (!ref->refname) - return REFTABLE_API_ERROR; - if (ref->update_index < w->min_update_index || + if (!ref->refname || + ref->update_index < w->min_update_index || ref->update_index > w->max_update_index) return REFTABLE_API_ERROR; @@ -296,24 +303,32 @@ int reftable_writer_add_ref(struct reftable_writer *w, err = writer_add_record(w, &rec); if (err < 0) - return err; + goto out; if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) { - struct strbuf h = STRBUF_INIT; - strbuf_add(&h, (char *)reftable_ref_record_val1(ref), + strbuf_add(&buf, (char *)reftable_ref_record_val1(ref), hash_size(w->opts.hash_id)); - writer_index_hash(w, &h); - strbuf_release(&h); + + err = writer_index_hash(w, &buf); + if (err < 0) + goto out; } if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) { - struct strbuf h = STRBUF_INIT; - strbuf_add(&h, reftable_ref_record_val2(ref), + strbuf_reset(&buf); + strbuf_add(&buf, reftable_ref_record_val2(ref), hash_size(w->opts.hash_id)); - writer_index_hash(w, &h); - strbuf_release(&h); + + err = writer_index_hash(w, &buf); + if (err < 0) + goto out; } - return 0; + + err = 0; + +out: + strbuf_release(&buf); + return err; } int reftable_writer_add_refs(struct reftable_writer *w, From patchwork Wed Oct 2 10:55:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819617 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D716A1EBFEC for ; Wed, 2 Oct 2024 10:55:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866558; cv=none; b=k3auW4s2Rgyj9+4VMcyS6V+Wpls7Np+a/3VWfh2CD8j6XVJrb55hxdVxMh0PQJuIzbLEFZL+xoXMw+gOU3BkFgdymPSOag4L1RGpCaoC3rwtLYAo2oIfK6S96Jy6JWFBWZCJri3ljyg4740eXVnKryQXe45ZbWGEaaBMOFNwgrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866558; c=relaxed/simple; bh=GMJ2R7EhbZ26MLb5z+CojS8kmfNu9cVTUleCA0frbBA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=U7gBzxJo/SLfihD5Ms4TX/XHQa/L9wu9vKOuWthtG+eEoZ6Ikwc05JpVo6gqkCnWX78f2ZXh7Fn2jV6KdjK/Ac17VtGcxfB07z2bM56kmXpigSTEX80yaLxJA/I6qq2ovXLTi2EQ0N0ZQx7VoisqUFQTQ80oRCtPNYnc5l6bHEQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=cBEuhX8H; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=ndhzBlJZ; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="cBEuhX8H"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ndhzBlJZ" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id E5EA41140239; Wed, 2 Oct 2024 06:55:55 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Wed, 02 Oct 2024 06:55:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866555; x=1727952955; bh=aht2XUEGSS gPFBhLVI+VpgVavEh2Ds/RKUhJijGak4U=; b=cBEuhX8H2hbFsLPAMoJT5dwfbJ /jLP3114mlVishbCXbxr2GGxwI5yYaTtIZmZ4fuKJeAqS5b1q1V1f7Ug3Nko1xHU ATImu3RvITuRCOHGJlC1fq0H35QN9epeG6781AbfPrtufJu2RJJszdSXQGe1ECk+ w3Ltb9ii6l/Cv+7RI2h+WIrtGi1ieetFmB4ZIT6q02QG/Mh1pfBei3zXSlt+JSt5 QvjqDdZX75KYb7XERw2p+PehSaoZGmHd2nfn6sMpFFJV/ITk4CqBcS9WWYPl9Pay ed2xS7G/bJnfxvLjmUbhJWWTF+UCGyYEBH5HiCY0ssMv8Mpy7G411FUPeflA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866555; x=1727952955; bh=aht2XUEGSSgPFBhLVI+VpgVavEh2 Ds/RKUhJijGak4U=; b=ndhzBlJZPqymV/IAOGl1L96Ii826twn6r8RKwHXTx/Br vTxvjtCttHteReu6nnmecxCvBx4wZfssknVmJisa9MBWGw5NCFaO/s4Seow3xgca 0/4//hTaOCaTN8sZ+uEdA/nJN8Q2Uk9cdzDWqCgQU6Iz94SwxjQ1D1uYDx1zBVg+ 1CMt6ObVHgb9qoBOX2JPsG8UBJXV8ky+ZmwZ5Wj5KlS57dnETMsqDBGDQIY6ij2t W4SapTP/Er2SdAbqCcn1US/gAVI96d7mpAyLKK7BGXZdL/aiTD+9dHyyPYok2NDW 6GQh74HggrKQ5xYCDW4qpUKfukjBhDqil1XMHsWdCg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepghhi thesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehgihhtshhtvghrsehpoh gsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:54 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 8c5e2610 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:04 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:48 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 09/25] reftable/writer: handle allocation failures in `reftable_new_writer()` Message-ID: <0ed99e0bdf19fa1e396312b79acf47e9ba8e6ae5.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reftable_new_writer()`. Adapt the function to return an error code to return such failures. While at it, rename it to match our code style as we have to touch up every callsite anyway. Signed-off-by: Patrick Steinhardt --- reftable/reftable-writer.h | 12 +++++++----- reftable/stack.c | 14 ++++++++++---- reftable/writer.c | 22 ++++++++++++++++------ t/unit-tests/lib-reftable.c | 8 +++++--- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h index 189b1f4144..43623dc7c3 100644 --- a/reftable/reftable-writer.h +++ b/reftable/reftable-writer.h @@ -90,11 +90,13 @@ struct reftable_stats { int object_id_len; }; -/* reftable_new_writer creates a new writer */ -struct reftable_writer * -reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t), - int (*flush_func)(void *), - void *writer_arg, const struct reftable_write_options *opts); +struct reftable_writer; + +/* Create a new writer. */ +int reftable_writer_new(struct reftable_writer **out, + ssize_t (*writer_func)(void *, const void *, size_t), + int (*flush_func)(void *), + void *writer_arg, const struct reftable_write_options *opts); /* Set the range of update indices for the records we will add. When writing a table into a stack, the min should be at least diff --git a/reftable/stack.c b/reftable/stack.c index 498fae846d..ea21ca6e5f 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -808,8 +808,11 @@ int reftable_addition_add(struct reftable_addition *add, } tab_fd = get_tempfile_fd(tab_file); - wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd, - &add->stack->opts); + err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush, + &tab_fd, &add->stack->opts); + if (err < 0) + goto done; + err = write_table(wr, arg); if (err < 0) goto done; @@ -898,8 +901,11 @@ static int stack_compact_locked(struct reftable_stack *st, goto done; } - wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, - &tab_fd, &st->opts); + err = reftable_writer_new(&wr, reftable_fd_write, reftable_fd_flush, + &tab_fd, &st->opts); + if (err < 0) + goto done; + err = stack_write_compact(st, wr, first, last, config); if (err < 0) goto done; diff --git a/reftable/writer.c b/reftable/writer.c index ed61aaf59c..8ab2e916d3 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -117,13 +117,17 @@ static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) w->block_writer->restart_interval = w->opts.restart_interval; } -struct reftable_writer * -reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t), - int (*flush_func)(void *), - void *writer_arg, const struct reftable_write_options *_opts) +int reftable_writer_new(struct reftable_writer **out, + ssize_t (*writer_func)(void *, const void *, size_t), + int (*flush_func)(void *), + void *writer_arg, const struct reftable_write_options *_opts) { - struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp)); struct reftable_write_options opts = {0}; + struct reftable_writer *wp; + + wp = reftable_calloc(1, sizeof(*wp)); + if (!wp) + return REFTABLE_OUT_OF_MEMORY_ERROR; if (_opts) opts = *_opts; @@ -134,13 +138,19 @@ reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t), strbuf_init(&wp->block_writer_data.last_key, 0); strbuf_init(&wp->last_key, 0); REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size); + if (!wp->block) { + reftable_free(wp); + return REFTABLE_OUT_OF_MEMORY_ERROR; + } wp->write = writer_func; wp->write_arg = writer_arg; wp->opts = opts; wp->flush = flush_func; writer_reinit_block_writer(wp, BLOCK_TYPE_REF); - return wp; + *out = wp; + + return 0; } void reftable_writer_set_limits(struct reftable_writer *w, uint64_t min, diff --git a/t/unit-tests/lib-reftable.c b/t/unit-tests/lib-reftable.c index ab1fa44a28..54c26c43e7 100644 --- a/t/unit-tests/lib-reftable.c +++ b/t/unit-tests/lib-reftable.c @@ -22,9 +22,11 @@ static int strbuf_writer_flush(void *arg UNUSED) struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf, struct reftable_write_options *opts) { - return reftable_new_writer(&strbuf_writer_write, - &strbuf_writer_flush, - buf, opts); + struct reftable_writer *writer; + int ret = reftable_writer_new(&writer, &strbuf_writer_write, &strbuf_writer_flush, + buf, opts); + check(!ret); + return writer; } void t_reftable_write_to_buf(struct strbuf *buf, From patchwork Wed Oct 2 10:55:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819618 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5842E1EC005 for ; Wed, 2 Oct 2024 10:55:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866561; cv=none; b=BkFN8/yTMsdoiUeKHWuNH5Zl9nhhrS+VRV5fcw7DFAwr+W2QJ0cpa7Yrs/+N/3NLPgKEHotJGQNw3WGrBHJXDEN/fkmRnV72n8gTpwhb6Z83OZq2SIwK51LEY/9Tm4k2kdD19KQjoiGo9ltWcUhlxs8YO7E5aBq2mIBtFP3M3sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866561; c=relaxed/simple; bh=fKA+oi/GNtlu33LZ/MBpy/eVh50EdP9jgCCFh2V41ZA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tIC4SwBjzhZPMLH0y1b9rJtw01YEvp1EWbKT93ztELI1Iw4t+wAZ1ZRzI9bHVIaSNohL/t+/MqMJqYldZuCLdb9H63EjHYoTgsrhYWj8JvHtuG1U5N4lCqettNdQrqW/v5lyo6wN+YHUqlPucWiHrN9goC3g8vpsbceHGsjMps0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=OjFJ+rqJ; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Krjl++LO; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="OjFJ+rqJ"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Krjl++LO" Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id 7158B1140223; Wed, 2 Oct 2024 06:55:58 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Wed, 02 Oct 2024 06:55:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866558; x=1727952958; bh=A9p2c8sCqj CF6g0PgewU0LcNOw2+zRrOB5265pra8nY=; b=OjFJ+rqJ+8sDfKKbtak8CVHzti 3zQT3+pU3oMnKtqxgmbPbM30LYXD3j4Jb7+8b95cmitr5rn20upd0dKv+J3f7igD 6LGUpjc/vQfQB2iTgGKiRuoZ8mL80R+3lV6xSaInqbwN2LdLjeIisz2pokkOSg72 yNcaWahSXrfKOVhHCSZarggVHW3dlNTN80v6P+dKFMUlFuLrqqB7hOi0RRhBtsCn 2vgnbZiJWJdxQD2ixPq6E8hMhaxZUk1eyA+zV0u3hvCRARMqOsdFFukPytxT689A R11nESUDVivY3dTbk/HkRj0S3HXcQjId56g22HofAn4xTUkoTgO+IeLK2OSg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866558; x=1727952958; bh=A9p2c8sCqjCF6g0PgewU0LcNOw2+ zRrOB5265pra8nY=; b=Krjl++LOAWEUWsuN6bpQHqc2ncgYe/xJBaYLMQuOlgbR mUFs5pgCNAOrI3DrUT4S1PxGGKdcIQgZuzlwQZbbuxPf4RY/8mPHJbDqHUmxZuOK gRSeqzaGOKT4U4pY7XTmfk7g/U9jwneB42RhOncx8DZTusIIjBP18ocN3FOhEhhE UHLbsET/CComuBtF3QpvuaI7eQ4VVERIx2IeJLICrD8eUVTvj0RTwRk8LLidAN+L 31ezzYfDD513eBivfb0r0WS7AAvLbAAYyNvGz90dihtPevNFLHrkE/x7A9u29ANc RAh2xw6l3Udyo6oQwVt6Rembm3UOTCrt7RbG0fmW8g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomhdprhgtphht thhopehgihhtsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhdrshdrrh esfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:57 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id db0f3608 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:06 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:54 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 10/25] reftable/merged: handle allocation failures in `merged_table_init_iter()` Message-ID: <8dfbfd9286c730fc63d40c6d095876fb1317cdcf.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `merged_table_init_iter()`. While at it, merge `merged_iter_init()` into the function. It only has a single caller and merging them makes it easier to handle allocation failures consistently. This change also requires us to adapt `reftable_stack_init_*_iterator()` to bubble up the new error codes of `merged_table_iter_init()`. Adapt callsites accordingly. Signed-off-by: Patrick Steinhardt --- refs/reftable-backend.c | 39 +++++++++++++---- reftable/merged.c | 74 ++++++++++++++++++++------------ reftable/merged.h | 6 +-- reftable/reftable-merged.h | 8 ++-- reftable/reftable-stack.h | 8 ++-- reftable/stack.c | 34 ++++++++++----- t/helper/test-reftable.c | 10 ++++- t/unit-tests/t-reftable-merged.c | 12 ++++-- t/unit-tests/t-reftable-stack.c | 4 +- 9 files changed, 131 insertions(+), 64 deletions(-) diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 5c241097a4..9c08c46c85 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -1299,7 +1299,9 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data struct reftable_log_record log = {0}; struct reftable_iterator it = {0}; - reftable_stack_init_log_iterator(arg->stack, &it); + ret = reftable_stack_init_log_iterator(arg->stack, &it); + if (ret < 0) + goto done; /* * When deleting refs we also delete all reflog entries @@ -1669,7 +1671,10 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data) * copy over all log entries from the old reflog. Last but not least, * when renaming we also have to delete all the old reflog entries. */ - reftable_stack_init_log_iterator(arg->stack, &it); + ret = reftable_stack_init_log_iterator(arg->stack, &it); + if (ret < 0) + goto done; + ret = reftable_iterator_seek_log(&it, arg->oldname); if (ret < 0) goto done; @@ -1890,7 +1895,10 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl if (ret < 0) goto done; - reftable_stack_init_log_iterator(stack, &iter->iter); + ret = reftable_stack_init_log_iterator(stack, &iter->iter); + if (ret < 0) + goto done; + ret = reftable_iterator_seek_log(&iter->iter, ""); if (ret < 0) goto done; @@ -1957,7 +1965,10 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store, if (refs->err < 0) return refs->err; - reftable_stack_init_log_iterator(stack, &it); + ret = reftable_stack_init_log_iterator(stack, &it); + if (ret < 0) + goto done; + ret = reftable_iterator_seek_log(&it, refname); while (!ret) { ret = reftable_iterator_next_log(&it, &log); @@ -1973,6 +1984,7 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store, break; } +done: reftable_log_record_release(&log); reftable_iterator_destroy(&it); return ret; @@ -1994,7 +2006,10 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store, if (refs->err < 0) return refs->err; - reftable_stack_init_log_iterator(stack, &it); + ret = reftable_stack_init_log_iterator(stack, &it); + if (ret < 0) + goto done; + ret = reftable_iterator_seek_log(&it, refname); while (!ret) { struct reftable_log_record log = {0}; @@ -2044,7 +2059,10 @@ static int reftable_be_reflog_exists(struct ref_store *ref_store, if (ret < 0) goto done; - reftable_stack_init_log_iterator(stack, &it); + ret = reftable_stack_init_log_iterator(stack, &it); + if (ret < 0) + goto done; + ret = reftable_iterator_seek_log(&it, refname); if (ret < 0) goto done; @@ -2150,7 +2168,9 @@ static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_da reftable_writer_set_limits(writer, ts, ts); - reftable_stack_init_log_iterator(arg->stack, &it); + ret = reftable_stack_init_log_iterator(arg->stack, &it); + if (ret < 0) + goto out; /* * In order to delete a table we need to delete all reflog entries one @@ -2174,6 +2194,7 @@ static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_da ret = reftable_writer_add_log(writer, &tombstone); } +out: reftable_log_record_release(&log); reftable_iterator_destroy(&it); return ret; @@ -2312,7 +2333,9 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, if (ret < 0) goto done; - reftable_stack_init_log_iterator(stack, &it); + ret = reftable_stack_init_log_iterator(stack, &it); + if (ret < 0) + goto done; ret = reftable_iterator_seek_log(&it, refname); if (ret < 0) diff --git a/reftable/merged.c b/reftable/merged.c index 128a810c55..de4f81abaf 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -30,22 +30,6 @@ struct merged_iter { ssize_t advance_index; }; -static void merged_iter_init(struct merged_iter *mi, - struct reftable_merged_table *mt, - uint8_t typ) -{ - memset(mi, 0, sizeof(*mi)); - mi->advance_index = -1; - mi->suppress_deletions = mt->suppress_deletions; - - REFTABLE_CALLOC_ARRAY(mi->subiters, mt->readers_len); - for (size_t i = 0; i < mt->readers_len; i++) { - reftable_record_init(&mi->subiters[i].rec, typ); - reader_init_iter(mt->readers[i], &mi->subiters[i].iter, typ); - } - mi->subiters_len = mt->readers_len; -} - static void merged_iter_close(void *p) { struct merged_iter *mi = p; @@ -244,25 +228,61 @@ reftable_merged_table_min_update_index(struct reftable_merged_table *mt) return mt->min; } -void merged_table_init_iter(struct reftable_merged_table *mt, - struct reftable_iterator *it, - uint8_t typ) +int merged_table_init_iter(struct reftable_merged_table *mt, + struct reftable_iterator *it, + uint8_t typ) { - struct merged_iter *mi = reftable_malloc(sizeof(*mi)); - merged_iter_init(mi, mt, typ); + struct merged_subiter *subiters; + struct merged_iter *mi = NULL; + int ret; + + REFTABLE_CALLOC_ARRAY(subiters, mt->readers_len); + if (!subiters) { + ret = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + + for (size_t i = 0; i < mt->readers_len; i++) { + reftable_record_init(&subiters[i].rec, typ); + reader_init_iter(mt->readers[i], &subiters[i].iter, typ); + } + + REFTABLE_CALLOC_ARRAY(mi, 1); + if (!mi) { + ret = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + mi->advance_index = -1; + mi->suppress_deletions = mt->suppress_deletions; + mi->subiters = subiters; + mi->subiters_len = mt->readers_len; + iterator_from_merged_iter(it, mi); + ret = 0; + +out: + if (ret < 0) { + for (size_t i = 0; subiters && i < mt->readers_len; i++) { + reftable_iterator_destroy(&subiters[i].iter); + reftable_record_release(&subiters[i].rec); + } + reftable_free(subiters); + reftable_free(mi); + } + + return ret; } -void reftable_merged_table_init_ref_iterator(struct reftable_merged_table *mt, - struct reftable_iterator *it) +int reftable_merged_table_init_ref_iterator(struct reftable_merged_table *mt, + struct reftable_iterator *it) { - merged_table_init_iter(mt, it, BLOCK_TYPE_REF); + return merged_table_init_iter(mt, it, BLOCK_TYPE_REF); } -void reftable_merged_table_init_log_iterator(struct reftable_merged_table *mt, - struct reftable_iterator *it) +int reftable_merged_table_init_log_iterator(struct reftable_merged_table *mt, + struct reftable_iterator *it) { - merged_table_init_iter(mt, it, BLOCK_TYPE_LOG); + return merged_table_init_iter(mt, it, BLOCK_TYPE_LOG); } uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt) diff --git a/reftable/merged.h b/reftable/merged.h index de5fd33f01..89bd0c4b35 100644 --- a/reftable/merged.h +++ b/reftable/merged.h @@ -26,8 +26,8 @@ struct reftable_merged_table { struct reftable_iterator; -void merged_table_init_iter(struct reftable_merged_table *mt, - struct reftable_iterator *it, - uint8_t typ); +int merged_table_init_iter(struct reftable_merged_table *mt, + struct reftable_iterator *it, + uint8_t typ); #endif diff --git a/reftable/reftable-merged.h b/reftable/reftable-merged.h index 16d19f8df2..a970d5dd89 100644 --- a/reftable/reftable-merged.h +++ b/reftable/reftable-merged.h @@ -37,12 +37,12 @@ int reftable_merged_table_new(struct reftable_merged_table **dest, uint32_t hash_id); /* Initialize a merged table iterator for reading refs. */ -void reftable_merged_table_init_ref_iterator(struct reftable_merged_table *mt, - struct reftable_iterator *it); +int reftable_merged_table_init_ref_iterator(struct reftable_merged_table *mt, + struct reftable_iterator *it); /* Initialize a merged table iterator for reading logs. */ -void reftable_merged_table_init_log_iterator(struct reftable_merged_table *mt, - struct reftable_iterator *it); +int reftable_merged_table_init_log_iterator(struct reftable_merged_table *mt, + struct reftable_iterator *it); /* returns the max update_index covered by this merged table. */ uint64_t diff --git a/reftable/reftable-stack.h b/reftable/reftable-stack.h index f4f8cabc7f..e958f911b4 100644 --- a/reftable/reftable-stack.h +++ b/reftable/reftable-stack.h @@ -73,16 +73,16 @@ struct reftable_iterator; * be used to iterate through refs. The iterator is valid until the next reload * or write. */ -void reftable_stack_init_ref_iterator(struct reftable_stack *st, - struct reftable_iterator *it); +int reftable_stack_init_ref_iterator(struct reftable_stack *st, + struct reftable_iterator *it); /* * Initialize an iterator for the merged tables contained in the stack that can * be used to iterate through logs. The iterator is valid until the next reload * or write. */ -void reftable_stack_init_log_iterator(struct reftable_stack *st, - struct reftable_iterator *it); +int reftable_stack_init_log_iterator(struct reftable_stack *st, + struct reftable_iterator *it); /* returns the merged_table for seeking. This table is valid until the * next write or reload, and should not be closed or deleted. diff --git a/reftable/stack.c b/reftable/stack.c index ea21ca6e5f..bb4d230918 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -136,18 +136,18 @@ int read_lines(const char *filename, char ***namesp) return err; } -void reftable_stack_init_ref_iterator(struct reftable_stack *st, +int reftable_stack_init_ref_iterator(struct reftable_stack *st, struct reftable_iterator *it) { - merged_table_init_iter(reftable_stack_merged_table(st), - it, BLOCK_TYPE_REF); + return merged_table_init_iter(reftable_stack_merged_table(st), + it, BLOCK_TYPE_REF); } -void reftable_stack_init_log_iterator(struct reftable_stack *st, - struct reftable_iterator *it) +int reftable_stack_init_log_iterator(struct reftable_stack *st, + struct reftable_iterator *it) { - merged_table_init_iter(reftable_stack_merged_table(st), - it, BLOCK_TYPE_LOG); + return merged_table_init_iter(reftable_stack_merged_table(st), + it, BLOCK_TYPE_LOG); } struct reftable_merged_table * @@ -952,7 +952,10 @@ static int stack_write_compact(struct reftable_stack *st, if (err < 0) goto done; - merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + if (err < 0) + goto done; + err = reftable_iterator_seek_ref(&it, ""); if (err < 0) goto done; @@ -977,7 +980,10 @@ static int stack_write_compact(struct reftable_stack *st, } reftable_iterator_destroy(&it); - merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + if (err < 0) + goto done; + err = reftable_iterator_seek_log(&it, ""); if (err < 0) goto done; @@ -1496,7 +1502,10 @@ int reftable_stack_read_ref(struct reftable_stack *st, const char *refname, struct reftable_iterator it = { 0 }; int ret; - reftable_merged_table_init_ref_iterator(st->merged, &it); + ret = reftable_merged_table_init_ref_iterator(st->merged, &it); + if (ret) + goto out; + ret = reftable_iterator_seek_ref(&it, refname); if (ret) goto out; @@ -1523,7 +1532,10 @@ int reftable_stack_read_log(struct reftable_stack *st, const char *refname, struct reftable_iterator it = {0}; int err; - reftable_stack_init_log_iterator(st, &it); + err = reftable_stack_init_log_iterator(st, &it); + if (err) + goto done; + err = reftable_iterator_seek_log(&it, refname); if (err) goto done; diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c index 29d4e9a755..5c8849d115 100644 --- a/t/helper/test-reftable.c +++ b/t/helper/test-reftable.c @@ -28,7 +28,10 @@ static int dump_table(struct reftable_merged_table *mt) const struct git_hash_algo *algop; int err; - reftable_merged_table_init_ref_iterator(mt, &it); + err = reftable_merged_table_init_ref_iterator(mt, &it); + if (err < 0) + return err; + err = reftable_iterator_seek_ref(&it, ""); if (err < 0) return err; @@ -63,7 +66,10 @@ static int dump_table(struct reftable_merged_table *mt) reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); - reftable_merged_table_init_log_iterator(mt, &it); + err = reftable_merged_table_init_log_iterator(mt, &it); + if (err < 0) + return err; + err = reftable_iterator_seek_log(&it, ""); if (err < 0) return err; diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c index 19e54bdfb8..3d2848632d 100644 --- a/t/unit-tests/t-reftable-merged.c +++ b/t/unit-tests/t-reftable-merged.c @@ -82,7 +82,8 @@ static void t_merged_single_record(void) struct reftable_iterator it = { 0 }; int err; - merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + check(!err); err = reftable_iterator_seek_ref(&it, "a"); check(!err); @@ -161,7 +162,8 @@ static void t_merged_refs(void) size_t cap = 0; size_t i; - merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + check(!err); err = reftable_iterator_seek_ref(&it, "a"); check(!err); check_int(reftable_merged_table_hash_id(mt), ==, GIT_SHA1_FORMAT_ID); @@ -367,7 +369,8 @@ static void t_merged_logs(void) size_t cap = 0; size_t i; - merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + check(!err); err = reftable_iterator_seek_log(&it, "a"); check(!err); check_int(reftable_merged_table_hash_id(mt), ==, GIT_SHA1_FORMAT_ID); @@ -390,7 +393,8 @@ static void t_merged_logs(void) check(reftable_log_record_equal(want[i], &out[i], GIT_SHA1_RAWSZ)); - merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + check(!err); err = reftable_iterator_seek_log_at(&it, "a", 2); check(!err); reftable_log_record_release(&out[0]); diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index 65e513d5ec..6e41a4cf7e 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -599,7 +599,9 @@ static void t_reftable_stack_iterator(void) reftable_iterator_destroy(&it); - reftable_stack_init_log_iterator(st, &it); + err = reftable_stack_init_log_iterator(st, &it); + check(!err); + reftable_iterator_seek_log(&it, logs[0].refname); for (i = 0; ; i++) { struct reftable_log_record log = { 0 }; From patchwork Wed Oct 2 10:55:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819619 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1E63200121 for ; Wed, 2 Oct 2024 10:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866563; cv=none; b=eno8GK0AkgoFKglXx9j1vphBERsNKy1gnrD1AUXA9tSVtJJmcVN26kpm1ws51KfYwN4jhvRgkW0hKlxayMtNSHUQHUjuNZ5VbwaAYvVmrbhOzQvvp0QrRQcGS1hdC8BUz5MQcRuzQIcwyp5pEWCFmhnE2LLWInsqk11LgFajLy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866563; c=relaxed/simple; bh=xofDXl8q4K22VJ53veiXYSPxdRfBrc039/G542444dM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sYtCVXosIO/ZhI5r/wXduQfriOLnKTR5OzvHMQfjuS5ZkQnL9abIUKXrPoneLRx5NFhoNHby1htOVaU+29OlIgqf7G54F99xDGJl1e5XcEJ6JOZmN3YzQM8gp0yph4hYre+yjJgGYaHY67nQp3XIb5voozPMoRmj5flb9B0M5uo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=UdGo7zh0; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=oDeOCuZM; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="UdGo7zh0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="oDeOCuZM" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id EFB381140237; Wed, 2 Oct 2024 06:56:00 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Wed, 02 Oct 2024 06:56:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866560; x=1727952960; bh=oDRrIWmC22 bLluO2AcwVBGHLbXpBHEkqKVOnaBep/VA=; b=UdGo7zh0ZeG/NSMGA0lP4PIr7b +bdKLcjy8095fZWJFVxntbNCVYrPnsX5JT9u3lM/EvqpYsXDbpT5rRCIhARL9qe1 gOhZKFkR3Y76aSP2kiH1ThaRTAisIqiB2qVipv6qcGca3ppUPjuaBaKre3T+mZW4 C/VxcbyH73Omf7/XgcagksYKokNgZtvLrPJDTqcaptt4O6XoVSQ8TWa6Grc9XU41 4sCnKpotiWm9hP6vMn/zXFWwiSeP9kgQCsWL3L7vsikx6/wtPhQCMEUeh+JaCxMi wf/9FimRMeYICEFuUZcEULdvjDY7eXFbbQt8wIl142hvk8g7NWeCtAto8OLQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866560; x=1727952960; bh=oDRrIWmC22bLluO2AcwVBGHLbXpB HEkqKVOnaBep/VA=; b=oDeOCuZM0aSILLp+Krg59Tq450+VC1voPNEZHZvgIxVY LyU+xqbeORwZl5I0Tph6S42YXUdfgS0kAnOqHATadnAo4mNqvKqhzWMyF0UqT95P M3NjoTMiQVOs9vvVHO+ijz1OXp1BvmV8AP/lSmv+WpbKIGrb1szoeQHdPb0p3oNw MipgBU1aWJ/HpsiBbqhAcM/P4H85YPBggqa1VAJUtnhLSVyjOGoNE+C2wgQmznuC lAOBtB5tylYjiqKu/9UKsaLsXzar/sXXeB7VMkX5pOTqSvI577jVx1i057T3DJ6f F1+0BqEHfjCueQC3MSrQso1iks0D8A//N4xYtnoSIA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgt phhtthhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopehgihhtshhtvghrsehpoh gsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:55:59 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 8c99478c (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:09 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:56 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 11/25] reftable/reader: handle allocation failures for unindexed reader Message-ID: <7b592a6c6b8b08a7e8089364abfc6d2d6f6e83e4.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures when creating unindexed readers. Signed-off-by: Patrick Steinhardt --- reftable/reader.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/reftable/reader.c b/reftable/reader.c index 6494ce2e32..485ee085da 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -734,19 +734,30 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r, REFTABLE_ALLOC_ARRAY(ti, 1); table_iter_init(ti, r); err = table_iter_seek_start(ti, BLOCK_TYPE_REF, 0); - if (err < 0) { - reftable_free(ti); - return err; - } + if (err < 0) + goto out; - filter = reftable_malloc(sizeof(struct filtering_ref_iterator)); + filter = reftable_malloc(sizeof(*filter)); + if (!filter) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } *filter = empty; strbuf_add(&filter->oid, oid, oid_len); iterator_from_table_iter(&filter->it, ti); iterator_from_filtering_ref_iterator(it, filter); - return 0; + + err = 0; + +out: + if (err < 0) { + if (ti) + table_iter_close(ti); + reftable_free(ti); + } + return err; } int reftable_reader_refs_for(struct reftable_reader *r, From patchwork Wed Oct 2 10:55:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819620 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3058720013A for ; Wed, 2 Oct 2024 10:56:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866565; cv=none; b=PSg+FcCAPn1JfTE8qhSMRVnppIt5CDQEHhVC6WX2mr/YVaekDKDWKSn49ag6bOGHFlHBh+jF9uqyARHRsvgc4rNVnJuOwt4fp2CJPRfD2Zxyt40hZ1CG0HFAaXJTZDa4jtH3sWr/SWZ+Tn25TjzHCtgXf2leGyLxKl+H/DD/7vM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866565; c=relaxed/simple; bh=TnMRuxLvOthM2Fk74dI32ReBzQNqW+VMyT2XyPacCdY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ppnm+zYhWvGB9xm/Kyz/7egi8qOEugiYeJgn5WZ5neJ62LwSgkVLMbSCigoRtTLlSLHsG6CNbDJmd2TQbz0tWt070+c+C8wFhvuEBSGDoSs2nZKgywUGMHNLdxpRKJAhgAvQ6Zc8JIEMHsdr0tcQVp++oStNmyuUIR1nFF0t8WY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=kaydlhaV; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=JGlxW8IM; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="kaydlhaV"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="JGlxW8IM" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id 6EAC01140223; Wed, 2 Oct 2024 06:56:03 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Wed, 02 Oct 2024 06:56:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866563; x=1727952963; bh=s4W/aPV3Ti GffFf15PzGX7aV+sCgI87f2r0PJ+GUM4M=; b=kaydlhaVNc11ZCKZUl9KH+Qkuf mjP7j3HAWJ8zee5bOgr/IFYAKitreU51a42UAFzmBsfERKxVDTvs4DtCXuvUmrMM ZydMHRpijUQQMGjKstb7NEH4IJ8GZgmD03NjzSO2M1NAtQ5E345QeKTg1996IBH/ yRJVbLzhAZCLQXCG337aKr9AG19wL8SDo9uDMkZON7kc1Qg0D+M0XatTgmG/dcEh LjJVqzvn0rVFaPOF3c5NFO/xp5mT8oiXgaXPY62f+Pbbol4G3Imm0sw97KkgfSHi 5FXfTXQljfhRzGpOequ/Slnu0JQa539UFFZiqDqQO6USxQDA/8B4isfIg9xA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866563; x=1727952963; bh=s4W/aPV3TiGffFf15PzGX7aV+sCg I87f2r0PJ+GUM4M=; b=JGlxW8IMrbWqZdIW8WzV5+o7lkhrDBXkug3VEs9gW2S9 7R63gw2rxZ6vfDesfg6oCN2TCMeNMiZM0AvPJiPEWxt2ZJonKKJ4pUvrk9ZIY3Yf SRjvkWbMmcZiUm+2TK5ivG5eZit9jDMB43twDDvcBHcaxfn45otypuXPMk4tk/MH N14+6aqA9c/DVO2mueorZ9LJG/UAx89bT0nZeGloJ7q/F7ZCKVFrpKW5et3I5+Zh +jQr8k5kNe3FrBR0VMmpyeuk14a0C6vs+Nd80xw3ayV9I2GMzo7EQJjgm0pLXft4 o1fCXAvTtN6orL3Gzw/qbZmADXgBaIByCZOeMA1eLg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgt phhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtoheplhdrshdrrh esfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:02 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 84e1befe (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:11 +0000 (UTC) Date: Wed, 2 Oct 2024 12:55:59 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 12/25] reftable/reader: handle allocation failures in `reader_init_iter()` Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reader_init_iter()`. This requires us to also adapt `reftable_reader_init_*_iterator()` to bubble up the new error codes. Adapt callers accordingly. Signed-off-by: Patrick Steinhardt --- reftable/merged.c | 4 +++- reftable/reader.c | 28 +++++++++++++++--------- reftable/reader.h | 6 ++--- reftable/reftable-reader.h | 8 +++---- t/unit-tests/t-reftable-readwrite.c | 34 +++++++++++++++++++---------- 5 files changed, 50 insertions(+), 30 deletions(-) diff --git a/reftable/merged.c b/reftable/merged.c index de4f81abaf..69790c345c 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -244,7 +244,9 @@ int merged_table_init_iter(struct reftable_merged_table *mt, for (size_t i = 0; i < mt->readers_len; i++) { reftable_record_init(&subiters[i].rec, typ); - reader_init_iter(mt->readers[i], &subiters[i].iter, typ); + ret = reader_init_iter(mt->readers[i], &subiters[i].iter, typ); + if (ret < 0) + goto out; } REFTABLE_CALLOC_ARRAY(mi, 1); diff --git a/reftable/reader.c b/reftable/reader.c index 485ee085da..f696e992df 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -554,32 +554,37 @@ static void iterator_from_table_iter(struct reftable_iterator *it, it->ops = &table_iter_vtable; } -void reader_init_iter(struct reftable_reader *r, - struct reftable_iterator *it, - uint8_t typ) +int reader_init_iter(struct reftable_reader *r, + struct reftable_iterator *it, + uint8_t typ) { struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); if (offs->is_present) { struct table_iter *ti; REFTABLE_ALLOC_ARRAY(ti, 1); + if (!ti) + return REFTABLE_OUT_OF_MEMORY_ERROR; + table_iter_init(ti, r); iterator_from_table_iter(it, ti); } else { iterator_set_empty(it); } + + return 0; } -void reftable_reader_init_ref_iterator(struct reftable_reader *r, - struct reftable_iterator *it) +int reftable_reader_init_ref_iterator(struct reftable_reader *r, + struct reftable_iterator *it) { - reader_init_iter(r, it, BLOCK_TYPE_REF); + return reader_init_iter(r, it, BLOCK_TYPE_REF); } -void reftable_reader_init_log_iterator(struct reftable_reader *r, - struct reftable_iterator *it) +int reftable_reader_init_log_iterator(struct reftable_reader *r, + struct reftable_iterator *it) { - reader_init_iter(r, it, BLOCK_TYPE_LOG); + return reader_init_iter(r, it, BLOCK_TYPE_LOG); } int reftable_reader_new(struct reftable_reader **out, @@ -689,7 +694,10 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, struct indexed_table_ref_iter *itr = NULL; /* Look through the reverse index. */ - reader_init_iter(r, &oit, BLOCK_TYPE_OBJ); + err = reader_init_iter(r, &oit, BLOCK_TYPE_OBJ); + if (err < 0) + goto done; + err = iterator_seek(&oit, &want); if (err != 0) goto done; diff --git a/reftable/reader.h b/reftable/reader.h index 3710ee09b4..02d10c5d37 100644 --- a/reftable/reader.h +++ b/reftable/reader.h @@ -56,9 +56,9 @@ struct reftable_reader { const char *reader_name(struct reftable_reader *r); -void reader_init_iter(struct reftable_reader *r, - struct reftable_iterator *it, - uint8_t typ); +int reader_init_iter(struct reftable_reader *r, + struct reftable_iterator *it, + uint8_t typ); /* initialize a block reader to read from `r` */ int reader_init_block_reader(struct reftable_reader *r, struct block_reader *br, diff --git a/reftable/reftable-reader.h b/reftable/reftable-reader.h index a600452b56..6a2d0b693f 100644 --- a/reftable/reftable-reader.h +++ b/reftable/reftable-reader.h @@ -46,12 +46,12 @@ void reftable_reader_incref(struct reftable_reader *reader); void reftable_reader_decref(struct reftable_reader *reader); /* Initialize a reftable iterator for reading refs. */ -void reftable_reader_init_ref_iterator(struct reftable_reader *r, - struct reftable_iterator *it); +int reftable_reader_init_ref_iterator(struct reftable_reader *r, + struct reftable_iterator *it); /* Initialize a reftable iterator for reading logs. */ -void reftable_reader_init_log_iterator(struct reftable_reader *r, - struct reftable_iterator *it); +int reftable_reader_init_log_iterator(struct reftable_reader *r, + struct reftable_iterator *it); /* returns the hash ID used in this table. */ uint32_t reftable_reader_hash_id(struct reftable_reader *r); diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index e1b235a5f1..acca927a2c 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -206,7 +206,8 @@ static void t_log_write_read(void) err = reftable_reader_new(&reader, &source, "file.log"); check(!err); - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, names[N - 1]); check(!err); @@ -221,8 +222,8 @@ static void t_log_write_read(void) reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); - reftable_reader_init_log_iterator(reader, &it); - + err = reftable_reader_init_log_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_log(&it, ""); check(!err); @@ -296,7 +297,8 @@ static void t_log_zlib_corruption(void) err = reftable_reader_new(&reader, &source, "file.log"); check(!err); - reftable_reader_init_log_iterator(reader, &it); + err = reftable_reader_init_log_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_log(&it, "refname"); check_int(err, ==, REFTABLE_ZLIB_ERROR); @@ -325,7 +327,8 @@ static void t_table_read_write_sequential(void) err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, ""); check(!err); @@ -376,7 +379,8 @@ static void t_table_read_api(void) err = reftable_reader_new(&reader, &source, "file.ref"); check(!err); - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, names[0]); check(!err); @@ -419,7 +423,8 @@ static void t_table_read_write_seek(int index, int hash_id) } for (i = 1; i < N; i++) { - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, names[i]); check(!err); err = reftable_iterator_next_ref(&it, &ref); @@ -435,7 +440,8 @@ static void t_table_read_write_seek(int index, int hash_id) strbuf_addstr(&pastLast, names[N - 1]); strbuf_addstr(&pastLast, "/"); - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, pastLast.buf); if (err == 0) { struct reftable_ref_record ref = { 0 }; @@ -534,7 +540,8 @@ static void t_table_refs_for(int indexed) if (!indexed) reader->obj_offsets.is_present = 0; - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, ""); check(!err); reftable_iterator_destroy(&it); @@ -593,7 +600,8 @@ static void t_write_empty_table(void) err = reftable_reader_new(&rd, &source, "filename"); check(!err); - reftable_reader_init_ref_iterator(rd, &it); + err = reftable_reader_init_ref_iterator(rd, &it); + check(!err); err = reftable_iterator_seek_ref(&it, ""); check(!err); @@ -802,7 +810,8 @@ static void t_write_multiple_indices(void) * Seeking the log uses the log index now. In case there is any * confusion regarding indices we would notice here. */ - reftable_reader_init_log_iterator(reader, &it); + err = reftable_reader_init_log_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_log(&it, ""); check(!err); @@ -858,7 +867,8 @@ static void t_write_multi_level_index(void) /* * Seeking the last ref should work as expected. */ - reftable_reader_init_ref_iterator(reader, &it); + err = reftable_reader_init_ref_iterator(reader, &it); + check(!err); err = reftable_iterator_seek_ref(&it, "refs/heads/199"); check(!err); From patchwork Wed Oct 2 10:56:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819621 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A182F1EC00B for ; Wed, 2 Oct 2024 10:56:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866568; cv=none; b=bDo9qlSulvsoZ1l9lYBsfuBCuDiH6TPRgyv+4xbswgW1t3b3fPsxyNTybAEVM+B3RjOigd9tYj8FWbAAyOwykrEvVLWFj96EfijaVmsDkkeYPkxZd8Cqjdm88V1S/ggiCbS/43wRdZ5ge8t4YWaGRuqxPS6MtgYYpMQSClBivv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866568; c=relaxed/simple; bh=H1Mm5KhJsOjzDHAUqGiZuYfA3ybGguq0Ienl5++K7jU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=QwQs0RCbnBivOZoWf3PTapp8VkQWdSwWG20Dd0qcKE26aW+SVFgnggnpUrSTCQEKOrlrsNlscUH3kfRfZsj5B9Z0TLyzGqv1lCHfwwlsOM7tjVESvTInW653eoreGTR1E+nKpPeALWIQUcHip2Ji+bMpR/Zm9oOy+bri2cuuf1s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=lOjTzHwQ; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=cO6avJuW; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="lOjTzHwQ"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="cO6avJuW" Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id DEF7E1140243; Wed, 2 Oct 2024 06:56:05 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Wed, 02 Oct 2024 06:56:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866565; x=1727952965; bh=CDOp7/ndkP DBDZK6gpRVe4iy7BT+NlOm7JPhmzrAe1I=; b=lOjTzHwQYmV84nS7Q2KbsTPkTP zUL/5uc6eorgAcwJZChmU68NCVOWVcvr8m7I1TRXx3k3xaukrTN4R9vH4izjUH3e bVvRfTc9hmuzIvwi5xSa80nygZwI+b9q0FN793jlkeUlJa95DxKnnEjBADZx5bON HC2+LW54x/NWTZB5LGOE8L6s7w51rRL/wvIBivFWa5GJkFI+n2Fbwn59LKw7LcEd 7I/U149h22j8X9rwbIOsHB+fIIHmNBCNFjJuwauZW9aclI9nmtqeYeTo5Ejp0h7Q 4eUnc4MYR749YjDK0gK8qL2ZnYM8RDEp21dyYzTcz3vVvgASARVeFH5Ursjg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866565; x=1727952965; bh=CDOp7/ndkPDBDZK6gpRVe4iy7BT+ NlOm7JPhmzrAe1I=; b=cO6avJuWUxca9IksU5QGqES4sY6taonZV831FI3yM2lY dmFxGEUsgcw3K+tqsjMY0dlR7wo0PuTEb4BctuO7Z5OI8LHQpdfQXDM7TCHdXgXH 96bcheCsMvAmLKVYi9JgLsKXNt0rY1S73WRUDuXgRaeXAqius+WDsLdLzLt3Brr4 j1lYQT9KM+n+NqTbupf9SSQDrtwmBmp0OCpki83ySQFLANn9wLUR7rnFmNAV2jtR rQUDXUW+lh38R6TU5UNdYpcT9tR3Brl0WhbCFT/vVdyVoipf29uRNfrh94zPog5U fvqYI6hujt1bBPxOtVuwcDTRVLFD9qAz9eLTIxfL/w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsoh hnrdgtohhmpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomhdprhgtphht thhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:04 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 6d7db1d9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:14 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:01 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 13/25] reftable/stack: handle allocation failures on reload Message-ID: <1e997a576693a9ae1841b14ef7f2a02841694bfb.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reftable_stack_reload_once()`. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index bb4d230918..060b2c1b90 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -214,13 +214,13 @@ void reftable_stack_destroy(struct reftable_stack *st) } static struct reftable_reader **stack_copy_readers(struct reftable_stack *st, - int cur_len) + size_t cur_len) { struct reftable_reader **cur = reftable_calloc(cur_len, sizeof(*cur)); - int i = 0; - for (i = 0; i < cur_len; i++) { + if (!cur) + return NULL; + for (size_t i = 0; i < cur_len; i++) cur[i] = st->readers[i]; - } return cur; } @@ -229,18 +229,30 @@ static int reftable_stack_reload_once(struct reftable_stack *st, int reuse_open) { size_t cur_len = !st->merged ? 0 : st->merged->readers_len; - struct reftable_reader **cur = stack_copy_readers(st, cur_len); + struct reftable_reader **cur; struct reftable_reader **reused = NULL; - size_t reused_len = 0, reused_alloc = 0; - size_t names_len = names_length(names); - struct reftable_reader **new_readers = - reftable_calloc(names_len, sizeof(*new_readers)); + struct reftable_reader **new_readers; + size_t reused_len = 0, reused_alloc = 0, names_len; size_t new_readers_len = 0; struct reftable_merged_table *new_merged = NULL; struct strbuf table_path = STRBUF_INIT; int err = 0; size_t i; + cur = stack_copy_readers(st, cur_len); + if (!cur) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + + names_len = names_length(names); + + new_readers = reftable_calloc(names_len, sizeof(*new_readers)); + if (!new_readers) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + while (*names) { struct reftable_reader *rd = NULL; const char *name = *names++; @@ -261,6 +273,10 @@ static int reftable_stack_reload_once(struct reftable_stack *st, * do by bumping their refcount. */ REFTABLE_ALLOC_GROW(reused, reused_len + 1, reused_alloc); + if (!reused) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } reused[reused_len++] = rd; reftable_reader_incref(rd); break; From patchwork Wed Oct 2 10:56:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819622 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4132020124D for ; Wed, 2 Oct 2024 10:56:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866571; cv=none; b=sFU58OkHYoj0DqiKQk0lpi3hccISHv79iTIKsMivCrYfQOPu7J+iNaCXR2qM0Tp4Hqsadhkok6e+aPVSF+o8Ua/MlfBxnzJSj/yxgBwBHssUzlxSFuKTzNyhuMMVrG8Tx7+UahH/O5WTjtXQUaXa6bEKfx5uzB8iqOdxxaWv4do= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866571; c=relaxed/simple; bh=rCPp3mmeI9gScA9HsXOPA2dfgstO2NnDkNHyDkLDeu4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=kmNHfzgIhzsZbcMRRFI30J/unhjJjjqdHWnwvVENtYwv0tEFTjOm8DgMz7vaoSktxT2YY539t8+ciZ/usV2phBsXttmm2dpes9ByX7dxZXZucF/+U62vVYybJVQMvKwmlVAV68YEL4vjLiwLhkp6H+fkaUlAsHobUNWzeuk8Xf4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=DUG0iObt; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=deXRL1/n; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="DUG0iObt"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="deXRL1/n" Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 6F3EE114023A; Wed, 2 Oct 2024 06:56:08 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Wed, 02 Oct 2024 06:56:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866568; x=1727952968; bh=wm7DXu2+fV PTxnBAEUkleoF1q3gPUSwhWEs6ZpuxEHA=; b=DUG0iObtmdDOar4DKkjt/RUumX TeCw8rSGCa4V4+oXg+OOXeI4pwfjfwVojLETR+XeClJVLCTZMjemecspiqVC8iyr oeQy7GfC8DVLWAqC3BPSUNXzGxv5LhXCLGrOjqJ1HnYb3uiTMnGorVV/hHSA1Z+X 5ijBhQ/XFv0O38mIjtGk1BC+/jjPIt9TxOPUT4BIIu4p3pnXQFg2mgRJTjLAvwaT I6oc9zqzKpJ/tHucMxaC1R1CSR98WULQ71vbaJTGGNFVOBADqJbRtBSx4r2k4c9r xofIeaUXd/OXr3gKWrWfpbgIhH7U2OCAnInsltmQTAz8PpEnvkD8aRJH/frg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866568; x=1727952968; bh=wm7DXu2+fVPTxnBAEUkleoF1q3gP USwhWEs6ZpuxEHA=; b=deXRL1/nIF3M//e+mxkG2pPeKTwLqP3t1ll4LtTV5nTa sYM63oAytSNbp2Esdy/Eu2y1Giv74lQ6lqGWzuxYh/DDO1CJJrsLhmex37TXnEEK Y8oY4RF+ZyVb7Gj388+LtTCXL5TGI6ff78VuqEAcHfMR+9GVjRlYMxqPD0v1cX44 9XoTGImnnXM7l4S2iLzpuDhPp50e4Qe7W2zBdIHirMDh5KduHpXKwkQx3Tg8CKxH yp3Uf8lA75cwseDei2DUPwBGnXflyWVFXs8Lb2lSg27tcz0qDUM512Ha1/z5UGDG u8Htfe9AtADEix0J+uhXKuZaW8l5zq4JTYoiFdA6Lg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgt phhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtoheplhdrshdrrh esfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:07 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 0c24042a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:17 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:04 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 14/25] reftable/stack: handle allocation failures in `reftable_new_stack()` Message-ID: <40d4d81378dbeffa0a916aca7d747ee9a045e045.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reftable_new_stack()`. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 060b2c1b90..1b77c9d014 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -56,10 +56,16 @@ static int reftable_fd_flush(void *arg) int reftable_new_stack(struct reftable_stack **dest, const char *dir, const struct reftable_write_options *_opts) { - struct reftable_stack *p = reftable_calloc(1, sizeof(*p)); struct strbuf list_file_name = STRBUF_INIT; - struct reftable_write_options opts = {0}; - int err = 0; + struct reftable_write_options opts = { 0 }; + struct reftable_stack *p; + int err; + + p = reftable_calloc(1, sizeof(*p)); + if (!p) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } if (_opts) opts = *_opts; @@ -74,15 +80,23 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir, p->list_file = strbuf_detach(&list_file_name, NULL); p->list_fd = -1; - p->reftable_dir = xstrdup(dir); p->opts = opts; + p->reftable_dir = reftable_strdup(dir); + if (!p->reftable_dir) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } err = reftable_stack_reload_maybe_reuse(p, 1); - if (err < 0) { + if (err < 0) + goto out; + + *dest = p; + err = 0; + +out: + if (err < 0) reftable_stack_destroy(p); - } else { - *dest = p; - } return err; } @@ -171,6 +185,10 @@ void reftable_stack_destroy(struct reftable_stack *st) { char **names = NULL; int err = 0; + + if (!st) + return; + if (st->merged) { reftable_merged_table_free(st->merged); st->merged = NULL; From patchwork Wed Oct 2 10:56:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819623 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D346A1E7676 for ; Wed, 2 Oct 2024 10:56:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866575; cv=none; b=rj5R2NTXcagFAdmTT+8NvdL8Ji3653jE6iVioOPpaGcHeSFzt5PakY2DbpcxU1rwp6pbz4PPOFa6T0U643p+n+sSDa5gx4c7lpF6WL5I80x8cOBQuxHwRaROpjgvluzQEh5iCRVlN6/XBkKRqws6KIpJYd7OrUsBeF2Bq7jKE4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866575; c=relaxed/simple; bh=vV0AsLiyRRy9aJacPjNjqGAPQUnFNugOVkPW5R/4AgM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=JHiGClaHIQ2uK4W8JgmQhQdydkp82guYfmwdsU3O2hY+BrQz7rJCXYZeZZPV+petBfjN/AzdBRjtlXK0EAtk1ADwDlt/6e1Tu/zZJ08UIyUKH+AinMUzeUTMfDwJ24oNJXM9klLNff3INxwXiLaa3Asw9KA1udHOSaKSqqhKnhU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=eZOGw5N2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=RwEw+hNi; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="eZOGw5N2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="RwEw+hNi" Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfout.phl.internal (Postfix) with ESMTP id E4BE313804BF; Wed, 2 Oct 2024 06:56:12 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-12.internal (MEProxy); Wed, 02 Oct 2024 06:56:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866572; x=1727952972; bh=xD9RQLoIJ4 TVbE8oPxgkWYjBzZfkkgReWNVwOJFNAVs=; b=eZOGw5N2RHXqxkIN/NkaK67IKo mtUzPcglp7xr5ppFb2be/m866t1rRLRQt5TKiJ0pHF6b32fmFuYpG/1P0we4b660 jrx1idD5sL6nhh8gT8okuEfuEj0/C3qAfnaVcyXW/KpropeEp9SD57wM8YAZn8Zl zbHemJJv2KwyKgfC6p0Tu4uzeg5Aib3wiaOkbweS+dGPMP+SsIP1mitoGlAMLmhH ZG+bjU/bRl41jV3+oY0HAwFbb/R/MWP9aDxwzAdLQUH//Gs6scJeEvi8wmEf7I2N 3u/vTULtppVgNjQfqxL8iWy05WdaY2ne2yzke3Qc9Vxeg0hbd8iRvnqnVjCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866572; x=1727952972; bh=xD9RQLoIJ4TVbE8oPxgkWYjBzZfk kgReWNVwOJFNAVs=; b=RwEw+hNiAYYerm8z7nE33yjWfFOTRYfSuACHwtGaohrL 4W1BAwsmSW7DlGifaRYSAGrAFNcZDRJ54LFVvlolVPFhZa7qNqqb46nnzXv7BGbi jB/EHPPrbNK9NXy24ov29v8nUGX8rp9y1U2IkUs6fFBxpBHQPFAOkl13KOH49nbQ 5ltdZFDJbY0GJvIkU592EuVJlLLg6KE+g7sX+tSKVJOs1eYZZPurMxkxRcp+e1IP OFYQRDmiax9cUzhIp0DI0A8BktflvsQOU1cE+b0H7fUYAra80dzmUQ9X+ttVnMhm T+UrI8LkT+RhwAoz3//hinY0skUD02djbDhCXCteyg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepvg hthhhomhhsohhnsegvugifrghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtohepghhi thhsthgvrhesphhosghogidrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:11 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id dabc0000 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:21 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:07 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 15/25] reftable/stack: handle allocation failures in `stack_compact_range()` Message-ID: <6aaae4baa6ec9bccf8f8e8a517dcebda995b65b2.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `stack_compact_range()`. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 1b77c9d014..2e6dd513d7 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -1131,6 +1131,11 @@ static int stack_compact_range(struct reftable_stack *st, * from the point of view of the newer process. */ REFTABLE_CALLOC_ARRAY(table_locks, last - first + 1); + if (!table_locks) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + for (i = last + 1; i > first; i--) { stack_filename(&table_name, st, reader_name(st->readers[i - 1])); @@ -1312,8 +1317,18 @@ static int stack_compact_range(struct reftable_stack *st, * thus have to allocate `readers_len + 1` many entries. */ REFTABLE_CALLOC_ARRAY(names, st->merged->readers_len + 1); - for (size_t i = 0; i < st->merged->readers_len; i++) - names[i] = xstrdup(st->readers[i]->name); + if (!names) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + + for (size_t i = 0; i < st->merged->readers_len; i++) { + names[i] = reftable_strdup(st->readers[i]->name); + if (!names[i]) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + } first_to_replace = first; last_to_replace = last; } From patchwork Wed Oct 2 10:56:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819624 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 491FE201257 for ; Wed, 2 Oct 2024 10:56:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866577; cv=none; b=eoH+19Wb5GJbklAHNAmNuXHsO8ZYHBDSOZTJ8j0COpR2TgwNLS0HgIP1Qj/i1hmUvvr3LGBDgatpceCwoLw1eJdphOzZKgG1fq5iJ+Avjv/GapjSum3ZYxKDdSSm29qZchw6NnDiEeraq+CXntxZMAl3QXY1vzqKKy41O+Oeep4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866577; c=relaxed/simple; bh=w+2eowW1AanmbkxWiJ4TTQpslKYmKUD6TDLC/4OpVVY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=aCsIKzAa1JsYf0CVU1DSIRdXFsFiIWdxYjWAcRAraBHfnoaECeXxd9hMj34ZZObuy+R4iA1qIdc4YURitPgDAmaP/GdypaxjcP/ndgj8BrK5H9HT1Vn+wx+0LD+yI4bGz2kGO/mPjxfs2BrLZTtIT/Gyu107VDx+3oW2g5CKswg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=ac58g5z2; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=K4tVBZgu; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="ac58g5z2"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="K4tVBZgu" Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id 6CF441380241; Wed, 2 Oct 2024 06:56:15 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 02 Oct 2024 06:56:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866575; x=1727952975; bh=ctMvMp+aBj 0Gl7Q5KgdMIXoAv9A2WUMeXeT3hFDa88s=; b=ac58g5z2ye2vuyWG4QbxXgu9/D QntEHvmatw/AQfF2Hfcoz++dmu5TZkz5sQ8wBv9nMt40PLVOpX+yzQ7dZ1grS92p vSYlFmSLu/rPcoJiOMQx+x1du+3qVI/mcROIzyurFlt1PjgD3wS9IrGedah2D9TQ TCcD2/V8sljT6enpGpp1iD25T/3sPfORSAdvO2zBbRSIMuDMyx+dFBgCWOQ1/scX gl5l1xf6qTadZ31+jdFc5OTYhAES6CV7GWlsIdeG/TLW9QfXGHtedyFyrrSo04e1 Sxs30qTjqtjM/s38Rrxgw56v3cmyiLiot8BICBrN0B4SJCg2v81CdI2xbQ0A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866575; x=1727952975; bh=ctMvMp+aBj0Gl7Q5KgdMIXoAv9A2 WUMeXeT3hFDa88s=; b=K4tVBZguy0Q2ttwGkP91l6tWutS6ExYDHykzn7HJx5ly LbkFgMOzqIHcFxCo5OQIMAmjZXvlAViNEl8heTbjh5JwOsQo48whscsPC/CqBjyH wivrQD7Au5c4oxSjV7isu7nJuZMcHlxzixYHrdQhKRFgSHekeQicUSdCzsiuhaSH xZL0mNkvGjRYevEy0zduckSeqfCM5NtkX0GkfgGVDal8Fkk0fYSrSX/Ri3JxjJud d/gFD8fjaYowgZIhIiTM18dQBIxQhsqq8X7KdpI5AWrR5U4amw3OjxkQgk7jc4uW rf3JMyZk9B6VVkDlWY4rW7yS5aDJr9InsXXeM0+CJQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopegvthhhohhmshhonhes vggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihhtshhtvghrsehpoh gsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:14 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 39101c89 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:24 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:11 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 16/25] reftable/stack: handle allocation failures in auto compaction Message-ID: <103a59ef0a5318b2ecef7ba65b7bb0c049b6dc6c.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `reftable_stack_auto_compact()`. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 2e6dd513d7..990784d9d2 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -1518,6 +1518,8 @@ static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st) uint64_t *sizes; REFTABLE_CALLOC_ARRAY(sizes, st->merged->readers_len); + if (!sizes) + return NULL; for (size_t i = 0; i < st->merged->readers_len; i++) sizes[i] = st->readers[i]->size - overhead; @@ -1527,11 +1529,17 @@ static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st) int reftable_stack_auto_compact(struct reftable_stack *st) { - uint64_t *sizes = stack_table_sizes_for_compaction(st); - struct segment seg = - suggest_compaction_segment(sizes, st->merged->readers_len, - st->opts.auto_compaction_factor); + struct segment seg; + uint64_t *sizes; + + sizes = stack_table_sizes_for_compaction(st); + if (!sizes) + return REFTABLE_OUT_OF_MEMORY_ERROR; + + seg = suggest_compaction_segment(sizes, st->merged->readers_len, + st->opts.auto_compaction_factor); reftable_free(sizes); + if (segment_size(&seg) > 0) return stack_compact_range(st, seg.start, seg.end - 1, NULL, STACK_COMPACT_RANGE_BEST_EFFORT); From patchwork Wed Oct 2 10:56:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819625 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAD0920126A for ; Wed, 2 Oct 2024 10:56:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866580; cv=none; b=CHikziB2uu7e2GSHFCw2zyQpKIdhMlhnAa3F+2d/BaSEvy7LnKbfqw6yMhU//HQLduFq9qWKmreWXGzo2IPchoGMfEKe1JVnEGpUE2W8/9SJMsKbDEHUN9InRTfCV+T/9kBw3m/yjedS5+cpjSgCd6Q9Gsbc+v8a0oigup/5weY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866580; c=relaxed/simple; bh=EKLM4HLY1J3UWglD2PFGKBssb9YjN15+GZauh+oJ4/o=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=cLNyPRxYEwc1e1QhW/H/2ARZ70mt5ef1CHlmGnRMsgIUl3LuEj7pB/pkwaSYyL83zJiSYXZysJfV2Cjbh0faeBjzSdijdgaaYBuT7XXXJeE0Dp4iAHMfPAQXYpra/ZdSi61BdlXQygd33iKShVrnizjMX4JCMLjzyZbvKIWLUcw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=kTOgMILs; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=VP0GP94R; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="kTOgMILs"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="VP0GP94R" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id F3091114020B; Wed, 2 Oct 2024 06:56:17 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Wed, 02 Oct 2024 06:56:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866577; x=1727952977; bh=NeH6GWRxfp VHTHVAv9F/b20tXGIwoNUozIP2jRdie38=; b=kTOgMILsPNbW/wkOB0mprhxysM zrn1emBihZiWXj7q6DFgLVfOhZB8Udmc5s8awQ8QW5GdigvD9MiAlHbSFsbFRcTC sySzqTEU6uWuui1K0HNZe7vDuD/uvEKJ6HD340B+QQ1j/1LwDPBu4zdQLN/+7UPB L9Lw01LUC8seKakJG8hv5g7bvKjZdVYhyhWbxF6DWVFm/N567vwTKbtr+sCRNXlC LaSyDQom8vpcIdh4t9OClho+dOG7R4hAQqS0J03Xifb8PfUeeGF5IDT06bZHxi0l Rspzz8brl4VMXyjYNJW+8TzhjtBeNQgeDeOQvd6I5PNAomEeEtWvdlN2LbPA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866577; x=1727952977; bh=NeH6GWRxfpVHTHVAv9F/b20tXGIw oNUozIP2jRdie38=; b=VP0GP94ROF85EkhX6pgDFQWCKOa0KCRwdiB/LPGjPekF Wh20+0+MP2vqsS8k9PT0iaEhGjqPeqrS60xWCr9gELSnhACP4PU+UZjXq0O3OLVV Ue/lXfPrW/SRxG0VgtCBjlIjua3pGe9cdMiEYBpE4OToZupt1dvmZEw2VnThmPFu xUrIsXvoYB6fYpSMthzeP3lfQcIGW1tcXjWGyElDzZRstW1XwL5I9Sy6dnXPnzQb AVgt20M4BffoSxjlbElsa+RxRxky+5DeKdXb8QI33vlACIe/GG/MXyjG5VEC0hlm z1XcGiec6zEtabBGJjzr33f7aFLr4LnZwJeyubKPXA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtoheplhdrshdrrhesfigvsgdruggvpdhrtghpthhtohepgh hithhsthgvrhesphhosghogidrtghomhdprhgtphhtthhopehgihhtsehvghgvrhdrkhgv rhhnvghlrdhorhhgpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomh hsohhnrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:16 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a88b320b (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:26 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:14 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 17/25] reftable/iter: handle allocation failures when creating indexed table iter Message-ID: <6799d299fe6dffcc6323f09dda6cd6cd51996056.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `new_indexed_table_ref_iter()`. While at it, rename the function to match our coding style. Signed-off-by: Patrick Steinhardt --- reftable/iter.c | 20 ++++++++++++++++---- reftable/iter.h | 2 +- reftable/reader.c | 7 ++++++- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/reftable/iter.c b/reftable/iter.c index 416a9f6996..d319538f80 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -181,14 +181,20 @@ static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec) } } -int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest, +int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest, struct reftable_reader *r, uint8_t *oid, int oid_len, uint64_t *offsets, int offset_len) { struct indexed_table_ref_iter empty = INDEXED_TABLE_REF_ITER_INIT; - struct indexed_table_ref_iter *itr = reftable_calloc(1, sizeof(*itr)); + struct indexed_table_ref_iter *itr; int err = 0; + itr = reftable_calloc(1, sizeof(*itr)); + if (!itr) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + *itr = empty; itr->r = r; strbuf_add(&itr->oid, oid, oid_len); @@ -197,10 +203,16 @@ int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest, itr->offset_len = offset_len; err = indexed_table_ref_iter_next_block(itr); + if (err < 0) + goto out; + + *dest = itr; + err = 0; + +out: if (err < 0) { + *dest = NULL; reftable_free(itr); - } else { - *dest = itr; } return err; } diff --git a/reftable/iter.h b/reftable/iter.h index befc4597df..b3225bc7ad 100644 --- a/reftable/iter.h +++ b/reftable/iter.h @@ -82,7 +82,7 @@ void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it, struct indexed_table_ref_iter *itr); /* Takes ownership of `offsets` */ -int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest, +int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest, struct reftable_reader *r, uint8_t *oid, int oid_len, uint64_t *offsets, int offset_len); diff --git a/reftable/reader.c b/reftable/reader.c index f696e992df..0179e4e73d 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -715,7 +715,7 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, goto done; } - err = new_indexed_table_ref_iter(&itr, r, oid, hash_size(r->hash_id), + err = indexed_table_ref_iter_new(&itr, r, oid, hash_size(r->hash_id), got.u.obj.offsets, got.u.obj.offset_len); if (err < 0) @@ -740,6 +740,11 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r, int err; REFTABLE_ALLOC_ARRAY(ti, 1); + if (!ti) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + table_iter_init(ti, r); err = table_iter_seek_start(ti, BLOCK_TYPE_REF, 0); if (err < 0) From patchwork Wed Oct 2 10:56:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819626 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28D64201277 for ; Wed, 2 Oct 2024 10:56:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866582; cv=none; b=Tp8DThnAipC3AWeCwY/KzKiCOgsN+EX5SJhR8qu0uM1bavhl/Z//TmowwyXcPh+D66v8fuiDHdoscLwIA+2cFT0hVX/nBI4/0yUlU8GVSiG5ySIZ0d2sh+J0mJtf7Ppe70tkYKcU7jE2QNpmjEyd2qvSt7gFY2TwPguq+QoE2k0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866582; c=relaxed/simple; bh=NEAklIhYm1WtSz3yi50MUdbLVYNWlJzAGlMkm7ihaUw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=mtMlP6QwRqBLayFEAHZKwztQPjRmT4wfDbSF2aZvVFi+ZaJIHSCRwpkbTHZDARozDmn7nwXG3RapJIjUaGyZwcoBpmqLltRuKQ677SSyi9a2OlUDjf3C5XM7ZfA8BiPqAkOhESfnd8CRoKODhDfoNMrTOkBx2FpkI/IFtG3jws8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=MmoSoPo0; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=phOxVfXt; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="MmoSoPo0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="phOxVfXt" Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id 6165C1140202; Wed, 2 Oct 2024 06:56:20 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Wed, 02 Oct 2024 06:56:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866580; x=1727952980; bh=63JfMVQ3u9 Ff0kIv55L90Gu0Ad+ZMvnwThAeQUUIFPE=; b=MmoSoPo0KPoRntZ759wp7I/rrT xzKvGxtVL198qiZ8WvNFqdEG8UPzFb1JpTtFIb7Ti4FHIi32x7T18SzXmnc9mduY sjfWZ5o4S8zUTdxz+9Q6l6vTNnp++4Yc3h5Z0+PAzhiyK9MlgrbPgel7tiIxootF j+0q49N3n831lxojQgvMPKlqiIdtkmd6SmhZd54OF8k8SPgv4gjAWFfSV5TG1UXj Nm6Jp0wLKjSaj75A63wJKK96+cW3W48yruLBkEI2rjq151Vk5xJwuvuSSvaqnGl/ PUJ02Dau+s7HURd2bKGFmteIUaYn6QwEnfEmnvhxGlf8UwzGnrEzXPFI9lcg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866580; x=1727952980; bh=63JfMVQ3u9Ff0kIv55L90Gu0Ad+Z MvnwThAeQUUIFPE=; b=phOxVfXtNhergNAD5+UOtbuWtd2FUroNBjuz8X5BFosb 7amU/f0yIaJj8AnWGUmhNOyPbIsTXBOcASQXw6/AtipLw2vVGA8MnTT6tw396/r5 nLXtkEHJMd5bxsySzV4M25E+MsoLpX8s+QyZ761Ey19ZGIzaGno9gfVTFndpoJtv Ic2X665APyCdZZjszlZCqHa1vgWuy8tOqLgbbV5TEP3FMo01RHH3fvda/qzMBhtU hHZkHV05sqD2HOa9LCISm7YcalTsFEdd3w8ngITJh2cfwwOwE4O2VnyjC5dryEpT wpCVP9T3HII8qSmPTAj3n9iZ6bY4/4Og+6lmVMShig== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopegvthhhohhmshhonhes vggufigrrhguthhhohhmshhonhdrtghomhdprhgtphhtthhopehgihhtshhtvghrsehpoh gsohigrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:19 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 4d8261f1 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:29 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:17 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 18/25] reftable/blocksource: handle allocation failures Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in the blocksource code. Signed-off-by: Patrick Steinhardt --- reftable/blocksource.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/reftable/blocksource.c b/reftable/blocksource.c index e93cac9bb6..a2a6a196d5 100644 --- a/reftable/blocksource.c +++ b/reftable/blocksource.c @@ -30,6 +30,8 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off, struct strbuf *b = v; assert(off + size <= b->len); REFTABLE_CALLOC_ARRAY(dest->data, size); + if (!dest->data) + return -1; memcpy(dest->data, b->buf + off, size); dest->len = size; return size; @@ -98,27 +100,40 @@ int reftable_block_source_from_file(struct reftable_block_source *bs, { struct file_block_source *p; struct stat st; - int fd; + int fd, err; fd = open(name, O_RDONLY); if (fd < 0) { if (errno == ENOENT) return REFTABLE_NOT_EXIST_ERROR; - return -1; + err = -1; + goto out; } if (fstat(fd, &st) < 0) { - close(fd); - return REFTABLE_IO_ERROR; + err = REFTABLE_IO_ERROR; + goto out; } REFTABLE_CALLOC_ARRAY(p, 1); + if (!p) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } + p->size = st.st_size; p->data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); - close(fd); assert(!bs->ops); bs->ops = &file_vtable; bs->arg = p; + + err = 0; + +out: + if (fd >= 0) + close(fd); + if (err < 0) + reftable_free(p); return 0; } From patchwork Wed Oct 2 10:56:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819627 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABF9220010A for ; Wed, 2 Oct 2024 10:56:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866586; cv=none; b=Yx6Lq7vfWCHf50saD2987tQwlSkhkWkdRbq+Nx6F9JSaYszjR1xk0YKIwvLRkRVCvDheLRyB6Wpc0105PMsO6REgw90FRMWSBBqnNIRFk2EpBHKREXyM/ypt7x+l+hpm+L4ARgU68LywCAveFH7APYP4sOxk4KQZs5fI8U1/Vfk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866586; c=relaxed/simple; bh=nxnFusU36jdefOtwH5N0TGO/cIzg8Jmv4BRaHZupmac=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Aoycfh69IkzSFwokCBjXGCkt4BpejbxLRBATkJwLNThVPj9UoG4jb9swH0l8KVtbBkqfHsuoaNnN+bTdDO6N7NJgy2ZXT3GcQTON0ldQg8sG8Nje/E9Ox5xpGlMJ098MoCmIFtv6fjNhz56JFdSrbYcfmuPAD2037t11ET77eHc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=RquX+nob; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=Xc5XKnQq; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="RquX+nob"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Xc5XKnQq" Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id D72BE11401FD; Wed, 2 Oct 2024 06:56:23 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Wed, 02 Oct 2024 06:56:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866583; x=1727952983; bh=6DQ2LC5BU5 rVy0ACAlOFLzRhYa+jqtSVnnTkLPRNixo=; b=RquX+nobuYI2MaGOzbHr5KSojY zhNJ3vmJ/xr9Go5Ib+AjTIkHNhV6qoABM3U/WmOU8NfTLdD1DhW2XO0wwR8qjc1Z 71nLDoRLAgomuApBkIcl0yDycWpSqHHQU9w3mTN1s3Y+758RexuVomK2RdmasNXM dJaO1ZbvTWk+qToFk4IMFnavt1yfiCAKZk2oPjSHwetUeu0wMDUpRZYpmHyJWAfI mKgegSh3NuztU3n/wIArzWPe5fWCgUR+bA55dXw1kGwYskUgqbf7ze0Aal0fBlAk PAp+7r+7C/rP3T2lBoTJaCPCFtqMypHS+dAszcNcC8H9fwwzFwciAdcZtG3Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866583; x=1727952983; bh=6DQ2LC5BU5rVy0ACAlOFLzRhYa+j qtSVnnTkLPRNixo=; b=Xc5XKnQqvSte6DD8ud+JXcMkR9u1ibv0nOytlt63VyLP 88BNMVT39Ytx2AcmKJOC5dGX0ixr7RB7jngg9yq4ULmcAl8Nyv6/KRvnzcrBKx3Y fBEGmuBTH5YU+Zp0PrGIdPEfTrcUwYiBxMQ5zGjTTxsuzAiKcsQI8QejzPru4uag L9IVbx5FFzamWx2JcuVksdKrYFr7U+gFaFY9YcaIB684fwGlHpxqsBTlmKBHpE8U qAKbZxiAJxwlMj1r2asIR5VYllbvr4gJTZSkCJNSNVrOnGlbcfUROvx5qSvuFlB0 Nmjd9klwfk5G8pI9COwNcf+GSE5SAn2gH5wCdmitqg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepvdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopehgihhtshhtvghrsehp ohgsohigrdgtohhmpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomh hsohhnrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:22 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a0ca2034 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:32 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:19 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 19/25] reftable/block: handle allocation failures Message-ID: <92d39b9021afc2dfcfa3d236c7c86286a7a4e101.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures in `block_writer_init()` and `block_reader_init()`. This requires us to bubble up error codes into `writer_reinit_block_writer()`. Adapt call sites accordingly. Signed-off-by: Patrick Steinhardt --- reftable/block.c | 23 +++++++++++++-- reftable/block.h | 4 +-- reftable/writer.c | 51 ++++++++++++++++++++++----------- t/unit-tests/t-reftable-block.c | 20 +++++++------ 4 files changed, 69 insertions(+), 29 deletions(-) diff --git a/reftable/block.c b/reftable/block.c index 00030eee06..bfa7dc61bf 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -52,6 +52,8 @@ static int block_writer_register_restart(struct block_writer *w, int n, return -1; if (is_restart) { REFTABLE_ALLOC_GROW(w->restarts, w->restart_len + 1, w->restart_cap); + if (!w->restarts) + return REFTABLE_OUT_OF_MEMORY_ERROR; w->restarts[w->restart_len++] = w->next; } @@ -63,8 +65,8 @@ static int block_writer_register_restart(struct block_writer *w, int n, return 0; } -void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, - uint32_t block_size, uint32_t header_off, int hash_size) +int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, + uint32_t block_size, uint32_t header_off, int hash_size) { bw->buf = buf; bw->hash_size = hash_size; @@ -78,8 +80,12 @@ void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, bw->last_key.len = 0; if (!bw->zstream) { REFTABLE_CALLOC_ARRAY(bw->zstream, 1); + if (!bw->zstream) + return REFTABLE_OUT_OF_MEMORY_ERROR; deflateInit(bw->zstream, 9); } + + return 0; } uint8_t block_writer_type(struct block_writer *bw) @@ -163,6 +169,10 @@ int block_writer_finish(struct block_writer *w) */ compressed_len = deflateBound(w->zstream, src_len); REFTABLE_ALLOC_GROW(w->compressed, compressed_len, w->compressed_cap); + if (!w->compressed) { + ret = REFTABLE_OUT_OF_MEMORY_ERROR; + return ret; + } w->zstream->next_out = w->compressed; w->zstream->avail_out = compressed_len; @@ -219,12 +229,21 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block, /* Log blocks specify the *uncompressed* size in their header. */ REFTABLE_ALLOC_GROW(br->uncompressed_data, sz, br->uncompressed_cap); + if (!br->uncompressed_data) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } /* Copy over the block header verbatim. It's not compressed. */ memcpy(br->uncompressed_data, block->data, block_header_skip); if (!br->zstream) { REFTABLE_CALLOC_ARRAY(br->zstream, 1); + if (!br->zstream) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + err = inflateInit(br->zstream); } else { err = inflateReset(br->zstream); diff --git a/reftable/block.h b/reftable/block.h index 1c8f25ee6e..18d7ea0337 100644 --- a/reftable/block.h +++ b/reftable/block.h @@ -45,8 +45,8 @@ struct block_writer { /* * initializes the blockwriter to write `typ` entries, using `buf` as temporary * storage. `buf` is not owned by the block_writer. */ -void block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, - uint32_t block_size, uint32_t header_off, int hash_size); +int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *buf, + uint32_t block_size, uint32_t header_off, int hash_size); /* returns the block type (eg. 'r' for ref records. */ uint8_t block_writer_type(struct block_writer *bw); diff --git a/reftable/writer.c b/reftable/writer.c index 8ab2e916d3..791e246337 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -102,19 +102,24 @@ static int writer_write_header(struct reftable_writer *w, uint8_t *dest) return header_size(writer_version(w)); } -static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) +static int writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) { - int block_start = 0; - if (w->next == 0) { + int block_start = 0, ret; + + if (w->next == 0) block_start = header_size(writer_version(w)); - } strbuf_reset(&w->last_key); - block_writer_init(&w->block_writer_data, typ, w->block, - w->opts.block_size, block_start, - hash_size(w->opts.hash_id)); + ret = block_writer_init(&w->block_writer_data, typ, w->block, + w->opts.block_size, block_start, + hash_size(w->opts.hash_id)); + if (ret < 0) + return ret; + w->block_writer = &w->block_writer_data; w->block_writer->restart_interval = w->opts.restart_interval; + + return 0; } int reftable_writer_new(struct reftable_writer **out, @@ -247,8 +252,11 @@ static int writer_add_record(struct reftable_writer *w, strbuf_reset(&w->last_key); strbuf_addbuf(&w->last_key, &key); - if (!w->block_writer) - writer_reinit_block_writer(w, reftable_record_type(rec)); + if (!w->block_writer) { + err = writer_reinit_block_writer(w, reftable_record_type(rec)); + if (err < 0) + goto done; + } if (block_writer_type(w->block_writer) != reftable_record_type(rec)) BUG("record of type %d added to writer of type %d", @@ -271,7 +279,9 @@ static int writer_add_record(struct reftable_writer *w, err = writer_flush_block(w); if (err < 0) goto done; - writer_reinit_block_writer(w, reftable_record_type(rec)); + err = writer_reinit_block_writer(w, reftable_record_type(rec)); + if (err < 0) + goto done; /* * Try to add the record to the writer again. If this still fails then @@ -461,7 +471,9 @@ static int writer_finish_section(struct reftable_writer *w) max_level++; index_start = w->next; - writer_reinit_block_writer(w, BLOCK_TYPE_INDEX); + err = writer_reinit_block_writer(w, BLOCK_TYPE_INDEX); + if (err < 0) + return err; idx = w->index; idx_len = w->index_len; @@ -555,7 +567,10 @@ static void write_object_record(void *void_arg, void *key) if (arg->err < 0) goto done; - writer_reinit_block_writer(arg->w, BLOCK_TYPE_OBJ); + arg->err = writer_reinit_block_writer(arg->w, BLOCK_TYPE_OBJ); + if (arg->err < 0) + goto done; + arg->err = block_writer_add(arg->w->block_writer, &rec); if (arg->err == 0) goto done; @@ -584,16 +599,18 @@ static int writer_dump_object_index(struct reftable_writer *w) struct common_prefix_arg common = { .max = 1, /* obj_id_len should be >= 2. */ }; - if (w->obj_index_tree) { + int err; + + if (w->obj_index_tree) infix_walk(w->obj_index_tree, &update_common, &common); - } w->stats.object_id_len = common.max + 1; - writer_reinit_block_writer(w, BLOCK_TYPE_OBJ); + err = writer_reinit_block_writer(w, BLOCK_TYPE_OBJ); + if (err < 0) + return err; - if (w->obj_index_tree) { + if (w->obj_index_tree) infix_walk(w->obj_index_tree, &write_object_record, &closure); - } if (closure.err < 0) return closure.err; diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index f1a49485e2..e52a612e85 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -34,8 +34,9 @@ static void t_ref_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); block.len = block_size; block_source_from_strbuf(&block.source ,&buf); - block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, - header_off, hash_size(GIT_SHA1_FORMAT_ID)); + ret = block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, + header_off, hash_size(GIT_SHA1_FORMAT_ID)); + check(!ret); rec.u.ref.refname = (char *) ""; rec.u.ref.value_type = REFTABLE_REF_DELETION; @@ -126,8 +127,9 @@ static void t_log_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); block.len = block_size; block_source_from_strbuf(&block.source ,&buf); - block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size, - header_off, hash_size(GIT_SHA1_FORMAT_ID)); + ret = block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size, + header_off, hash_size(GIT_SHA1_FORMAT_ID)); + check(!ret); for (i = 0; i < N; i++) { rec.u.log.refname = xstrfmt("branch%02"PRIuMAX , (uintmax_t)i); @@ -214,8 +216,9 @@ static void t_obj_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); block.len = block_size; block_source_from_strbuf(&block.source, &buf); - block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size, - header_off, hash_size(GIT_SHA1_FORMAT_ID)); + ret = block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size, + header_off, hash_size(GIT_SHA1_FORMAT_ID)); + check(!ret); for (i = 0; i < N; i++) { uint8_t bytes[] = { i, i + 1, i + 2, i + 3, i + 5 }, *allocated; @@ -296,8 +299,9 @@ static void t_index_block_read_write(void) REFTABLE_CALLOC_ARRAY(block.data, block_size); block.len = block_size; block_source_from_strbuf(&block.source, &buf); - block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size, - header_off, hash_size(GIT_SHA1_FORMAT_ID)); + ret = block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size, + header_off, hash_size(GIT_SHA1_FORMAT_ID)); + check(!ret); for (i = 0; i < N; i++) { strbuf_init(&recs[i].u.idx.last_key, 9); From patchwork Wed Oct 2 10:56:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819628 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69D2C205E10 for ; Wed, 2 Oct 2024 10:56:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866588; cv=none; b=YDg0whZiGfipiFIk9jLrhbuKsyjltPKsQGGN/0O4BqKWq6UuV9P4jD81IaPApXZnhe3HBz/yfoV+thRCzrsqi+7rmQIqvZphUaO2eYPufpQLDaQZ2aHf/iw0HYFSlUZSg2oglWOLiR17jvQKL9PaCXmvAH6YQbvdrphGyXfMocs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866588; c=relaxed/simple; bh=ALtrWnXStKntMaN9I3VU7mDu/QueCoZ80iWZMqKMWPs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=X90OoaCh/78VzhMU3vQKJTjpJzo5fc3cPVMQzrvbgYd9N8e25VjzwGiqzvyRUhOaD03FSmvO7R2A6T9TerFXTOOAx/QdtZXZDeIXQpDQd92xEwmZKPzbAyIki/g2MSgRhPiNELn1ol5p5bofLR9as9tJAj3V3UEZMzie7TeK0fE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=hD6b9lzR; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=JiNu973F; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="hD6b9lzR"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="JiNu973F" Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id 90E5B11400F9; Wed, 2 Oct 2024 06:56:26 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 02 Oct 2024 06:56:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866586; x=1727952986; bh=0LCa2vW0ow hrUoXxBmwAC7H/BVRaPf+WMa+mdj9hfjA=; b=hD6b9lzRwholC0RbpkBGnyOEXf c4ouRM7Haas4HliLiW8YfzeT63ua28MVIO/Qw339oRAZN6LfoyxfQCvubKRSyXyD XVGNHqSiUyP0TzqE+7R2g/eshAjkTTWKBtFIPllQWE8UzOLXok5n8OWJn3legeLj gW1s3U/uIaORePB+w08OIRWhtGwuEqWQ77yDhvcdfcdT8Ugtoq7XPegJ/vHLXfNc L+7a+/wnI4NAoblAvF+dO1EJ/pQjzy4FENzn+NtD9Y/37b/myDJrs29JTFvdS/hN 6JdXxlR4tOvk8cuGJyFuPOj7TDGZObzlN2w416zxmz9gyQhCZUYwFX6zqh9A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866586; x=1727952986; bh=0LCa2vW0owhrUoXxBmwAC7H/BVRa Pf+WMa+mdj9hfjA=; b=JiNu973FSBEE1EVmCaqn6GMaoBkk9Uz5xrCglCsddJ6F HcZVXOowUQnIGLGBIhL2abo2gALUFTfS4/bOZ4noRV2JUatbkKzgCJctPCoCVKg0 Aj307vTV7PS7Y05B7PIuVzC0IGFq40pJLJz37HPhREV4dgE8ChFVusYIeQ0QfN4T 6JMetTIsPmW+iRubtwRO+qxXjhfUJAsAWkUAo8aX2bgESPpu1AKC++qEyIV+IeQq hqqpNJbDvz2Gczl3k2Xb+7WBdNs7SNpu7xK7FckuJra/ChWIPtyD4CMhhCogTmBN 0OJixr4HSkCFYgb8fdtpR3FaIkc35NES5MCrPoPYbw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithhsthgvrhesphhosghogidrtghomhdprhgtph htthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgtphht thhopehlrdhsrdhrseifvggsrdguvgdprhgtphhtthhopehgihhtsehvghgvrhdrkhgvrh hnvghlrdhorhhg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:25 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 01d7eeae (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:35 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:22 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 20/25] reftable/pq: handle allocation failures when adding entries Message-ID: <3416004e0d73ec46be616ca2809bb40d4d10c44a.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle allocation failures when adding entries to the pqueue. Adapt its only caller accordingly. Signed-off-by: Patrick Steinhardt --- reftable/merged.c | 5 ++++- reftable/pq.c | 7 ++++++- reftable/pq.h | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/reftable/merged.c b/reftable/merged.c index 69790c345c..8e202a8efd 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -54,7 +54,10 @@ static int merged_iter_advance_subiter(struct merged_iter *mi, size_t idx) if (err) return err; - merged_iter_pqueue_add(&mi->pq, &e); + err = merged_iter_pqueue_add(&mi->pq, &e); + if (err) + return err; + return 0; } diff --git a/reftable/pq.c b/reftable/pq.c index 2b5b7d1c0e..03b9912282 100644 --- a/reftable/pq.c +++ b/reftable/pq.c @@ -8,6 +8,7 @@ license that can be found in the LICENSE file or at #include "pq.h" +#include "reftable-error.h" #include "reftable-record.h" #include "system.h" #include "basics.h" @@ -44,11 +45,13 @@ struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq) return e; } -void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e) +int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e) { size_t i = 0; REFTABLE_ALLOC_GROW(pq->heap, pq->len + 1, pq->cap); + if (!pq->heap) + return REFTABLE_OUT_OF_MEMORY_ERROR; pq->heap[pq->len++] = *e; i = pq->len - 1; @@ -59,6 +62,8 @@ void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry SWAP(pq->heap[j], pq->heap[i]); i = j; } + + return 0; } void merged_iter_pqueue_release(struct merged_iter_pqueue *pq) diff --git a/reftable/pq.h b/reftable/pq.h index 707bd26767..83c062eeca 100644 --- a/reftable/pq.h +++ b/reftable/pq.h @@ -23,7 +23,7 @@ struct merged_iter_pqueue { }; struct pq_entry merged_iter_pqueue_remove(struct merged_iter_pqueue *pq); -void merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e); +int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry *e); void merged_iter_pqueue_release(struct merged_iter_pqueue *pq); int pq_less(struct pq_entry *a, struct pq_entry *b); From patchwork Wed Oct 2 10:56:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819629 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFDBC1EBFE8 for ; Wed, 2 Oct 2024 10:56:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866594; cv=none; b=re2LmCSo0J4Z4xdeat/+7u9EAsBOiwbay8Pwpy6vyGFfdQY5VE6LAwb4/049ZhfpE/GgzY1C+g4p8iT3pjEql2BADs/329eE9hdwhEiTLv2JW9eXOsDNPKj+4J5re8LPQw/w4S0x5KdIZf7ma4+u/Oevo0QCaWnNH7/x3GWSdBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866594; c=relaxed/simple; bh=C5jR1IlCIKDK8WiUjz6HP24TIbtXWl+mhD3M36uUlsg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MlhMbnWM9E+DBgO0w5GQnBNUsL51QsvLdsQq+KX7zsjMwUD2JIlTwR8RwacmWDZHAx/mvvm1cYa1KdE2uGKtNIgCuOPSaTl6JxUew1rFK+QHEReRuu4okT+3QOhxflb8OBBmlGKq7njHP2yVVlkz+lVF9riGCz8brF8Yk+wbKPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=omdVsEKz; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=aQkF17uJ; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="omdVsEKz"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="aQkF17uJ" Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.phl.internal (Postfix) with ESMTP id F14C913802BC; Wed, 2 Oct 2024 06:56:31 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Wed, 02 Oct 2024 06:56:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866591; x=1727952991; bh=srMVGjg1LJ KX0ylPrgUBx2pqwuQ+br5EBxg31iRJj9Q=; b=omdVsEKz88TAx9oinqLGMOrj67 1rVx3tjIfWhPOh3QvyYFEgGnOqjPuvwGGxcutVu6Ld9x5PLeF2P1nfwGjnDePIn0 KuOeFmaTdC1TMRRoRssiDcqV6wLRB0gAPQnPcqD6kHJh3NOQeBfpjjidY45DqO0H 6VUIkNXlXFaV7YtjScrnTOwa4kkgfdbdggMt6qOuNtqbsWvMFSOh0Qq9owoeAfZ3 HNjV3+jHzzlF/Y5QWUhcPEnhS4nhj1YeKzsp0/AQ1AggEUMo6i5eAVOLxQDjRKYg hoYkQ08lrwR/tWWzMrolvg8ozIQmjawR3cYAnQx3I+cCEw19PBcHVkUGJkhA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866591; x=1727952991; bh=srMVGjg1LJKX0ylPrgUBx2pqwuQ+ br5EBxg31iRJj9Q=; b=aQkF17uJZfP1QpQB8t14wyxa26lNXCuENqzyzepQVF9+ 4DqflMZx71Krd5J6MkDJTf4OdqrWjA9MzhGyXl082Ceqa7fJnOxtiUr4FBKpFuwJ kC+w/PM/GfTHVqRxG/DBe31mxZ44FGHUI47/gXJwxtfJJqjpUYaT87Eh/ekSLxik k9iPqDS/exkQv/cntvHHTqfBxrmHvjQ4CHqnSqxs2Y/mVU2uO+zkuKceGQjkJSaU vUwBy2Y/MPZQZH3XUjJzMaeK0W1NovFt+I4DefZbQZE2ijaCmM5cVdAc2TN21K3g b36BiKulyn7makla2rJVp3N0jG7Hek/m4fOM5jcpvA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepfeenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtohepvghthhho mhhsohhnsegvugifrghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtoheplhdrshdrrh esfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:30 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id ba2d3c0a (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:41 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:25 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 21/25] reftable/tree: handle allocation failures Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The tree interfaces of the reftable library handle both insertion and searching of tree nodes with a single function, where the behaviour is altered between the two via an `insert` bit. This makes it quit awkward to handle allocation failures because on inserting we'd have to check for `NULL` pointers and return an error, whereas on searching entries we don't have to handle it as an allocation error. Split up concerns of this function into two separate functions, one for inserting entries and one for searching entries. This makes it easy for us to check for allocation errors as `tree_insert()` should never return a `NULL` pointer now. Adapt callers accordingly. Signed-off-by: Patrick Steinhardt --- reftable/tree.c | 42 +++++++++++++++++++++++----------- reftable/tree.h | 21 +++++++++++++---- reftable/writer.c | 7 +++--- t/unit-tests/t-reftable-tree.c | 10 ++++---- 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/reftable/tree.c b/reftable/tree.c index 5ffb2e0d69..f4dbe72090 100644 --- a/reftable/tree.c +++ b/reftable/tree.c @@ -11,28 +11,44 @@ license that can be found in the LICENSE file or at #include "basics.h" -struct tree_node *tree_search(void *key, struct tree_node **rootp, - int (*compare)(const void *, const void *), - int insert) +struct tree_node *tree_search(struct tree_node *tree, + void *key, + int (*compare)(const void *, const void *)) { int res; + if (!tree) + return NULL; + res = compare(key, tree->key); + if (res < 0) + return tree_search(tree->left, key, compare); + else if (res > 0) + return tree_search(tree->right, key, compare); + return tree; +} + +struct tree_node *tree_insert(struct tree_node **rootp, + void *key, + int (*compare)(const void *, const void *)) +{ + int res; + if (!*rootp) { - if (!insert) { + struct tree_node *n; + + REFTABLE_CALLOC_ARRAY(n, 1); + if (!n) return NULL; - } else { - struct tree_node *n; - REFTABLE_CALLOC_ARRAY(n, 1); - n->key = key; - *rootp = n; - return *rootp; - } + + n->key = key; + *rootp = n; + return *rootp; } res = compare(key, (*rootp)->key); if (res < 0) - return tree_search(key, &(*rootp)->left, compare, insert); + return tree_insert(&(*rootp)->left, key, compare); else if (res > 0) - return tree_search(key, &(*rootp)->right, compare, insert); + return tree_insert(&(*rootp)->right, key, compare); return *rootp; } diff --git a/reftable/tree.h b/reftable/tree.h index fbdd002e23..9604453b6d 100644 --- a/reftable/tree.h +++ b/reftable/tree.h @@ -15,12 +15,23 @@ struct tree_node { struct tree_node *left, *right; }; -/* looks for `key` in `rootp` using `compare` as comparison function. If insert - * is set, insert the key if it's not found. Else, return NULL. +/* + * Search the tree for the node matching the given key using `compare` as + * comparison function. Returns the node whose key matches or `NULL` in case + * the key does not exist in the tree. + */ +struct tree_node *tree_search(struct tree_node *tree, + void *key, + int (*compare)(const void *, const void *)); + +/* + * Insert a node into the tree. Returns the newly inserted node if the key does + * not yet exist. Otherwise it returns the preexisting node. Returns `NULL` + * when allocating the new node fails. */ -struct tree_node *tree_search(void *key, struct tree_node **rootp, - int (*compare)(const void *, const void *), - int insert); +struct tree_node *tree_insert(struct tree_node **rootp, + void *key, + int (*compare)(const void *, const void *)); /* performs an infix walk of the tree. */ void infix_walk(struct tree_node *t, void (*action)(void *arg, void *key), diff --git a/reftable/writer.c b/reftable/writer.c index 791e246337..e180c10840 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -208,8 +208,7 @@ static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) struct obj_index_tree_node *key; struct tree_node *node; - node = tree_search(&want, &w->obj_index_tree, - &obj_index_tree_node_compare, 0); + node = tree_search(w->obj_index_tree, &want, &obj_index_tree_node_compare); if (!node) { struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT; @@ -221,8 +220,8 @@ static int writer_index_hash(struct reftable_writer *w, struct strbuf *hash) strbuf_reset(&key->hash); strbuf_addbuf(&key->hash, hash); - tree_search((void *)key, &w->obj_index_tree, - &obj_index_tree_node_compare, 1); + tree_insert(&w->obj_index_tree, key, + &obj_index_tree_node_compare); } else { key = node->key; } diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/t-reftable-tree.c index 700479d34b..79b175a45a 100644 --- a/t/unit-tests/t-reftable-tree.c +++ b/t/unit-tests/t-reftable-tree.c @@ -37,16 +37,17 @@ static void t_tree_search(void) * values[1] and values[10] (inclusive) in the tree. */ do { - nodes[i] = tree_search(&values[i], &root, &t_compare, 1); + nodes[i] = tree_insert(&root, &values[i], &t_compare); + check(nodes[i] != NULL); i = (i * 7) % 11; } while (i != 1); for (i = 1; i < ARRAY_SIZE(nodes); i++) { check_pointer_eq(&values[i], nodes[i]->key); - check_pointer_eq(nodes[i], tree_search(&values[i], &root, &t_compare, 0)); + check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare)); } - check(!tree_search(values, &root, t_compare, 0)); + check(!tree_search(root, values, t_compare)); tree_free(root); } @@ -62,7 +63,8 @@ static void t_infix_walk(void) size_t count = 0; do { - tree_search(&values[i], &root, t_compare, 1); + struct tree_node *node = tree_insert(&root, &values[i], t_compare); + check(node != NULL); i = (i * 7) % 11; count++; } while (i != 1); From patchwork Wed Oct 2 10:56:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819630 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48E55205E22 for ; Wed, 2 Oct 2024 10:56:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866596; cv=none; b=KBkoAVu/FULmND1SPi0o4DTPzZGqnl7ECav+8qjXPoGnaBEe6MmzCbqDELKkC+x+KBmcLctXTgnd1hT7xqX2zj6oSZMY3rUCOFJ2ZnQUhythaJjGZCqIo1hGlBxAmkPluSqqj4fR1r0dh/55LdkvG3JtkiDPQ4DmrIOCmyCR9zI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866596; c=relaxed/simple; bh=Js7HP2OJiT05g4/v3YLHPMCkeBdIax6CCb6KII8RBSo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BeOGyIm1fIBeBXvgc9cw+GpIjxfhtFDx8/IxjOhvlA7ZZQxbeazBtZ3JQHbvU0uHt62Ap37c2Lvq5LonDb9MjgbRAWI9jlQvadfsCFVaamR5R1TYSq5+nB4LO56ziK0mG+vE75bOJGCgsctCkfiw+eiqkoF/B2qLGHTisezX+j8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=pgzo/aNy; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=T/eRxuk7; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="pgzo/aNy"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="T/eRxuk7" Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id 716011380253; Wed, 2 Oct 2024 06:56:34 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 02 Oct 2024 06:56:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866594; x=1727952994; bh=g2XDkTI66r qFIZj1IYO46FpbaUeqCOB59ukA85txfDA=; b=pgzo/aNynl1dbKKQnQbRRyywOm 5yJiYCr97gJmAN5WH2ErvrvH4l/V/Nxp18ScCynghT6fq3bvj/cndIACsbe6RzVc 1ujx/tAwBzYHgWm+kniOGXUoobVTiGzHZyCbViw6NzjhjbHiK2yteqOU09uN2cOb ZxcepXSGgk1u0FQR93xDGPoB9Tl46HWn/VRLbFiXaPznz3hJCbvo0pr1q1JfZeWJ Wrz0T/9I5I9K5AL2jBlHSyS4ymzmDVXdCLRdvnZDnx4Tj3COXzXq0ml3GSdelQF3 4tkdL/pFAXYq7NPce89edEz8bPj82fOke7+bWy0OclQ4sOGth5GjWDaErHug== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866594; x=1727952994; bh=g2XDkTI66rqFIZj1IYO46FpbaUeq COB59ukA85txfDA=; b=T/eRxuk7l+QeyKVz6GXo5EQCljo0jvPWenHZjP9MMF1p H7m/p0F/+cC+61l5OxV9l/KMjS1aO98lyGmuUnS5txcNfXoZsJvN8ZcAPqae2w7b 818CcMttj4lCxxRcFd1nI3+2XvRqM/MmQKvhEovc4xiftEvh4wrvLJ4fy9IrG26I Xiz1zXmtV0Q8sHstu/EZw8GOTzVQfqfGdc3BVldtxSDY/YE4t66JF32Y6CV+PzfE /CJ2+MZPqLUrrBMMWjXkh0dFTUwP4It6AAJPaTbodrvKi+9TV/B1LIXk+PWLWFe/ r4BRL2hK3s3J50vHgI34bin1i8XPJwEmGb1F+t7sKg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopegvthhhohhmshhonhesvggufigrrhguthhhohhmshhonhdrtghomhdprhgt phhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtoheplhdrshdrrh esfigvsgdruggv X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:33 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id fb1a65d9 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:43 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:31 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 22/25] reftable: handle trivial allocation failures Message-ID: <28661500ff1710e866966b93c1d7c7d820f4c511.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Handle trivial allocation failures in the reftable library and its unit tests. Signed-off-by: Patrick Steinhardt --- reftable/merged.c | 3 +++ reftable/reader.c | 10 +++++++++- reftable/stack.c | 20 ++++++++++++++++++++ reftable/writer.c | 13 +++++++++++-- t/unit-tests/t-reftable-block.c | 4 ++++ t/unit-tests/t-reftable-merged.c | 4 ++++ t/unit-tests/t-reftable-readwrite.c | 27 ++++++++++++++++----------- 7 files changed, 67 insertions(+), 14 deletions(-) diff --git a/reftable/merged.c b/reftable/merged.c index 8e202a8efd..514d6facf4 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -203,6 +203,9 @@ int reftable_merged_table_new(struct reftable_merged_table **dest, } REFTABLE_CALLOC_ARRAY(m, 1); + if (!m) + return REFTABLE_OUT_OF_MEMORY_ERROR; + m->readers = readers; m->readers_len = n; m->min = first_min; diff --git a/reftable/reader.c b/reftable/reader.c index 0179e4e73d..98e7aa2637 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -598,6 +598,10 @@ int reftable_reader_new(struct reftable_reader **out, int err; REFTABLE_CALLOC_ARRAY(r, 1); + if (!r) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } /* * We need one extra byte to read the type of first block. We also @@ -627,7 +631,11 @@ int reftable_reader_new(struct reftable_reader **out, r->size = file_size - footer_size(r->version); r->source = *source; - r->name = xstrdup(name); + r->name = reftable_strdup(name); + if (!r->name) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } r->hash_id = 0; r->refcount = 1; diff --git a/reftable/stack.c b/reftable/stack.c index 990784d9d2..7df28ab343 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -116,6 +116,11 @@ static int fd_read_lines(int fd, char ***namesp) } REFTABLE_ALLOC_ARRAY(buf, size + 1); + if (!buf) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } + if (read_in_full(fd, buf, size) != size) { err = REFTABLE_IO_ERROR; goto done; @@ -140,6 +145,8 @@ int read_lines(const char *filename, char ***namesp) if (fd < 0) { if (errno == ENOENT) { REFTABLE_CALLOC_ARRAY(*namesp, 1); + if (!*namesp) + return REFTABLE_OUT_OF_MEMORY_ERROR; return 0; } @@ -420,6 +427,10 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st, } REFTABLE_CALLOC_ARRAY(names, 1); + if (!names) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto out; + } } else { err = fd_read_lines(fd, &names); if (err < 0) @@ -779,7 +790,11 @@ int reftable_stack_new_addition(struct reftable_addition **dest, { int err = 0; struct reftable_addition empty = REFTABLE_ADDITION_INIT; + REFTABLE_CALLOC_ARRAY(*dest, 1); + if (!*dest) + return REFTABLE_OUT_OF_MEMORY_ERROR; + **dest = empty; err = reftable_stack_init_addition(*dest, st); if (err) { @@ -886,7 +901,12 @@ int reftable_addition_add(struct reftable_addition *add, REFTABLE_ALLOC_GROW(add->new_tables, add->new_tables_len + 1, add->new_tables_cap); + if (!add->new_tables) { + err = REFTABLE_OUT_OF_MEMORY_ERROR; + goto done; + } add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL); + done: delete_tempfile(&tab_file); strbuf_release(&temp_tab_file_name); diff --git a/reftable/writer.c b/reftable/writer.c index e180c10840..550172e65c 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -49,8 +49,14 @@ static int padded_write(struct reftable_writer *w, uint8_t *data, size_t len, { int n = 0; if (w->pending_padding > 0) { - uint8_t *zeroed = reftable_calloc(w->pending_padding, sizeof(*zeroed)); - int n = w->write(w->write_arg, zeroed, w->pending_padding); + uint8_t *zeroed; + int n; + + zeroed = reftable_calloc(w->pending_padding, sizeof(*zeroed)); + if (!zeroed) + return -1; + + n = w->write(w->write_arg, zeroed, w->pending_padding); if (n < 0) return n; @@ -767,6 +773,9 @@ static int writer_flush_nonempty_block(struct reftable_writer *w) * case we will end up with a multi-level index. */ REFTABLE_ALLOC_GROW(w->index, w->index_len + 1, w->index_cap); + if (!w->index) + return REFTABLE_OUT_OF_MEMORY_ERROR; + index_record.offset = w->next; strbuf_reset(&index_record.last_key); strbuf_addbuf(&index_record.last_key, &w->block_writer->last_key); diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c index e52a612e85..d470060e8b 100644 --- a/t/unit-tests/t-reftable-block.c +++ b/t/unit-tests/t-reftable-block.c @@ -32,6 +32,7 @@ static void t_ref_block_read_write(void) struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); + check(block.data != NULL); block.len = block_size; block_source_from_strbuf(&block.source ,&buf); ret = block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size, @@ -125,6 +126,7 @@ static void t_log_block_read_write(void) struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); + check(block.data != NULL); block.len = block_size; block_source_from_strbuf(&block.source ,&buf); ret = block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size, @@ -214,6 +216,7 @@ static void t_obj_block_read_write(void) struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); + check(block.data != NULL); block.len = block_size; block_source_from_strbuf(&block.source, &buf); ret = block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size, @@ -297,6 +300,7 @@ static void t_index_block_read_write(void) struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT; REFTABLE_CALLOC_ARRAY(block.data, block_size); + check(block.data != NULL); block.len = block_size; block_source_from_strbuf(&block.source, &buf); ret = block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size, diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c index 3d2848632d..3c84363e98 100644 --- a/t/unit-tests/t-reftable-merged.c +++ b/t/unit-tests/t-reftable-merged.c @@ -29,7 +29,9 @@ merged_table_from_records(struct reftable_ref_record **refs, int err; REFTABLE_CALLOC_ARRAY(*readers, n); + check(*readers != NULL); REFTABLE_CALLOC_ARRAY(*source, n); + check(*source != NULL); for (size_t i = 0; i < n; i++) { t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts); @@ -285,7 +287,9 @@ merged_table_from_log_records(struct reftable_log_record **logs, int err; REFTABLE_CALLOC_ARRAY(*readers, n); + check(*readers != NULL); REFTABLE_CALLOC_ARRAY(*source, n); + check(*source != NULL); for (size_t i = 0; i < n; i++) { t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts); diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index acca927a2c..bfa069caff 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -52,8 +52,11 @@ static void write_table(char ***names, struct strbuf *buf, int N, int i; REFTABLE_CALLOC_ARRAY(*names, N + 1); + check(*names != NULL); REFTABLE_CALLOC_ARRAY(refs, N); + check(refs != NULL); REFTABLE_CALLOC_ARRAY(logs, N); + check(logs != NULL); for (i = 0; i < N; i++) { refs[i].refname = (*names)[i] = xstrfmt("refs/heads/branch%02d", i); @@ -150,23 +153,25 @@ static void t_log_overflow(void) static void t_log_write_read(void) { - int N = 2; - char **names = reftable_calloc(N + 1, sizeof(*names)); - int err; struct reftable_write_options opts = { .block_size = 256, }; struct reftable_ref_record ref = { 0 }; - int i = 0; struct reftable_log_record log = { 0 }; - int n; struct reftable_iterator it = { 0 }; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); const struct reftable_stats *stats = NULL; + int N = 2, err, i, n; + char **names; + + names = reftable_calloc(N + 1, sizeof(*names)); + check(names != NULL); + reftable_writer_set_limits(w, 0, N); + for (i = 0; i < N; i++) { char name[256]; struct reftable_ref_record ref = { 0 }; @@ -178,6 +183,7 @@ static void t_log_write_read(void) err = reftable_writer_add_ref(w, &ref); check(!err); } + for (i = 0; i < N; i++) { struct reftable_log_record log = { 0 }; @@ -476,8 +482,7 @@ static void t_table_read_write_seek_index(void) static void t_table_refs_for(int indexed) { - int N = 50; - char **want_names = reftable_calloc(N + 1, sizeof(*want_names)); + char **want_names; int want_names_len = 0; uint8_t want_hash[GIT_SHA1_RAWSZ]; @@ -485,15 +490,15 @@ static void t_table_refs_for(int indexed) .block_size = 256, }; struct reftable_ref_record ref = { 0 }; - int i = 0; - int n; - int err; struct reftable_reader *reader; struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts); struct reftable_iterator it = { 0 }; - int j; + int N = 50, n, j, err, i; + + want_names = reftable_calloc(N + 1, sizeof(*want_names)); + check(want_names != NULL); t_reftable_set_hash(want_hash, 4, GIT_SHA1_FORMAT_ID); From patchwork Wed Oct 2 10:56:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819631 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA62F205E22 for ; Wed, 2 Oct 2024 10:56:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866600; cv=none; b=ouJBoFc4bM7Ax+DjBm/LOznJDP2M6c9izbzjjSsLqo7i6DRY/cPIYsYQs4R0LHuOFhR19m9030k2PCofLL6oDlhtQ34loJDsWI/CIkm5+K51FKifrGgfjxsxYcOi9F2l9FYhgfDtz4IFV3hGJAelJsL2MmvFSWMUlQ/lCw5CTAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866600; c=relaxed/simple; bh=Aiu0TrPVIDSaLetFCGEd4M5+m2hAbO0TMUeO/vJWATk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Kl8BAyUmtqmRxU+GgSFX+7zMKkh3Ia4QjgVtADgccvbuAiEjSCTFHnQKAW6RBp0sMKSo0dJCuC3CCsJ5HuzJNKdrMC/L9NW8nBYsSAw5KH9Rx9DquV7ci1Dt6cXNOv1kLlcWQEgjZtgLI3/Nr5/QcqzRMbYmDHndzSz3QPo5U+Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=oS/yGl5J; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=KVPx8gDr; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="oS/yGl5J"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KVPx8gDr" Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id F08E61140204; Wed, 2 Oct 2024 06:56:37 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Wed, 02 Oct 2024 06:56:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866597; x=1727952997; bh=/eTEYbDW8f ZGHJjkwfDGTD6sOrL9RoAUNxDfRjfK1as=; b=oS/yGl5J5GW0l2P5Tr1l3bjXv8 mGgGQoud27WLr/OGhZ/YV2zU2CwQqhk3RvbGlnbBji/uHObvZ/rLlhFaVVjnGSlE 8gyJ8Z23E8GSGUyqRb8Wd3lPK4edXihgrphMAWhhJMeT+sIcdvWk2qE4dMSPANhE jO8HbkCxs/7rBqVOYsFzTRhZzlEpUT03btzK8AK0u+DUJJB10DMJVrUfKpWARVch FdT7DVvc8PT4PTDRYfT9xkVfJrFNPz+kWnmv1lO5Z9jOm4rKMI7Zy/IFnQ93XSTk eOaRv8JgkWWo2ahOmhYHQlbb0VeVmuQr88Z4HS5yOSLPhyztz6dS0aefR4cg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866597; x=1727952997; bh=/eTEYbDW8fZGHJjkwfDGTD6sOrL9 RoAUNxDfRjfK1as=; b=KVPx8gDruzsmIS8ACGJIdyvena7guEItV+ExnfwsDuQ7 CZAc2Gvai5HhOCEcJqjOfr9gmOSml4nliDLeArjspCXXiFHtGQoJ6BTUroSbQ7k6 LJsm9rYfJF654h6Z2wwpY6wEBN2dFwoD3hWGCg/7imPu4ndqrDRyvs0dAR3sC9yZ y8WQGQKseeN7014md5ImQjmJY3Dyv2T0RsdVnOjAYlu5S8wPXuR7gG8WvK4bqUNP ux7xPncGM3pSmwW2GBlEmrMnBYh6TO6MtbknVpPL9ioR7ysFN94GKClAxoMfdBO4 3estF9iMfPzM/jLbx6dUjegbYDGEZSgrQNGliTjwKw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepveekkeffhfeitdeludeigfejtdetvdelvdduhefgueeg udfghfeukefhjedvkedtnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhdpnhgspghrtghpthhtohepgedpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprh gtphhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtoheplhdrshdr rhesfigvsgdruggvpdhrtghpthhtohepvghthhhomhhsohhnsegvugifrghrughthhhomh hsohhnrdgtohhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:36 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 42ff9cac (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:46 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:33 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 23/25] reftable: fix calls to free(3P) Message-ID: <228cc812632d3c310d9b4f02cacc06fe60f6c23c.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: There are a small set of calls to free(3P) in the reftable library. As the reftable allocators are pluggable we should rather call the reftable specific function, which is `reftable_free()`. Convert the code accordingly. Signed-off-by: Patrick Steinhardt --- reftable/stack.c | 2 +- t/unit-tests/t-reftable-readwrite.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/reftable/stack.c b/reftable/stack.c index 7df28ab343..b2babe8e3d 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -1421,7 +1421,7 @@ static int stack_compact_range(struct reftable_stack *st, struct lock_file *table_lock = &table_locks[i]; char *table_path = get_locked_file_path(table_lock); unlink(table_path); - free(table_path); + reftable_free(table_path); } done: diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index bfa069caff..27ce84445e 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -76,8 +76,8 @@ static void write_table(char ***names, struct strbuf *buf, int N, t_reftable_write_to_buf(buf, refs, N, logs, N, &opts); - free(refs); - free(logs); + reftable_free(refs); + reftable_free(logs); } static void t_log_buffer_size(void) From patchwork Wed Oct 2 10:56:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819632 Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4897C205E30 for ; Wed, 2 Oct 2024 10:56:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.158 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866602; cv=none; b=gUkhwDKA8qf8KSHlIMhrTEg9iM0gqez+Vv8uTYUeZptGNhFjTk17Yvg6QoUxp6mASQvBEnVuZleh5C2Q4s5ncMYY4H4mPA5rMQQIK60LVmiEoKXc+DCvu/yDDTtbLY8SpUxiP6mJRkuXG33O2z2pO27puerVyIc8JTwI7F2Dbvc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866602; c=relaxed/simple; bh=u081I90gmmLlnAJajMhkpRxIBdNhSNZlkgGvfGrqPlc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=U2u27z5IvvtOuoJ70KXrUT9dMzIcXSbfrrpzVdWhnUKc1PmUv4GGNViH3dJsHvukLAH3j0lP0rp6BwSv6Jv0dTGgbm+8mBXE07wkeXH5GhZJuei7pRoIQ5rJc+4+PuhNmJxZTmASgkuoJvMlfLdClF9RoJzUlQmE6Hub+6MsXZg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=kQjpEVFf; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=oJ6gGClr; arc=none smtp.client-ip=103.168.172.158 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="kQjpEVFf"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="oJ6gGClr" Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id 717AC1140231; Wed, 2 Oct 2024 06:56:40 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Wed, 02 Oct 2024 06:56:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866600; x=1727953000; bh=dlMhMVzREL e4UZSTK2pEfSalKtM9Uu7wV91OvMUNORk=; b=kQjpEVFf449t+7GiulWodlI5l3 jgeBzlna9/fSPJn0GcQoIdmao4asthqMl2RYPrjilZPVVm6LdbJ/Z0+iJDu56Voc Ri9NSJMPk+ZvsvWnEeoDg+Ua5C7fEuBKXn3tkMhLm1ZVO5gzd26lk56qlmahl+RN UFwGI7aO+8WJiJ3D8O8VYpeGpWdFgDx2wZLWUyHXvQz5kWF0b0M4S8PKhNKqhkNl MPpHTKgb/P253zDOajqW5vY8VxRXKHMikCIdQtjuG2py4E2oDBtqGWkawRJZac6P 57ZaU+7bLZpgUZ/ivQ8Z+XQHGH+kfWVt0nyDs6ucZ6eyusvrx4VtZndSYqjg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866600; x=1727953000; bh=dlMhMVzRELe4UZSTK2pEfSalKtM9 Uu7wV91OvMUNORk=; b=oJ6gGClrk9IH2rLgyZSQww5TVuwG1EyN6gUGFlW4Iel3 kE5XaozyCxbnL6+mn9cELfOSsE5FEDpjscsad3nMnQY6jv6YOB9J5W2KfB6RUjXw JlWOuLCh8BmJ2xFufF4/Xtd4/ylADMQM9hZn8sX1jgHN6MiRj7si0/Yeff4cbRMH 0pcWxIE0lw2tpad59kFyFiYhCQ2N+BaLtOPpCwG9BfSJmf7v8LAVtP4ZiXzgmB6Y 2ZmPExK9KF/fzfhlcKj+JoKm4otliaCPTzdb9HxlyADA4ZpUYitLV/NFGAOvPu0y FtGa/8u9mfe5Q4q/N4FhhGHPWsUtrAN8g2nu/by7Vg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhephfdvveekteejuddttdfghfegvdejfeejffegjeegjedv tdeftdelfeehtdejtdeunecuffhomhgrihhnpehuphgurghtvgdrvghmrghilhdpuhhpug grthgvrdhnrghmvgenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhl fhhrohhmpehpshesphhkshdrihhmpdhnsggprhgtphhtthhopeegpdhmohguvgepshhmth hpohhuthdprhgtphhtthhopehgihhtshhtvghrsehpohgsohigrdgtohhmpdhrtghpthht ohepvghthhhomhhsohhnsegvugifrghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtoh epghhithesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopehlrdhsrdhrseif vggsrdguvg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:39 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a5e4718f (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:49 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:36 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 24/25] reftable: introduce `REFTABLE_FREE_AND_NULL()` Message-ID: <1c66f6ef8d9827653fd9df960330431877e1dbc9.1727866394.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: We have several calls to `FREE_AND_NULL()` in the reftable library, which of course uses free(3P). As the reftable allocators are pluggable we should rather call the reftable specific function, which is `reftable_free()`. Introduce a new macro `REFTABLE_FREE_AND_NULL()` and adapt the callsites accordingly. Signed-off-by: Patrick Steinhardt --- reftable/basics.h | 1 + reftable/block.c | 6 +++--- reftable/iter.c | 2 +- reftable/pq.c | 2 +- reftable/reader.c | 2 +- reftable/record.c | 10 +++++----- reftable/stack.c | 8 ++++---- reftable/writer.c | 4 ++-- t/unit-tests/t-reftable-stack.c | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/reftable/basics.h b/reftable/basics.h index 69adeab2e4..7f0f20e50c 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -71,6 +71,7 @@ char *reftable_strdup(const char *str); REFTABLE_REALLOC_ARRAY(x, alloc); \ } \ } while (0) +#define REFTABLE_FREE_AND_NULL(p) do { reftable_free(p); (p) = NULL; } while (0) /* Find the longest shared prefix size of `a` and `b` */ struct strbuf; diff --git a/reftable/block.c b/reftable/block.c index bfa7dc61bf..8d41a2f99e 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -551,9 +551,9 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br, void block_writer_release(struct block_writer *bw) { deflateEnd(bw->zstream); - FREE_AND_NULL(bw->zstream); - FREE_AND_NULL(bw->restarts); - FREE_AND_NULL(bw->compressed); + REFTABLE_FREE_AND_NULL(bw->zstream); + REFTABLE_FREE_AND_NULL(bw->restarts); + REFTABLE_FREE_AND_NULL(bw->compressed); strbuf_release(&bw->last_key); /* the block is not owned. */ } diff --git a/reftable/iter.c b/reftable/iter.c index d319538f80..d926db653b 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -237,7 +237,7 @@ void reftable_iterator_destroy(struct reftable_iterator *it) return; it->ops->close(it->iter_arg); it->ops = NULL; - FREE_AND_NULL(it->iter_arg); + REFTABLE_FREE_AND_NULL(it->iter_arg); } int reftable_iterator_seek_ref(struct reftable_iterator *it, diff --git a/reftable/pq.c b/reftable/pq.c index 03b9912282..6ee1164dd3 100644 --- a/reftable/pq.c +++ b/reftable/pq.c @@ -68,6 +68,6 @@ int merged_iter_pqueue_add(struct merged_iter_pqueue *pq, const struct pq_entry void merged_iter_pqueue_release(struct merged_iter_pqueue *pq) { - FREE_AND_NULL(pq->heap); + REFTABLE_FREE_AND_NULL(pq->heap); memset(pq, 0, sizeof(*pq)); } diff --git a/reftable/reader.c b/reftable/reader.c index 98e7aa2637..8d37253922 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -678,7 +678,7 @@ void reftable_reader_decref(struct reftable_reader *r) if (--r->refcount) return; block_source_close(&r->source); - FREE_AND_NULL(r->name); + REFTABLE_FREE_AND_NULL(r->name); reftable_free(r); } diff --git a/reftable/record.c b/reftable/record.c index 787e134c9a..30d563e16d 100644 --- a/reftable/record.c +++ b/reftable/record.c @@ -476,8 +476,8 @@ static void reftable_obj_record_key(const void *r, struct strbuf *dest) static void reftable_obj_record_release(void *rec) { struct reftable_obj_record *obj = rec; - FREE_AND_NULL(obj->hash_prefix); - FREE_AND_NULL(obj->offsets); + REFTABLE_FREE_AND_NULL(obj->hash_prefix); + REFTABLE_FREE_AND_NULL(obj->offsets); memset(obj, 0, sizeof(struct reftable_obj_record)); } @@ -834,10 +834,10 @@ static int reftable_log_record_decode(void *rec, struct strbuf key, if (val_type != r->value_type) { switch (r->value_type) { case REFTABLE_LOG_UPDATE: - FREE_AND_NULL(r->value.update.message); + REFTABLE_FREE_AND_NULL(r->value.update.message); r->value.update.message_cap = 0; - FREE_AND_NULL(r->value.update.email); - FREE_AND_NULL(r->value.update.name); + REFTABLE_FREE_AND_NULL(r->value.update.email); + REFTABLE_FREE_AND_NULL(r->value.update.name); break; case REFTABLE_LOG_DELETION: break; diff --git a/reftable/stack.c b/reftable/stack.c index b2babe8e3d..63976e5cea 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -203,7 +203,7 @@ void reftable_stack_destroy(struct reftable_stack *st) err = read_lines(st->list_file, &names); if (err < 0) { - FREE_AND_NULL(names); + REFTABLE_FREE_AND_NULL(names); } if (st->readers) { @@ -224,7 +224,7 @@ void reftable_stack_destroy(struct reftable_stack *st) } strbuf_release(&filename); st->readers_len = 0; - FREE_AND_NULL(st->readers); + REFTABLE_FREE_AND_NULL(st->readers); } if (st->list_fd >= 0) { @@ -232,8 +232,8 @@ void reftable_stack_destroy(struct reftable_stack *st) st->list_fd = -1; } - FREE_AND_NULL(st->list_file); - FREE_AND_NULL(st->reftable_dir); + REFTABLE_FREE_AND_NULL(st->list_file); + REFTABLE_FREE_AND_NULL(st->reftable_dir); reftable_free(st); free_names(names); } diff --git a/reftable/writer.c b/reftable/writer.c index 550172e65c..b032a47dec 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -593,7 +593,7 @@ static void object_record_free(void *void_arg UNUSED, void *key) { struct obj_index_tree_node *entry = key; - FREE_AND_NULL(entry->offsets); + REFTABLE_FREE_AND_NULL(entry->offsets); strbuf_release(&entry->hash); reftable_free(entry); } @@ -709,7 +709,7 @@ static void writer_clear_index(struct reftable_writer *w) { for (size_t i = 0; w->index && i < w->index_len; i++) strbuf_release(&w->index[i].last_key); - FREE_AND_NULL(w->index); + REFTABLE_FREE_AND_NULL(w->index); w->index_len = 0; w->index_cap = 0; } diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c index 6e41a4cf7e..cc2db2cdef 100644 --- a/t/unit-tests/t-reftable-stack.c +++ b/t/unit-tests/t-reftable-stack.c @@ -1149,7 +1149,7 @@ static void unclean_stack_close(struct reftable_stack *st) for (size_t i = 0; i < st->readers_len; i++) reftable_reader_decref(st->readers[i]); st->readers_len = 0; - FREE_AND_NULL(st->readers); + REFTABLE_FREE_AND_NULL(st->readers); } static void t_reftable_stack_compaction_concurrent_clean(void) From patchwork Wed Oct 2 10:56:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13819633 Received: from fout-a1-smtp.messagingengine.com (fout-a1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0C1D205E35 for ; Wed, 2 Oct 2024 10:56:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.144 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866605; cv=none; b=OKyzX44HhpoA2XGpgNFdk0Cyrp4m9mKZZgbkn0w0SSXw42ICbdWMGxCDiMGUryZv1z7mVyPbmXg9YFPofaksyVZ1tussuoklSqCkNjmuFuyync0bwG9mKIctoAqbbht3AXlF9kI+3nJrbzqW2HTYNzpwS/qfnX4o52fPXzCkIf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727866605; c=relaxed/simple; bh=9ZFMv2JZWW+TRfc3oBkUE+dawIl0I9Mcfu8paRefvTI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=NUy5/hu9X4Mcb/xpA6x7N/hUQqLMhVxNV1LtD3gV9dbtCsY9vjajOHNEuTM7U+u2jpNT5+3X/lwqBJLxvJZ8NOf1ye3PxKqWklFG1x3FcFlEM98nX04EklT/8ppKbnOiFgNkUDJZpyDC4pGdAIw9FWzUrywjEH+3/RA9szR7xgU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=TJLBY0Uu; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=BOapkdGC; arc=none smtp.client-ip=103.168.172.144 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="TJLBY0Uu"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="BOapkdGC" Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfout.phl.internal (Postfix) with ESMTP id 04B48138028E; Wed, 2 Oct 2024 06:56:43 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Wed, 02 Oct 2024 06:56:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1727866603; x=1727953003; bh=TVlxpys1tf cNPIZpDIdXxujPFsiv63h/1MWoXPv87Wk=; b=TJLBY0UuQ1DX39bTXi3Ces6dj0 otOqTkHBrXvJHc6eHtWzgkV4kYrewX5DIIZ4+6yOPDE/n7OjWI69EY/Y8xcmGhet I+hgzZid8S+DcA2H6em5aoI14yoqFN7yjXLIgLCRtntrGmuTgHNmOzHGpHWUfdDW DfnMvDbvJDzx4TGrQGzdWfqngFH72x0IGci6TxS4fNyxi2vgjvNLpE+ZBGzEx3Cg t9EHfGOrJGhdgcqbTaICdf0zz4SkrVRQiQRzOoT4cIzExknFhcj9ncHiMZEgpDFG jMVFDcGUFRCkliCJ9+V6CB4vDoB6YCf9dXdRGyX4bxniF8pVZNwA/uXNrCmQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1727866603; x=1727953003; bh=TVlxpys1tfcNPIZpDIdXxujPFsiv 63h/1MWoXPv87Wk=; b=BOapkdGCLlzWAKMiYmTh0tTF+ycSDJEND1nIayDMGicV O86OGKqNBu9SjmkGnfaQ75r98O57rwEMwhvCVgywldOTMzaEub3hh5UdMvy0yOms SssALX8eT0tCRZcyflP3EEJg/YoFXE0FZz+/Ei03enbNkdAh3qG9uBJ9UKTsDHrs amSXxnpsqV/bNFMhDJNbtOpUFHxI0USMQNQLuiXaXfAwp8B8kSKgNLwAACD+qOAo qIfxH/C1zcqGhjHQy86OapwuRzjd/ja192Dprm/lUnWW99TGjjEo9e1/QrApCKT0 a3rNBeOVMrmr7+n2nh5YgsfcHq8HleuqFO6wj44K7A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduledgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimh eqnecuggftrfgrthhtvghrnhepueeiueduhfevhfekiefgkefggeeljedtffetheehtefh hfdvledukeekfffhffdvnecuffhomhgrihhnpehgohhoghhlvgdrtghomhenucevlhhush htvghrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm pdhnsggprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehgih htshhtvghrsehpohgsohigrdgtohhmpdhrtghpthhtohepvghthhhomhhsohhnsegvugif rghrughthhhomhhsohhnrdgtohhmpdhrtghpthhtohepghhithesvhhgvghrrdhkvghrnh gvlhdrohhrghdprhgtphhtthhopehlrdhsrdhrseifvggsrdguvg X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 2 Oct 2024 06:56:41 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 9e37ef50 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 2 Oct 2024 10:55:51 +0000 (UTC) Date: Wed, 2 Oct 2024 12:56:39 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Edward Thomson , Junio C Hamano , =?utf-8?b?UmVuw6k=?= Scharfe Subject: [PATCH v5 25/25] reftable/basics: ban standard allocator functions Message-ID: References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The reftable library uses pluggable allocators, which means that we shouldn't ever use the standard allocator functions. But it is an easy mistake to make to accidentally use e.g. free(3P) instead of the reftable-specific `reftable_free()` function, and we do not have any mechanism to detect this misuse right now. Introduce a couple of macros that ban the standard allocators, similar to how we do it in "banned.h". Note that we do not ban the following two classes of functions: - Macros like `FREE_AND_NULL()` or `REALLOC_ARRAY()`. As those expand to code that contains already-banned functions we'd get a compiler error even without banning those macros explicitly. - Git-specific allocators like `xmalloc()` and friends. The primary reason is that there are simply too many of them, so we're rather aiming for best effort here. Furthermore, the eventual goal is to make them unavailable in the reftable library place by not pulling them in via "git-compat-utils.h" anymore. Signed-off-by: Patrick Steinhardt --- reftable/basics.c | 1 + reftable/basics.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/reftable/basics.c b/reftable/basics.c index ea53cf102a..c8396dc525 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -6,6 +6,7 @@ license that can be found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd */ +#define REFTABLE_ALLOW_BANNED_ALLOCATORS #include "basics.h" #include "reftable-basics.h" diff --git a/reftable/basics.h b/reftable/basics.h index 7f0f20e50c..4c9ef0fe6c 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -73,6 +73,20 @@ char *reftable_strdup(const char *str); } while (0) #define REFTABLE_FREE_AND_NULL(p) do { reftable_free(p); (p) = NULL; } while (0) +#ifndef REFTABLE_ALLOW_BANNED_ALLOCATORS +# define REFTABLE_BANNED(func) use_reftable_##func##_instead +# undef malloc +# define malloc(sz) REFTABLE_BANNED(malloc) +# undef realloc +# define realloc(ptr, sz) REFTABLE_BANNED(realloc) +# undef free +# define free(ptr) REFTABLE_BANNED(free) +# undef calloc +# define calloc(nelem, elsize) REFTABLE_BANNED(calloc) +# undef strdup +# define strdup(str) REFTABLE_BANNED(strdup) +#endif + /* Find the longest shared prefix size of `a` and `b` */ struct strbuf; int common_prefix_size(struct strbuf *a, struct strbuf *b);