From patchwork Wed Apr 12 10:22:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13208727 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F238C7619A for ; Wed, 12 Apr 2023 10:22:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231252AbjDLKWm (ORCPT ); Wed, 12 Apr 2023 06:22:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231127AbjDLKWi (ORCPT ); Wed, 12 Apr 2023 06:22:38 -0400 Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2C146E86 for ; Wed, 12 Apr 2023 03:22:36 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 1A9283200979; Wed, 12 Apr 2023 06:22:36 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 12 Apr 2023 06:22: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:sender :subject:subject:to:to; s=fm3; t=1681294955; x=1681381355; bh=ZO ulI9eTvPJ9mLVAQ9N04IyJwCVTvNCP0mGWIoNXvNk=; b=HRewMT62A3UEACzJPh 9T+IRuZLATbZfbY8DITDeOyv5LlQwhDDNh118jn/PbFrRv+5SufebCuDgpdKCqiA 1cnFNwa2iEw/zSFvEtDCyH2Wt3RqbqkFM+kHiLzRuZWDNlNdpwFMcZo9xBVuFour q13C/fCSDLk2vxKkMq0l9J3kwncOdyHcd2G3qoGO1YMuHBbHhk1wMhHHi2soS0Ms V4mqu0stW9lFhMtaSB5BUoajazUDtGi/5ZfNYH9ZncQ3DC8NoPm2D7bvgjOu2lXG ldcrtVZ+7gKh/0xcDswqB7YG0hp0iq0jqy0Y6l83gUSAXbcmDPuXdTLaacWPFesh PWTQ== 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:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1681294955; x=1681381355; bh=ZOulI9eTvPJ9m LVAQ9N04IyJwCVTvNCP0mGWIoNXvNk=; b=MtZDz8Wxw/rZhqwpnXq4e31PFELPJ QGV1H0DgdQX2Y4vmvhss/pHXvLWBRZZGBoFGg8xIY8vQd+4zjVTPM7FlzOlL2SmZ 2ynWDKYwApKaWuu/oZLtcZmzsxVhSw0CxyNDyw/nA6+BzBwh/+QQDEPTSnpQhmwr soBYXZ/bo0iMPR3wYZEeNBOpXn2zW8rJqvstvRG6C+iqDpSGALXVzE3lbeaqYUaL QT8vr2XnXsKpBNKNmhR9lBuWUp+bC/S1SEEGdVU7IPNFHMVFpyDrPR/Yn0jVFJHm wD6HqelzyxofDe75P2yvgXdo5CFs8v3cZCYd9whWXVjSwoGcMD0g7GT/A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdekiedgvdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 12 Apr 2023 06:22:34 -0400 (EDT) Received: by pks.im (OpenSMTPD) with ESMTPSA id f124c3ef (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 12 Apr 2023 10:22:12 +0000 (UTC) Date: Wed, 12 Apr 2023 12:22:31 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Taylor Blau , peff@peff.net, dstolee@microsoft.com Subject: [PATCH v2 1/8] midx: fix segfault with no packs and invalid preferred pack Message-ID: <5ecad306b42441fa7d4f50bdfb9c09ccce22b6c9.1681294715.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When asked to write a multi-pack-index the user can specify a preferred pack that is used as a tie breaker when multiple packs contain the same objects. When this packfile cannot be found, we just pick the first pack that is getting tracked by the newly written multi-pack-index as a fallback. Picking the fallback can fail in the case where we're asked to write a multi-pack-index with no packfiles at all: picking the fallback value will cause a segfault as we blindly index into the array of packfiles, which would be empty. Fix this bug by exiting early in case we have determined that the MIDX wouldn't have any packfiles to index. While the request itself does not make much sense anyway, it is still preferable to exit gracefully than to abort. Signed-off-by: Patrick Steinhardt --- midx.c | 6 ++++++ t/t5319-multi-pack-index.sh | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/midx.c b/midx.c index 9af3e5de88..22ea7ffb75 100644 --- a/midx.c +++ b/midx.c @@ -1307,6 +1307,12 @@ static int write_midx_internal(const char *object_dir, for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); stop_progress(&ctx.progress); + if (!ctx.nr) { + error(_("no pack files to index.")); + result = 1; + goto cleanup; + } + if ((ctx.m && ctx.nr == ctx.m->num_packs) && !(packs_to_include || packs_to_drop)) { struct bitmap_index *bitmap_git; diff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh index 499d5d4c78..be7f3c1e1f 100755 --- a/t/t5319-multi-pack-index.sh +++ b/t/t5319-multi-pack-index.sh @@ -183,6 +183,17 @@ test_expect_success 'write midx with --stdin-packs' ' compare_results_with_midx "mixed mode (one pack + extra)" +test_expect_success 'write with no objects and preferred pack' ' + test_when_finished "rm -rf empty" && + git init empty && + test_must_fail git -C empty multi-pack-index write \ + --stdin-packs --preferred-pack=does-not-exist err && + cat >expect <<-EOF && + error: no pack files to index. + EOF + test_cmp expect err +' + test_expect_success 'write progress off for redirected stderr' ' git multi-pack-index --object-dir=$objdir write 2>err && test_line_count = 0 err