From patchwork Thu Apr 13 11:16:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13210157 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 8E31FC77B61 for ; Thu, 13 Apr 2023 11:16:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230399AbjDMLQY (ORCPT ); Thu, 13 Apr 2023 07:16:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjDMLQM (ORCPT ); Thu, 13 Apr 2023 07:16:12 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2601BF for ; Thu, 13 Apr 2023 04:16:10 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 4B4DC5C0143; Thu, 13 Apr 2023 07:16:10 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 13 Apr 2023 07:16:10 -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=1681384570; x=1681470970; bh=gd 9KpwTyqqu1to0OfxFuskUgZclLgXDuX5CdVRAHRqQ=; b=aqjOL5LFXBZqAC1caO hI4tK+VuanOQxuvP/Vwv5gWdgaV0v2U6kLxm/sSvUTx8dPKz52VxO8oiSUjMLRaZ fnXbtlYo0cJ6ksQqYBsQbrcqaqxoUsHX9n0RFP/l4GrDAb5/9+Oxg55e4uOs9aB8 DVdo1uEpVaIPZjmIh7qBr1HP62hvt5Gwi0bVWGEBHYAmPklvJkSE5xAS1QXASlo3 rselXBV6g1JrJRHGMFc/epw8cwlGfk4eEj3kGZoN6thMi1e8al9/ZjGJ1edfYBIl kk01JFYDNgG98XYmpd1SQhEpgCaQ6SZbzilTc9D0lrYz1WqtQOVYS7yOs2dSffGW A0lw== 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=1681384570; x=1681470970; bh=gd9KpwTyqqu1t o0OfxFuskUgZclLgXDuX5CdVRAHRqQ=; b=ZDBHYtmjG4OmV8rHcbBy+wngNhGB6 DRDpKwtcy8e1KmkUp7wC7e2/GIOuE7z9YkDGfgJvt0LNKsFpJUGSIQl/vwv3AdzG oW7wuq8Lz/CVsuKnY3cvK2r83orGFvHLPDqw87/0u/WX2k/UunGEvAfkcbkmovbL G5WYD612jKACINJNkgPMk22liDHKJgoUCAXh21enTzMFXo/Y/3XGmEk/MipAeMQF gvatefArG7HtJUkb37IEh2/IhKfxa17Tf4rSbouwGOoxr3eH9+FiUnBvlSTfIZaZ +QJMHMAB5ex7wyOidyGlTGWXLot9LWrG3SgQlKW7DBe+u5YpyXITfqYtA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdekkedgfeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehgtd erredttddvnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucggtffrrghtthgvrhhnpeeukedtvedtffevleejtefgheehieegke eluddvfeefgeehgfeltddtheejleffteenucevlhhushhtvghrufhiiigvpedtnecurfgr rhgrmhepmhgrihhlfhhrohhmpehpshesphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 13 Apr 2023 07:16:08 -0400 (EDT) Received: by pks.im (OpenSMTPD) with ESMTPSA id 3ea7c0d1 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 13 Apr 2023 11:15:45 +0000 (UTC) Date: Thu, 13 Apr 2023 13:16:06 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Taylor Blau , peff@peff.net, dstolee@microsoft.com Subject: [PATCH v3 01/10] midx: fix segfault with no packs and invalid preferred pack Message-ID: 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 resetting the preferred packfile index to `-1` before searching for the preferred pack. This fixes the segfault as we already check for whether the index is `> - 1`. If it is not, we simply don't pick a preferred packfile at all. Helped-by: Taylor Blau Signed-off-by: Patrick Steinhardt --- midx.c | 6 +++--- t/t5319-multi-pack-index.sh | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/midx.c b/midx.c index 47989f7ea7..67eb617591 100644 --- a/midx.c +++ b/midx.c @@ -1328,17 +1328,17 @@ static int write_midx_internal(const char *object_dir, } if (preferred_pack_name) { - int found = 0; + ctx.preferred_pack_idx = -1; + for (i = 0; i < ctx.nr; i++) { if (!cmp_idx_or_pack_name(preferred_pack_name, ctx.info[i].pack_name)) { ctx.preferred_pack_idx = i; - found = 1; break; } } - if (!found) + if (ctx.preferred_pack_idx == -1) warning(_("unknown preferred pack: '%s'"), preferred_pack_name); } else if (ctx.nr && diff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh index 499d5d4c78..0883c7c6bd 100755 --- a/t/t5319-multi-pack-index.sh +++ b/t/t5319-multi-pack-index.sh @@ -183,6 +183,18 @@ 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 && + warning: unknown preferred pack: ${SQ}does-not-exist${SQ} + 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