From patchwork Tue Sep 28 02:18:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521457 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F7E7C433EF for ; Tue, 28 Sep 2021 02:19:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7F79061247 for ; Tue, 28 Sep 2021 02:19:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238575AbhI1CUm (ORCPT ); Mon, 27 Sep 2021 22:20:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238529AbhI1CUl (ORCPT ); Mon, 27 Sep 2021 22:20:41 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73EB2C061604 for ; Mon, 27 Sep 2021 19:19:02 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id d21so55081066wra.12 for ; Mon, 27 Sep 2021 19:19:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NOTtP7QG8bBmVdro4EjXBoilQTXKAEyvSH13/rvxGOE=; b=Ih8oAEb3olRPcrx8joifg/+hbapiR6EBy7Z6dZHpY/Fk834jN7scsxvE+mUo2AHgXS iP4Uw2fSF3Dz34BxiMsiwwUrQ8x2ZJ4fLIHldrHodCej+7iW71zMkcm35Uf+5Dsi+lO7 IIfocnOVtHMh8Hbk0v2lWyA2Vv5wvIaWn34t8hH2Bh41aKWL4TuY7LIMt/sYp/T3L88Q 8RcEKqKmkX5DTTCddKGDvOJxGs+eHaQxUXw2dFbUyzEBfnxNWAAhlkLhPUY6J5WsuR8V nTdxblkODLFnr/ycO32KI8/3yHI8Wfri0UKjQueCpYbHnSRznloOSQhqXKMqavvMUeNP PouQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NOTtP7QG8bBmVdro4EjXBoilQTXKAEyvSH13/rvxGOE=; b=Ydqkow+8roSnwkQ+NX+gsRd23r3MTsWS+7w87sLG//i71y1GzufYCMVMLxS2UakV2S I1EBcMlJvL1qvnRxX6xoxFCpC7DMR3rzh5P7+bQb0dlUTQ5cms0RH3bbA4YaHWpcQEay YtWOmumS+SZh05vaLb5LmMhyrvbZNc1Vo9U7WDPI1gUXz3hbtetVCZA1BuKQvSqUo5iP 6CuiY/4KgDyW3Jdk7fJhNNVip5H8e5pWr1qVE0eevrEox8grgpyH+e01aYVAJ2F/NhZg Cv6J102muFvw3f+I6D4i1ozdtFZuuTQ7oFBU5qvgtGmNakOgWbaXvB7X9YzU0tB5l6ax wa/w== X-Gm-Message-State: AOAM533xSJr6dUx+jvMtBHVRkZnWmmmpXXu5a0P+Fj0baLAVDgMau8Ze mIlqb7Naowmo+NVExzTKW/1XvBG1j6+QqA== X-Google-Smtp-Source: ABdhPJxry7FOdeFmKftEWcYVjmNrorvlD5dAG0G3Uhdt63LgAjRyRS/ctE/qtcHRKyTseAHJF87tPQ== X-Received: by 2002:a5d:6545:: with SMTP id z5mr3534483wrv.90.1632795540886; Mon, 27 Sep 2021 19:19:00 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:00 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 01/17] fsck tests: add test for fsck-ing an unknown type Date: Tue, 28 Sep 2021 04:18:42 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a blindspot in the fsck tests by checking what we do when we encounter an unknown "garbage" type produced with hash-object's --literally option. This behavior needs to be improved, which'll be done in subsequent patches, but for now let's test for the current behavior. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1450-fsck.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index 5071ac63a5b..969bfbbdd8f 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -865,4 +865,21 @@ test_expect_success 'detect corrupt index file in fsck' ' test_i18ngrep "bad index file" errors ' +test_expect_success 'fsck hard errors on an invalid object type' ' + git init --bare garbage-type && + ( + cd garbage-type && + + empty=$(git hash-object --stdin -w -t blob err.expect <<-\EOF && + fatal: invalid object type + EOF + test_must_fail git fsck >out 2>err && + test_cmp err.expect err && + test_must_be_empty out + ) +' + test_done From patchwork Tue Sep 28 02:18:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521461 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63234C4332F for ; Tue, 28 Sep 2021 02:19:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4E2E861260 for ; Tue, 28 Sep 2021 02:19:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238606AbhI1CUo (ORCPT ); Mon, 27 Sep 2021 22:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238567AbhI1CUm (ORCPT ); Mon, 27 Sep 2021 22:20:42 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38E3CC061575 for ; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id x20so10536694wrg.10 for ; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YIzUQxPGKq5KMfj4KRupphPBQSuNyWG5kGI8GHhLclM=; b=ReviMLbgiaU0MFmTEiM49jy6FYmPO62TwkyxvMgB0M/bh/jVBv0DHV7UIVHfHiLPyZ T8RYrTqrp/jaP63swtTZg9C315UMYcFphv5ja7NaphNTE0dQlQ4GmkNc+IXsnYICBrmS xMnH2DPA/jNDWzqr2/Yq6ea7a3bF7EdDfPL/JdEfNdpahUfrai6mBS9T7XZm/Xv28iGp vtR/emn03p56/gm6amdDR8Gb0TrjqxMvZ3cNl75IL0IOHZNuBGYfEpDKPSTyNLiXiM6K yCx2pmTbK3pols9RDJwtUqfZna9D0Y9dDiptpnfSo0JfsaPPtD5pS2Uw5kziWd6qcyNE BteQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YIzUQxPGKq5KMfj4KRupphPBQSuNyWG5kGI8GHhLclM=; b=DeO31VfOP68zwD3Z4swX2z9luHWtcL2E6D12HFxGKNlF3Cp47952hU47+nHAC4ANo/ rA0aLRl33vMM/8y3vqZm8uPGJNnHdDTlMC1Ce5JOrGc4yrqtBT9WRm3YsDMaMIB6qB1u SB4tmg9i30x0CWfcCVWG/Gak+vCRUKuGDpHy454gXmD2Sp/lOdJFtzQHz5SsEK1ZBr5N noKFQ9OT9+Cbg8yQ1qY5Xg07Kb8tF4stVuSaKxz96aEJ8tEiXhPVWMZ+TzSJxw6mXQZS 1EGcNPzNKsE1pmawNppeJWPHpfCnxlC9lH0Wa4IMd8EHAlyCZCiclfZasucg2VRENYjY 2tKA== X-Gm-Message-State: AOAM531ED54wRqJmlM02qPlG2353f3uYyB/WRaZTqPSsMjhFHtSeiqiO lTBNdHGIRy8RwK5joScP+XWA+w1He6hVdg== X-Google-Smtp-Source: ABdhPJxF/3wvQWwNZoqNshq3M62+ffb+xMIB2qOv5WpDcAlXWPoymhxtqTmEoafHo950rrsXKNssYQ== X-Received: by 2002:a5d:4243:: with SMTP id s3mr3806183wrr.216.1632795541640; Mon, 27 Sep 2021 19:19:01 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:01 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 02/17] fsck tests: refactor one test to use a sub-repo Date: Tue, 28 Sep 2021 04:18:43 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Refactor one of the fsck tests to use a throwaway repository. It's a pervasive pattern in t1450-fsck.sh to spend a lot of effort on the teardown of a tests so we're not leaving corrupt content for the next test. We can instead use the pattern of creating a named sub-repository, then we don't have to worry about cleaning up after ourselves, nobody will care what state the broken "hash-mismatch" repository is after this test runs. See [1] for related discussion on various "modern" test patterns that can be used to avoid verbosity and increase reliability. 1. https://lore.kernel.org/git/87y27veeyj.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1450-fsck.sh | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index 969bfbbdd8f..f8edd15abf8 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -48,24 +48,25 @@ remove_object () { rm "$(sha1_file "$1")" } -test_expect_success 'object with bad sha1' ' - sha=$(echo blob | git hash-object -w --stdin) && - old=$(test_oid_to_path "$sha") && - new=$(dirname $old)/$(test_oid ff_2) && - sha="$(dirname $new)$(basename $new)" && - mv .git/objects/$old .git/objects/$new && - test_when_finished "remove_object $sha" && - git update-index --add --cacheinfo 100644 $sha foo && - test_when_finished "git read-tree -u --reset HEAD" && - tree=$(git write-tree) && - test_when_finished "remove_object $tree" && - cmt=$(echo bogus | git commit-tree $tree) && - test_when_finished "remove_object $cmt" && - git update-ref refs/heads/bogus $cmt && - test_when_finished "git update-ref -d refs/heads/bogus" && +test_expect_success 'object with hash mismatch' ' + git init --bare hash-mismatch && + ( + cd hash-mismatch && - test_must_fail git fsck 2>out && - test_i18ngrep "$sha.*corrupt" out + oid=$(echo blob | git hash-object -w --stdin) && + old=$(test_oid_to_path "$oid") && + new=$(dirname $old)/$(test_oid ff_2) && + oid="$(dirname $new)$(basename $new)" && + + mv objects/$old objects/$new && + git update-index --add --cacheinfo 100644 $oid foo && + tree=$(git write-tree) && + cmt=$(echo bogus | git commit-tree $tree) && + git update-ref refs/heads/bogus $cmt && + + test_must_fail git fsck 2>out && + grep "$oid.*corrupt" out + ) ' test_expect_success 'branch pointing to non-commit' ' From patchwork Tue Sep 28 02:18:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521463 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D94BC433EF for ; Tue, 28 Sep 2021 02:19:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EABC76127A for ; Tue, 28 Sep 2021 02:19:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238608AbhI1CUo (ORCPT ); Mon, 27 Sep 2021 22:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238592AbhI1CUm (ORCPT ); Mon, 27 Sep 2021 22:20:42 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F27C5C061575 for ; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id g16so55222739wrb.3 for ; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=P50YPnIH11C10wWjZFy8wMfbPIc/A+tG21VtHGADvtk=; b=Si+cozM95/UlWG8Zz2g/a1yP/jFSN8cleaIIF8whIwBc2NjgjAj0ktap6cqr1DAZWd 5k35GCtCCcXChnKIqOAM9VxbakW4dszsfhgv7rB/gfHML4+5o8MzGm0DaZHgAijHtT9K w11JRrhk+r0/iW7K+1p1B+h0N+AAz37dx4SHcbpdhttIGLCrxLAs+CG1q4PMuwWNuIOb ZGwgZjBE3jh0o1fcse7gE67MtpHk3Oqajsh2cV9tk1strbW4oFfdR2M31kiaidOHvJaG x/Hx1lVkq+xeGAIEYRKK0xB2332vSqOtdsmK8bkVB+IQ3i6XkZ8yri2qD1qDA9WgWY1v XKCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P50YPnIH11C10wWjZFy8wMfbPIc/A+tG21VtHGADvtk=; b=JDV/CBSC1lDug9N+f7+eEj8RMLpIzCL1zy4TmQD42Xmx3aHcS7XJ5nvY2/aAgBm5ZQ bI97rVcjuEn3f42tGIlPEDNC+/Ut80B0MijTMIfAiYuA0h17Jbv3Wo6G38av9qf4sG7E o3NF/6SCj+vTn6L+bRo7nXWR6J8Cm7jfbY0IN0pwDKYP2kjNLzak8x1CziMiA93c9/ad zZKZdYKu8gaWRDsiQv3NpNdsxako7M2e4zWhoFlsWIHF3cR078GMvmWfDfGNx2L49gsu ar/py5J0VUHWh3bRccrvP5mieRhoLjNBfhe0XJmG0kz0OrOMEux3csbAf97hKLsLTkui TDHA== X-Gm-Message-State: AOAM532l7Q8Ubt2EgObnetMNEj2yJHOvxkt+emVi3TWHO59cTE7wJvZo BxXr+uLoDi9O7bYE310GPBPwswkI/z0+qA== X-Google-Smtp-Source: ABdhPJyuyW8Rwnd4XTqkGrA4/mT94DRcYuRQWzzIzJulrfLYYezGuzSiBGHwJ/oEWdulfQlht6E8zQ== X-Received: by 2002:a5d:664f:: with SMTP id f15mr2303454wrw.143.1632795542330; Mon, 27 Sep 2021 19:19:02 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:01 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 03/17] fsck tests: test current hash/type mismatch behavior Date: Tue, 28 Sep 2021 04:18:44 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org If fsck we move an object around between .git/objects/?? directories to simulate a hash mismatch "git fsck" will currently hard die() in object-file.c. This behavior will be fixed in subsequent commits, but let's test for it as-is for now. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1450-fsck.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index f8edd15abf8..175ed304637 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -69,6 +69,30 @@ test_expect_success 'object with hash mismatch' ' ) ' +test_expect_success 'object with hash and type mismatch' ' + git init --bare hash-type-mismatch && + ( + cd hash-type-mismatch && + + oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) && + old=$(test_oid_to_path "$oid") && + new=$(dirname $old)/$(test_oid ff_2) && + oid="$(dirname $new)$(basename $new)" && + + mv objects/$old objects/$new && + git update-index --add --cacheinfo 100644 $oid foo && + tree=$(git write-tree) && + cmt=$(echo bogus | git commit-tree $tree) && + git update-ref refs/heads/bogus $cmt && + + cat >expect <<-\EOF && + fatal: invalid object type + EOF + test_must_fail git fsck 2>actual && + test_cmp expect actual + ) +' + test_expect_success 'branch pointing to non-commit' ' git rev-parse HEAD^{tree} >.git/refs/heads/invalid && test_when_finished "git update-ref -d refs/heads/invalid" && From patchwork Tue Sep 28 02:18:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521465 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60ACAC433FE for ; Tue, 28 Sep 2021 02:19:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4090661247 for ; Tue, 28 Sep 2021 02:19:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238617AbhI1CUq (ORCPT ); Mon, 27 Sep 2021 22:20:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238512AbhI1CUn (ORCPT ); Mon, 27 Sep 2021 22:20:43 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C95D2C061575 for ; Mon, 27 Sep 2021 19:19:04 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id x20so10536763wrg.10 for ; Mon, 27 Sep 2021 19:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jOD6bMoeab8G0bSyc1ooH1a9r327BrXjvT93FoAlkTM=; b=gzlzYu9bpVs7BMOXgx/xwd/lbCahm3jGDreOA4GwXKvXimn6TgBrbksna3dYjDR5dL q8vPSdgeaO6G2f9tqIA3BXvJD7cKdBvjLWSVlzO6GPX4uIJsE8/Xq//LkPiE1tbISfoL q060bq7nUKnx/EwCZMEJrUWdLJul+qkId3tJTGoQyBrfFgMP88rUXT4g6Gsc20RSSI2e GnQAkV1DXa1nwgcI6NhdZTFJG0/RwB5BaK5d4zqAMHaWaywQ5TLrGFE/4ie2R+7Sr+1s Tp7cxbpQtDi4gdH/RpEOzxH9EfpWd4hfdT7/p5Cu4wPziwujNk9O4yyXXL/EULV90Ir2 lRnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jOD6bMoeab8G0bSyc1ooH1a9r327BrXjvT93FoAlkTM=; b=VwI8UEEQlEcQOsKhtTOBqXvU1Qlzo09OnPsOJdFSSsB8mxzAlja7MsXTiwERq4LxQx TDg11mApUJEG5l3wQspIljsJowVSb9zIyKkxEmV0lpb1yvzYv9vYzuMaY4i0UlHONy6T yWZnamZQesfDwRBJcvJgCXbztCAZDHzkbXhY/Sq1Rq3cHiyAXEmCLhWtW8+w3Lk6pqX0 dmb1mS5NsIDssII6Qa57vEE0NYbB5FsHQXb0C1wNlaUq3/Q/qxEIG3pKiQCgQ02a3cKa /AImqvExUcsgqIog0mDuyQHFZnda38MFE+vkdtYNsCOScUAsDbOuKTW043A2ye+iY15h gSLg== X-Gm-Message-State: AOAM5331JkGTVv/sfmjw/0tIWyYautICxBcT1fRW6uqw3TVNApaYgh+X iiWI1wZeOULR2IQAx8Fhf5XBhO6Oy2RUGA== X-Google-Smtp-Source: ABdhPJyn/o1qNnHZ6t9ZMtR/pX2yB0YTIuK510dJzhyVtMlvdATrtodIV8g5kh41gc65Ezl04kCySA== X-Received: by 2002:a5d:4b50:: with SMTP id w16mr3509476wrs.71.1632795543241; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:02 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 04/17] fsck tests: test for garbage appended to a loose object Date: Tue, 28 Sep 2021 04:18:45 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There wasn't any output tests for this scenario, let's ensure that we don't regress on it in the changes that come after this. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1450-fsck.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index 175ed304637..bd696d21dba 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -93,6 +93,26 @@ test_expect_success 'object with hash and type mismatch' ' ) ' +test_expect_success POSIXPERM 'zlib corrupt loose object output ' ' + git init --bare corrupt-loose-output && + ( + cd corrupt-loose-output && + oid=$(git hash-object -w --stdin --literally >$oidf && + + cat >expect.error <<-EOF && + error: garbage at end of loose object '\''$oid'\'' + error: unable to unpack contents of ./$oidf + error: $oid: object corrupt or missing: ./$oidf + EOF + test_must_fail git fsck 2>actual && + grep ^error: actual >error && + test_cmp expect.error error + ) +' + test_expect_success 'branch pointing to non-commit' ' git rev-parse HEAD^{tree} >.git/refs/heads/invalid && test_when_finished "git update-ref -d refs/heads/invalid" && From patchwork Tue Sep 28 02:18:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521467 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E4FEC433F5 for ; Tue, 28 Sep 2021 02:19:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EF0BB61262 for ; Tue, 28 Sep 2021 02:19:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238611AbhI1CUr (ORCPT ); Mon, 27 Sep 2021 22:20:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238567AbhI1CUo (ORCPT ); Mon, 27 Sep 2021 22:20:44 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E8CEC061575 for ; Mon, 27 Sep 2021 19:19:05 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id t18so55268451wrb.0 for ; Mon, 27 Sep 2021 19:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iI+WM2HBrYyDrGeWCfoUsX86fKnG1r5hj88mRCKeUw4=; b=A14ZMwq/1r0V8U4GCzyLJIHJP5eNg3/QTW+x7mCYRGqRaqNJ5SUPb4NMeuq7ULEuPu cnk5tsBga8R+njNgO2k0XyPegl2BLbGWCuqkOUufAa4GpgIKNo6k6dtt1XJ1xgpI/7RA i6iz0A/SFayTT10UvvTlW38QvzS+++otxd/y11dx7G02sxTb8U6cHrCNGvBQXk0FAkt2 zBbyXWClBsgfrbwpLeOs1vtHW3uI/qpgyJisN9AxL5GW87caUg7LZMeIk6q2vlAxSyua ZrYHMteaOJJ05/N1EzO6omf9bIUpJOiLfNv6OPdkqpZQ5acSkjn4KjV3NTibfOOLpkog txYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iI+WM2HBrYyDrGeWCfoUsX86fKnG1r5hj88mRCKeUw4=; b=jNJ6K5poSt1El3GOO5XbcD+Foz5yIelKrGRR4KK2XDgcddM3sp7I/twlgcA74gct/j GOfvUo0AfuCUFHhHhnGokKeJ7WwI3s1rnwCeLhHrANqtANAlVZ2rF6TuXGCKNo9nUqv0 77Kr5S2pWJpChDDTehCYn6EFQCVSB1Mn6474kwCMJ2fzYdAZPbbv32uj7T36nzi7+ig3 yYUk2o72K8Ffn6oRny2sJVWuVsSfVszARpj3ixBo55atv11+e/jdmMOH7zBLtR8wvAfI gzCKwRYpQBinMctKaDOAsY56kc77Q5jRqEvWiy1XEbCvtacOPLGtjAuPSx+GUiPPO0P7 xtAQ== X-Gm-Message-State: AOAM5336j1JLsUyaPTo0gHbR/v8VS6gzYiMDxk/VWYbSzeTizGba+rEL NmjTscUSIgRtUGdu62ZIj6mTfrELNmvNnw== X-Google-Smtp-Source: ABdhPJzcn3h20cBbUS+sM1dm+icmLxgYahXGlt7yVgu6ZPU8bD8R6pACu9XkVETgmxjn1JPlxTUvDA== X-Received: by 2002:a05:6000:144d:: with SMTP id v13mr3438449wrx.303.1632795543895; Mon, 27 Sep 2021 19:19:03 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:03 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 05/17] cat-file tests: move bogus_* variable declarations earlier Date: Tue, 28 Sep 2021 04:18:46 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the short/long bogus bogus object type variables into a form where the two sets can be used concurrently. This'll be used by subsequently added tests. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1006-cat-file.sh | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 18b3779ccb6..ea6a53d425b 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -315,36 +315,39 @@ test_expect_success '%(deltabase) reports packed delta bases' ' } ' -bogus_type="bogus" -bogus_content="bogus" -bogus_size=$(strlen "$bogus_content") -bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin) +test_expect_success 'setup bogus data' ' + bogus_short_type="bogus" && + bogus_short_content="bogus" && + bogus_short_size=$(strlen "$bogus_short_content") && + bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) && + + bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" && + bogus_long_content="bogus" && + bogus_long_size=$(strlen "$bogus_long_content") && + bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin) +' test_expect_success "Type of broken object is correct" ' - echo $bogus_type >expect && - git cat-file -t --allow-unknown-type $bogus_sha1 >actual && + echo $bogus_short_type >expect && + git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && test_cmp expect actual ' test_expect_success "Size of broken object is correct" ' - echo $bogus_size >expect && - git cat-file -s --allow-unknown-type $bogus_sha1 >actual && + echo $bogus_short_size >expect && + git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual && test_cmp expect actual ' -bogus_type="abcdefghijklmnopqrstuvwxyz1234679" -bogus_content="bogus" -bogus_size=$(strlen "$bogus_content") -bogus_sha1=$(echo_without_newline "$bogus_content" | git hash-object -t $bogus_type --literally -w --stdin) test_expect_success "Type of broken object is correct when type is large" ' - echo $bogus_type >expect && - git cat-file -t --allow-unknown-type $bogus_sha1 >actual && + echo $bogus_long_type >expect && + git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual && test_cmp expect actual ' test_expect_success "Size of large broken object is correct when type is large" ' - echo $bogus_size >expect && - git cat-file -s --allow-unknown-type $bogus_sha1 >actual && + echo $bogus_long_size >expect && + git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual && test_cmp expect actual ' From patchwork Tue Sep 28 02:18:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521469 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7392CC433EF for ; Tue, 28 Sep 2021 02:19:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58D2161262 for ; Tue, 28 Sep 2021 02:19:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238624AbhI1CUt (ORCPT ); Mon, 27 Sep 2021 22:20:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238592AbhI1CUp (ORCPT ); Mon, 27 Sep 2021 22:20:45 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A3E0C061575 for ; Mon, 27 Sep 2021 19:19:06 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id t8so55322815wri.1 for ; Mon, 27 Sep 2021 19:19:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gVf2klUvKrJzi92Idrp/ceD4GOQ4ihxxamGH7CtMziY=; b=UDHcH0FOtpiNfSvUrzhYyIUd3UOZqKCxS1lhDqpszKGadzGom+9urOfUoMT8dqAwVI UurRczKBLgD92lrPe7qvyepEY5DAry+uOLGCOIAo7fiuzBm4cf58JJI0nItSkFOEwuGE ylNU7QrfxzQm/qSWOKzm/h68LQRiCeUgR06l2nipYcrOS3LwbUfXcCPFHSPHG44TJIYF Tv3eyIgN0A3rgcHBOqog7n7cKpwqLnWDSAXFZJneuqKbQLE5aI8Qaqnlp6a7ttCDEhqw 4w/z3gnDysK+4YaI61jZX4fKq+cni0DAcNU7kX0TyboimsC5+NJrjVIX/XRUD357ZkhV 1+4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gVf2klUvKrJzi92Idrp/ceD4GOQ4ihxxamGH7CtMziY=; b=xjxPP7fyobzu3RUXJ6PU4Ztqvzoyfr7TOAd1cMshqfY8rctwnCIUHNQXTIyqzay5IA YDFmi6Voi3eGvpXNuxp9mnqTFZRas3HKN24RDrH9sZ6f6PHQrqQ5Bs3641+umO3CSPO6 EdUOQzvY1PEfOqCBYzyo+5iHQ1Sk5gQOOay59P+ZOp2WoJImT0OrE7c8ZFm53xQ5l5Wt wKUPbkj7OR2aR4/YhFJgGOQI9uzlubCE2fWIKqZGvS38KrulW4pGwQ1iiqfj0+pEQDei gJDw1TZAWlhY+X9BRxKp9/uYbA1W0lUzYJzw7HRIX/jyv9qkXqkOQY1ELZbcX0KRgdkO d0RA== X-Gm-Message-State: AOAM5339edueX91K1zg84aSb7ymh4ND6WRdLlkeQM4lynUgdrSBTcVc1 uxP0eOeAMSS8GeLZXkvZ1FDSFBGCB5ZT9A== X-Google-Smtp-Source: ABdhPJyvkre2lVRv7t0D2mXp7Z92Wuf1nBDqliK+8gu9yBJC3dcQ0fVOWYcavk2+kskDC22v1J8f6w== X-Received: by 2002:a5d:4608:: with SMTP id t8mr3699449wrq.136.1632795544646; Mon, 27 Sep 2021 19:19:04 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:04 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 06/17] cat-file tests: test for missing/bogus object with -t, -s and -p Date: Tue, 28 Sep 2021 04:18:47 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When we look up a missing object with cat_one_file() what error we print out currently depends on whether we'll error out early in get_oid_with_context(), or if we'll get an error later from oid_object_info_extended(). The --allow-unknown-type flag then changes whether we pass the "OBJECT_INFO_ALLOW_UNKNOWN_TYPE" flag to get_oid_with_context() or not. The "-p" flag is yet another special-case in printing the same output on the deadbeef OID as we'd emit on the deadbeef_short OID for the "-s" and "-t" options, it also doesn't support the "--allow-unknown-type" flag at all. Let's test the combination of the two sets of [-t, -s, -p] and [--{no-}allow-unknown-type] (the --no-allow-unknown-type is implicit in not supplying it), as well as a [missing,bogus] object pair. This extends tests added in 3e370f9faf0 (t1006: add tests for git cat-file --allow-unknown-type, 2015-05-03). Signed-off-by: Ævar Arnfjörð Bjarmason --- t/oid-info/oid | 2 ++ t/t1006-cat-file.sh | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/t/oid-info/oid b/t/oid-info/oid index a754970523c..7547d2c7903 100644 --- a/t/oid-info/oid +++ b/t/oid-info/oid @@ -27,3 +27,5 @@ numeric sha1:0123456789012345678901234567890123456789 numeric sha256:0123456789012345678901234567890123456789012345678901234567890123 deadbeef sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef deadbeef sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef +deadbeef_short sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbee +deadbeef_short sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index ea6a53d425b..abf57339a29 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -327,6 +327,81 @@ test_expect_success 'setup bogus data' ' bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin) ' +for arg1 in '' --allow-unknown-type +do + for arg2 in -s -t -p + do + if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p" + then + continue + fi + + + test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" ' + cat >expect <<-\EOF && + fatal: invalid object type + EOF + + if test "$arg1" = "--allow-unknown-type" + then + git cat-file $arg1 $arg2 $bogus_short_sha1 + else + test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual && + test_must_be_empty out && + test_cmp expect actual + fi + ' + + test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" ' + if test "$arg2" = "-p" + then + cat >expect <<-EOF + error: unable to unpack $bogus_long_sha1 header + fatal: Not a valid object name $bogus_long_sha1 + EOF + else + cat >expect <<-EOF + error: unable to unpack $bogus_long_sha1 header + fatal: git cat-file: could not get object info + EOF + fi && + + if test "$arg1" = "--allow-unknown-type" + then + git cat-file $arg1 $arg2 $bogus_short_sha1 + else + test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual && + test_must_be_empty out && + test_cmp expect actual + fi + ' + + test_expect_success "cat-file $arg1 $arg2 error on missing short OID" ' + cat >expect.err <<-EOF && + fatal: Not a valid object name $(test_oid deadbeef_short) + EOF + test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual && + test_must_be_empty out + ' + + test_expect_success "cat-file $arg1 $arg2 error on missing full OID" ' + if test "$arg2" = "-p" + then + cat >expect.err <<-EOF + fatal: Not a valid object name $(test_oid deadbeef) + EOF + else + cat >expect.err <<-\EOF + fatal: git cat-file: could not get object info + EOF + fi && + test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual && + test_must_be_empty out && + test_cmp expect.err err.actual + ' + done +done + test_expect_success "Type of broken object is correct" ' echo $bogus_short_type >expect && git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && From patchwork Tue Sep 28 02:18:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521471 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10679C433FE for ; Tue, 28 Sep 2021 02:19:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E620A61266 for ; Tue, 28 Sep 2021 02:19:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238638AbhI1CUw (ORCPT ); Mon, 27 Sep 2021 22:20:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238612AbhI1CUq (ORCPT ); Mon, 27 Sep 2021 22:20:46 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B9E4C061740 for ; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id f78-20020a1c1f51000000b0030cdb3d6079so1481290wmf.3 for ; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3+VgJQBF4Ibz/S7wpfnLpadfvhVfbkj78rCHCz1DmPQ=; b=nDIoMFBDot53ifg5GAxHzVIuLHN71GGAaQ27YF1lOVa/iClCDQ1RdjpuLJsr873QSf JzZYfmpMNgC3JV4exlA9S4NI3rb3Ly6Ch/sHfPWL514cElHrN1IHi7PsISQ5QsbxyVf9 kWYJ/3bskoICCDsAX4j4ZOil2y+fOv/OsmC8KHskRLDxW3p9msU7OaUZ8+Ic4pdRnAtp gyCaxDbHV9ClRO+ANeRgyh0rRbuzIgSJY6vk1udLSiJdzFTN2sVi1nxCcZtnxHorQHQW 8CoPVg9bL2TppSThTMFxn/6IMkWfr3CxFoeJvb0De2o+y5ersUKbxSIOtRrZcjyDmH7o dBlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3+VgJQBF4Ibz/S7wpfnLpadfvhVfbkj78rCHCz1DmPQ=; b=aZHGYSkrBwLQ1l8bfP8JXEiAWeRQRPZ+bNQyKrCrmv7Z72eqlhcFJyWYec4duXxLK0 ZLPK8rtsaWoAnUhtnuzIsPDMl7AgmFfRQ3LlWUP4m3cMrqofwjhyEBJ/O4x+L1AnFAcD 3gQsUwmcyFXDMVVfhZubvWPgHiZVb2vjcrtgHrPfCY/40vMaGjI3byVFJ4L+fqph03Ab Ut6S6T0bG5PeWL/4XFmfisbwYG02B4ixZQsIPjkIFx3lzMnG7f61qBXNhml0QWz9r8d5 qeVyTNTDsbXoDze18ySE/gTYHZxandSqDq97UA4mjPpSSdH/vy8fJ6pZTXwltvwSgzJa qZ3Q== X-Gm-Message-State: AOAM532icqgEwuRFtwtr6RemLektEeeiD8DCkQ+p4AaCXV1P0jQdbUfi 7epwZYg4jxkuTkHn1Hd0YH6sIuoALnW4aA== X-Google-Smtp-Source: ABdhPJxxZLjNTvNuRTlupfu0lbKHbMCQK0JB0ri7ALjOJBPG4Ibzc/TSUgSBnnGSqiAHC1YUWq0igg== X-Received: by 2002:a1c:43c3:: with SMTP id q186mr2164852wma.143.1632795545517; Mon, 27 Sep 2021 19:19:05 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:04 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 07/17] cat-file tests: add corrupt loose object test Date: Tue, 28 Sep 2021 04:18:48 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Fix a blindspot in the tests for "cat-file" (and by proxy, the guts of object-file.c) by testing that when we can't decode a loose object with zlib we'll emit an error from zlib.c. Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1006-cat-file.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index abf57339a29..15774979ad3 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -426,6 +426,58 @@ test_expect_success "Size of large broken object is correct when type is large" test_cmp expect actual ' +test_expect_success 'cat-file -t and -s on corrupt loose object' ' + git init --bare corrupt-loose.git && + ( + cd corrupt-loose.git && + + # Setup and create the empty blob and its path + empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) && + git hash-object -w --stdin other.blob && + other_blob=$(git hash-object -w --stdin out.expect <<-EOF && + 0 + EOF + git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual && + test_must_be_empty err.actual && + test_cmp out.expect out.actual && + + # Swap the two to corrupt the repository + mv -f "$other_path" "$empty_path" && + test_must_fail git fsck 2>err.fsck && + grep "hash mismatch" err.fsck && + + # confirm that cat-file is reading the new swapped-in + # blob... + cat >out.expect <<-EOF && + blob + EOF + git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual && + test_must_be_empty err.actual && + test_cmp out.expect out.actual && + + # ... since it has a different size now. + cat >out.expect <<-EOF && + 6 + EOF + git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual && + test_must_be_empty err.actual && + test_cmp out.expect out.actual && + + # So far "cat-file" has been happy to spew the found + # content out as-is. Try to make it zlib-invalid. + mv -f other.blob "$empty_path" && + test_must_fail git fsck 2>err.fsck && + grep "^error: inflate: data stream error (" err.fsck + ) +' + # Tests for git cat-file --follow-symlinks test_expect_success 'prep for symlink tests' ' echo_without_newline "$hello_content" >morx && From patchwork Tue Sep 28 02:18:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521473 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8755CC433F5 for ; Tue, 28 Sep 2021 02:19:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 70D3E61262 for ; Tue, 28 Sep 2021 02:19:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238643AbhI1CUx (ORCPT ); Mon, 27 Sep 2021 22:20:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238618AbhI1CUq (ORCPT ); Mon, 27 Sep 2021 22:20:46 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF146C06176C for ; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id b192so2120841wmb.2 for ; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=941xkVdEIIfKskTrUanhKcz+5TrptFvB/pvltBzo3Rc=; b=Kn8ueQhi6YKMpzD+Ie1ri2dn9Zweusxpsr1RGnGCcqX+DCep6AMlSwljKpXoI7VAqd 5J8U3Byb5leIVkazEFduQs+iLemSd0EjYoqR5q6a1wczYKgF7Njzf44in/Ira7kWjbdh 3RLGD446YP7jOpuESZj6Y18YtTPTJ8TA9H/GOYhXn421WSXaiInY4Tymo3sghwIEPpaL Gwj7CTQ6yZsr6ijkgZiOYdNUr83XD8PMRZ8mcXdjeUUCCCizidiIjrbwG8QUXLtbUOgA 6GCvtXCcGowopULsYIUwcAvnKMN7mEdz89LZlISPbXStJK80FTH0p7pqul8iAeNTinvG EP/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=941xkVdEIIfKskTrUanhKcz+5TrptFvB/pvltBzo3Rc=; b=y7Z4ncpQnVuHOLgU2QdvATwm5VrMmycpiD5ssLc0ShxjkFI2ZxZ6JVwn3fhLGMjLmK sWbt/D3SHdxoK5DdX7+h04WZsvqIuQnpKgTHKgFje8ADdunyQQzCwcCQQqpaHA+czlVk J9wXf9nPm3/COFgb8LxTEjiVA/J/m2uY12EwiteeGpVSjoR/sV1e78Bz5Yixhb/40Z60 TGyFBBxXGPbp9P8V/VUFipsQhua93aTE99KAxkNsfQjeaMxivmGbZ4/Iu4iJsJZo6Pf/ LfdfTIXMBNfWnO2J/EcVftFZuUGP2Btef7cxnz+ze3ZJsx+QRyxAksRDEEznQlTm7j/F 3u/Q== X-Gm-Message-State: AOAM5334TmliC9+QfRBN73AgY+PBPSeaduYuHz7/tmtKJfFIz9p6oXYK mm2+GlSz08DDMrr5vc/nrnUfM+B4+JuQTQ== X-Google-Smtp-Source: ABdhPJwM8NDKCiPSRhC1i4bQGWMEAxxE42VTb8cDv05wKSEmO/um+XbWtZ08y+ugU9DELak1T2b5jA== X-Received: by 2002:a05:600c:230b:: with SMTP id 11mr2283736wmo.102.1632795546184; Mon, 27 Sep 2021 19:19:06 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:05 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 08/17] cat-file tests: test for current --allow-unknown-type behavior Date: Tue, 28 Sep 2021 04:18:49 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Add more tests for the current --allow-unknown-type behavior. As noted in [1] I don't think much of this makes sense, but let's test for it as-is so we can see if the behavior changes in the future. 1. https://lore.kernel.org/git/87r1i4qf4h.fsf@evledraar.gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- t/t1006-cat-file.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 15774979ad3..5b16c69c286 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -402,6 +402,67 @@ do done done +test_expect_success '-e is OK with a broken object without --allow-unknown-type' ' + git cat-file -e $bogus_short_sha1 +' + +test_expect_success '-e can not be combined with --allow-unknown-type' ' + test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1 +' + +test_expect_success '-p cannot print a broken object even with --allow-unknown-type' ' + test_must_fail git cat-file -p $bogus_short_sha1 && + test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1 +' + +test_expect_success ' does not work with objects of broken types' ' + cat >err.expect <<-\EOF && + fatal: invalid object type "bogus" + EOF + test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual && + test_cmp err.expect err.actual +' + +test_expect_success 'broken types combined with --batch and --batch-check' ' + echo $bogus_short_sha1 >bogus-oid && + + cat >err.expect <<-\EOF && + fatal: invalid object type + EOF + + test_must_fail git cat-file --batch err.actual && + test_cmp err.expect err.actual && + + test_must_fail git cat-file --batch-check err.actual && + test_cmp err.expect err.actual +' + +test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' ' + test_expect_code 128 git cat-file --batch --allow-unknown-type expect <<-EOF && + $bogus_short_type + EOF + git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && + test_cmp expect actual && + + # Create it manually, as "git replace" will die on bogus + # types. + head=$(git rev-parse --verify HEAD) && + test_when_finished "rm -rf .git/refs/replace" && + mkdir -p .git/refs/replace && + echo $head >.git/refs/replace/$bogus_short_sha1 && + + cat >expect <<-EOF && + commit + EOF + git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && + test_cmp expect actual +' + test_expect_success "Type of broken object is correct" ' echo $bogus_short_type >expect && git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual && From patchwork Tue Sep 28 02:18:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521475 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 050AAC433FE for ; Tue, 28 Sep 2021 02:19:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DCB8961247 for ; Tue, 28 Sep 2021 02:19:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238621AbhI1CUz (ORCPT ); Mon, 27 Sep 2021 22:20:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238620AbhI1CUr (ORCPT ); Mon, 27 Sep 2021 22:20:47 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5F95C06176D for ; Mon, 27 Sep 2021 19:19:08 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id r11-20020a1c440b000000b0030cf0f01fbaso969406wma.1 for ; Mon, 27 Sep 2021 19:19:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=d564A1KdVdLFXzWwd5C0iHFxsjimTsWNt6g1L+VMLRg=; b=kNts406AnEVYFkvd1mlt00DI8foPV0VwSN5b76iSYCmKNIzxGg1iFqXLEJocLmqctS uuvGax1F7jblrOAU6AGopzNXtiAkUCh7NYMt+7iPFOu3JhSqV+FxB8Tsm1GOgu4du/fi L0b5JtuRHgDW5RhEFycdklGVrc6z/UGheD2DQIv5PeXFvdU8BnlLW1GfpbQBnE3i7kFf 1XhSe/CEuHRzrPbrxEpFjALI/05XxT/WV6PibDkq3hS7f8T+utr/4wRdQO8scFg1XCkr ONMUCK7bQAxY3UCcmzMi6aVVnGhDevsTqwZN50U7uqX1k5zgA/3D4xDWpDeBvRS3/JK5 P6iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=d564A1KdVdLFXzWwd5C0iHFxsjimTsWNt6g1L+VMLRg=; b=vJ1OZ6u1gJerZRC+WHVwHO+LpjrjcLGSLSge3C2wD4GtXGoV1dxEOXA2m2oNzojN5q 19KTqRlOl3//tgC/xTQFj6sFoIoDy0zY/fGAKIGdE7gruZ+tSMiq8nhnHPWd1U2RP3o4 RWuCT17nMis2T/sR0YwP4tI+f0vFMv17RMOSzZr6TinsJQGjWqzeoY8lrJHkNbE5dVj/ 3G5vgSGOUrfBsRDSfrXyyrPuifSAfBRGLXNU5EpaKa0LDaGS5hfP2KgWxgU43+wBJpL1 LySyoIZbcF0aId1UsgGiexKQExgY7a0+WVgC7PmdZQl2rIVyq+zPrMnnyg/Kf4nf+0Dp Lafw== X-Gm-Message-State: AOAM531s6+qZnHfGQW2Htkq8f/2D/7gHrHq57BG2PKzdtNUSf/odwuts ilC24pXJpJvqyNR0pXQ1NnIOKiBu83SFwQ== X-Google-Smtp-Source: ABdhPJxJNBCrLikC57VMS9aKSQBWS5iUhT2gmAHHhpb1AjIC19DjNwInpPxbK7ItTV4Imj3BS0wpOA== X-Received: by 2002:a1c:2543:: with SMTP id l64mr2185260wml.9.1632795547019; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:06 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 09/17] object-file.c: don't set "typep" when returning non-zero Date: Tue, 28 Sep 2021 04:18:50 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org When the loose_object_info() function returns an error stop faking up the "oi->typep" to OBJ_BAD. Let the return value of the function itself suffice. This code cleanup simplifies subsequent changes. That we set this at all is a relic from the past. Before 052fe5eaca9 (sha1_loose_object_info: make type lookup optional, 2013-07-12) we would always return the type_from_string(type) via the parse_sha1_header() function, or -1 (i.e. OBJ_BAD) if we couldn't parse it. Then in a combination of 46f034483eb (sha1_file: support reading from a loose object of unknown type, 2015-05-03) and b3ea7dd32d6 (sha1_loose_object_info: handle errors from unpack_sha1_rest, 2017-10-05) our API drifted even further towards conflating the two again. Having read the code paths involved carefully I think this is OK. We are just about to return -1, and we have only one caller: do_oid_object_info_extended(). That function will in turn go on to return -1 when we return -1 here. This might be introducing a subtle bug where a caller of oid_object_info_extended() would inspect its "typep" and expect a meaningful value if the function returned -1. Such a problem would not occur for its simpler oid_object_info() sister function. That one always returns the "enum object_type", which in the case of -1 would be the OBJ_BAD. Having read the code for all the callers of these functions I don't believe any such bug is being introduced here, and in any case we'd likely already have such a bug for the "sizep" member (although blindly checking "typep" first would be a more common case). Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/object-file.c b/object-file.c index be4f94ecf3b..766ba88b851 100644 --- a/object-file.c +++ b/object-file.c @@ -1525,8 +1525,6 @@ static int loose_object_info(struct repository *r, git_inflate_end(&stream); munmap(map, mapsize); - if (status && oi->typep) - *oi->typep = status; if (oi->sizep == &size_scratch) oi->sizep = NULL; strbuf_release(&hdrbuf); From patchwork Tue Sep 28 02:18:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521479 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 978A7C433FE for ; Tue, 28 Sep 2021 02:19:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7D64261247 for ; Tue, 28 Sep 2021 02:19:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238644AbhI1CU6 (ORCPT ); Mon, 27 Sep 2021 22:20:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238567AbhI1CUs (ORCPT ); Mon, 27 Sep 2021 22:20:48 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61E78C061604 for ; Mon, 27 Sep 2021 19:19:09 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id c73-20020a1c9a4c000000b0030d040bb895so1493406wme.2 for ; Mon, 27 Sep 2021 19:19:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=b88RcZqYlYMpUuXMPMIWwB3G8ZEG8s3VzD/ra7YFT/o=; b=YzK9xEoXxHTzMGb9TcSVwFh92xm9nCU+5ZVKQOmoWmWL7DdubeagifjP3DV/D780gG 5VTpG35Rv3OQuwGmuc8Pc93sJ0P+3XXb0/JzoeZRIkmeaq1RFhZL71F+Nr9iH+Cl6jSM 4mQGSQa1s5xgX9CgV6krjnH/CzHyclv1MAJsMMWuT6nNiQBpLfn6NKL1QN1dhRZSRNqD t/yU+f49h6bMq8urPIwZpJWgcZufuw0eaLMZqoEefAaXtpuE5MyU7T97RHWDR5B82bN8 KFCqF1UzH+hUqp0jawtSR1dTtLYQO+TAekUdklLnvmK0L6ywlNLO0Om++7uHFAD/qcoz q+pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=b88RcZqYlYMpUuXMPMIWwB3G8ZEG8s3VzD/ra7YFT/o=; b=bK4lP7UpGMo7dvKO//3KdhYbxf2sIlwIgdtI7VrvaK7WD+osEc7JDjqL7Ucwv4PMCS nQHdspunRJv+1o4Ua1fRqvIMbZ0Mph6dKoQxhpzGaIyrB8PjrHpHTTRGgRTXtW2CtrFf 3VhP/1KVxGkplcDWvvBN06+NYn5COIkWYmBiode1QsJq2B4xxMZnBRdk91vdRcpb2mwM rjrp3HNe0gb0r5qfAobvEIKAnxeni0VlvmQSsVsOx3T12FzLSFGp2tRk6dUGjhwLAZzz dVXsDomenuH1Rs3rUEB7XN8x0xifCSnqvZHO2Sj5Hqp8HxDNEhOUlPhikCJZkyVGGmtD fc/g== X-Gm-Message-State: AOAM533mZhqk1TIuC1SGMb2HPm01MOagBWdlfNj/wWn0YqutI50xxS7D aGKZ1syeTMw7DHoB50QWciCpRqJ0MYoPzA== X-Google-Smtp-Source: ABdhPJzWTKI0ZVXv5EhtS10S8pEr3nwDol7njopZFeHYNzO2gcIaTvEHFs9C8ciMl1/TxCbi173cNg== X-Received: by 2002:a1c:7e53:: with SMTP id z80mr2203759wmc.152.1632795547824; Mon, 27 Sep 2021 19:19:07 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:07 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 10/17] object-file.c: return -1, not "status" from unpack_loose_header() Date: Tue, 28 Sep 2021 04:18:51 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Return a -1 when git_inflate() fails instead of whatever Z_* status we'd get from zlib.c. This makes no difference to any error we report, but makes it more obvious that we don't care about the specific zlib error codes here. See d21f8426907 (unpack_sha1_header(): detect malformed object header, 2016-09-25) for the commit that added the "return status" code. As far as I can tell there was never a real reason (e.g. different reporting) for carrying down the "status" as opposed to "-1". At the time that d21f8426907 was written there was a corresponding "ret < Z_OK" check right after the unpack_sha1_header() call (the "unpack_sha1_header()" function was later rename to our current "unpack_loose_header()"). However, that check was removed in c84a1f3ed4d (sha1_file: refactor read_object, 2017-06-21) without changing the corresponding return code. So let's do the minor cleanup of also changing this function to return a -1. Signed-off-by: Ævar Arnfjörð Bjarmason --- object-file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/object-file.c b/object-file.c index 766ba88b851..8475b128944 100644 --- a/object-file.c +++ b/object-file.c @@ -1284,7 +1284,7 @@ int unpack_loose_header(git_zstream *stream, buffer, bufsiz); if (status < Z_OK) - return status; + return -1; /* Make sure we have the terminating NUL */ if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) From patchwork Tue Sep 28 02:18:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521477 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5542FC433F5 for ; Tue, 28 Sep 2021 02:19:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3F97361260 for ; Tue, 28 Sep 2021 02:19:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238660AbhI1CU4 (ORCPT ); Mon, 27 Sep 2021 22:20:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238626AbhI1CUt (ORCPT ); Mon, 27 Sep 2021 22:20:49 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 305CEC06176A for ; Mon, 27 Sep 2021 19:19:10 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id s24so2118072wmh.4 for ; Mon, 27 Sep 2021 19:19:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fEiiMNLeWhHpkPeWI7AgqY35zAw9fM7TjvheilTsgNg=; b=aPMETPDnW7IoIHliC7Me0zXIHZgri4rLKgwTtlJBNKlGYOtcBQMHga2X7TL+BdOlHI K4WaqqLS4hRkF4qK2xjqAzX9WbRJRbuMDXa/TezeftKJmHp1XPBFetAXLchqAjhNdRnH lvYOZ9R0augbui9lQDn6P9RJ+ezHoKd4Nd2tOiF44XxLXAkWQrvpdql2e8eItaJnYwH1 rNziR1xHuHEHR7BBwiWHbd7eIc6Y6Wl3TeRi40QyLMhA1Y6s+TsYPlpuVuQVknC+3uKL 1TtdyW+Qs74Xo8KQv+GTS5xeuCjBITfOEfWJ5fL3plStEM3/2sNkDiYSMQBWfHwdryU/ fhfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fEiiMNLeWhHpkPeWI7AgqY35zAw9fM7TjvheilTsgNg=; b=l+7oISu7YoMOak+QcpgqqTEOTx5FPKJTG6RyOttg/woEKDW81/ISBpi6Khsq7vFdzp mvCqRIeXWyghW0tj8IaeaZdagtQ5kH3UFGnpGl6m6cbCyyVo/vLexNkWUOedahYgRaTH LULg+aZUqaRr4olRUS9eOBfdDYOxFd2zZmoFbIh34G/oHxfuuKVymTUSexgP50p8Cunm TvYZjDV9dUrpmIq6mG1BMc5yZ8bUVkR+4eodXSZBHhY3V2VAR0TpfyJdpfbqUDLOcblZ puHMp13ViDgQH6GnX8uZzY/IQvmPgdT5I0mmON5aun4QErKF18lDK4Ep56wi5lMV5Pg3 twhg== X-Gm-Message-State: AOAM532OeLysUc7e2Fh6IdjiHLcf/8yCYnwFdcOBf2qEYUQ6aCVSFj0e N5BXyd+9mlEnApoRkjClwQFxb3f6O0JwDg== X-Google-Smtp-Source: ABdhPJyMv+h9l1r+GJa0skXVXxVQkHjbbWkZ7zJygUEcL+6y8B46MBoFietJhRN1qaEWhq1xpgBnuQ== X-Received: by 2002:a05:600c:896:: with SMTP id l22mr2193135wmp.173.1632795548583; Mon, 27 Sep 2021 19:19:08 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:08 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 11/17] object-file.c: make parse_loose_header_extended() public Date: Tue, 28 Sep 2021 04:18:52 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Make the parse_loose_header_extended() function public and remove the parse_loose_header() wrapper. The only direct user of it outside of object-file.c itself was in streaming.c, that caller can simply pass the required "struct object-info *" instead. This change is being done in preparation for teaching read_loose_object() to accept a flag to pass to parse_loose_header(). It isn't strictly necessary for that change, we could simply use parse_loose_header_extended() there, but will leave the API in a better end state. It would be a better end-state to have already moved the declaration of these functions to object-store.h to avoid the forward declaration of "struct object_info" in cache.h, but let's leave that cleanup for some other time. 1. https://lore.kernel.org/git/patch-v6-09.22-5b9278e7bb4-20210907T104559Z-avarab@gmail.com/ Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 4 +++- object-file.c | 20 +++++++------------- streaming.c | 5 ++++- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cache.h b/cache.h index f6295f3b048..35f254dae4a 100644 --- a/cache.h +++ b/cache.h @@ -1320,7 +1320,9 @@ char *xdg_cache_home(const char *filename); int git_open_cloexec(const char *name, int flags); #define git_open(name) git_open_cloexec(name, O_RDONLY) int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz); -int parse_loose_header(const char *hdr, unsigned long *sizep); +struct object_info; +int parse_loose_header(const char *hdr, struct object_info *oi, + unsigned int flags); int check_object_signature(struct repository *r, const struct object_id *oid, void *buf, unsigned long size, const char *type); diff --git a/object-file.c b/object-file.c index 8475b128944..6b91c4edcf6 100644 --- a/object-file.c +++ b/object-file.c @@ -1385,8 +1385,8 @@ static void *unpack_loose_rest(git_zstream *stream, * too permissive for what we want to check. So do an anal * object header parse by hand. */ -static int parse_loose_header_extended(const char *hdr, struct object_info *oi, - unsigned int flags) +int parse_loose_header(const char *hdr, struct object_info *oi, + unsigned int flags) { const char *type_buf = hdr; unsigned long size; @@ -1446,14 +1446,6 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi, return *hdr ? -1 : type; } -int parse_loose_header(const char *hdr, unsigned long *sizep) -{ - struct object_info oi = OBJECT_INFO_INIT; - - oi.sizep = sizep; - return parse_loose_header_extended(hdr, &oi, 0); -} - static int loose_object_info(struct repository *r, const struct object_id *oid, struct object_info *oi, int flags) @@ -1508,10 +1500,10 @@ static int loose_object_info(struct repository *r, if (status < 0) ; /* Do nothing */ else if (hdrbuf.len) { - if ((status = parse_loose_header_extended(hdrbuf.buf, oi, flags)) < 0) + if ((status = parse_loose_header(hdrbuf.buf, oi, flags)) < 0) status = error(_("unable to parse %s header with --allow-unknown-type"), oid_to_hex(oid)); - } else if ((status = parse_loose_header_extended(hdr, oi, flags)) < 0) + } else if ((status = parse_loose_header(hdr, oi, flags)) < 0) status = error(_("unable to parse %s header"), oid_to_hex(oid)); if (status >= 0 && oi->contentp) { @@ -2599,6 +2591,8 @@ int read_loose_object(const char *path, unsigned long mapsize; git_zstream stream; char hdr[MAX_HEADER_LEN]; + struct object_info oi = OBJECT_INFO_INIT; + oi.sizep = size; *contents = NULL; @@ -2613,7 +2607,7 @@ int read_loose_object(const char *path, goto out; } - *type = parse_loose_header(hdr, size); + *type = parse_loose_header(hdr, &oi, 0); if (*type < 0) { error(_("unable to parse header of %s"), path); git_inflate_end(&stream); diff --git a/streaming.c b/streaming.c index 5f480ad50c4..8beac62cbb7 100644 --- a/streaming.c +++ b/streaming.c @@ -223,6 +223,9 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, const struct object_id *oid, enum object_type *type) { + struct object_info oi = OBJECT_INFO_INIT; + oi.sizep = &st->size; + st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize); if (!st->u.loose.mapped) return -1; @@ -231,7 +234,7 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, st->u.loose.mapsize, st->u.loose.hdr, sizeof(st->u.loose.hdr)) < 0) || - (parse_loose_header(st->u.loose.hdr, &st->size) < 0)) { + (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0)) { git_inflate_end(&st->z); munmap(st->u.loose.mapped, st->u.loose.mapsize); return -1; From patchwork Tue Sep 28 02:18:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521481 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B4E2C433EF for ; Tue, 28 Sep 2021 02:19:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 754D761262 for ; Tue, 28 Sep 2021 02:19:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238654AbhI1CU7 (ORCPT ); Mon, 27 Sep 2021 22:20:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238630AbhI1CUu (ORCPT ); Mon, 27 Sep 2021 22:20:50 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 112C1C06176D for ; Mon, 27 Sep 2021 19:19:11 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id t16-20020a1c7710000000b003049690d882so752779wmi.5 for ; Mon, 27 Sep 2021 19:19:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xcg4+LCcJJ5IPtvEtv8hp1X0fWyHojV4w8MVvyc3nwY=; b=HaS1XIf5wxFicm4ehERDqgS/Y73lwEYQFJFCD8g/yPIa6b4/XWs4BjzZwYCJVRhAR1 O+QsyOAy1DUIU9EPGvBtZ2ZrE+8rTnnasc8elK8iDKVP0exe06N9P3gn2YhlHddHQ2zt oTt578sg2NgOMC+kmamQYmv0u7HpiIn01TM0BQlCVGrYdoz/0iB6zvZ2NxOchU2YlPAu 8kd1eDk5M1ZBxB/p5girh+2VFVISyqZquOxqQnlDyY5Jh401dijLSk8MQTE4DINpvSWe aecxCmwy8fbGA16ayDoECSKTU8RpbwE4ngAyEVColSiBqXaK5hElYfu+nprEP1Vo+TRy i2rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xcg4+LCcJJ5IPtvEtv8hp1X0fWyHojV4w8MVvyc3nwY=; b=s/44EDl+2r3ZNyGoE58f7EtGWqvDKWZLZATX4XPeoKVsa93IPs0hXQBpMgq8yorYrr 2NFm3dbWFGx9afAGG0goI/i2YmdliH9OKWzT0Jozv+HZAUOHJGXKrTLrWhPLIQWaGxcU j5DghmSKUn6kJrQXfIBivUefl+TOBZWysMERQ3eUuWcwa0qjg933bC2DlVun6d/qyL2a oI3hoHaQWtmSxP1DSu22LOJXOJiXFBnRRyyHms74lRy92s5J0J+XXQAj0OT6poO7mI9i hVh7SAguA5zJ+oWKKcSQbob4sliOSDtGd3xQ50jJM4qE60zlXNbZ5DBvxu8Oo8bzxhwe uU1Q== X-Gm-Message-State: AOAM533TQ21614rAgZ9p/I6Z8zL/U4Hh18+Z1J0Q6z1mWtL6/8VCiICu cogjNkHj3tNFi+pnpgNyiwC40tIQF5tF5A== X-Google-Smtp-Source: ABdhPJxuifthq6TlWTD6iXBks4MpwJufH+7U8r8l6zLpIuc/VHHE13Qpy4H2AvKWwe2uIzQurRIhSw== X-Received: by 2002:a1c:7e10:: with SMTP id z16mr2197046wmc.141.1632795549423; Mon, 27 Sep 2021 19:19:09 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:08 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 12/17] object-file.c: simplify unpack_loose_short_header() Date: Tue, 28 Sep 2021 04:18:53 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Combine the unpack_loose_short_header(), unpack_loose_header_to_strbuf() and unpack_loose_header() functions into one. The unpack_loose_header_to_strbuf() function was added in 46f034483eb (sha1_file: support reading from a loose object of unknown type, 2015-05-03). Its code was mostly copy/pasted between it and both of unpack_loose_header() and unpack_loose_short_header(). We now have a single unpack_loose_header() function which accepts an optional "struct strbuf *" instead. I think the remaining unpack_loose_header() function could be further simplified, we're carrying some complexity just to be able to emit a garbage type longer than MAX_HEADER_LEN, we could alternatively just say "we found a garbage type ..." instead. But let's leave the current behavior in place for now. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 17 ++++++++++++++- object-file.c | 58 ++++++++++++++++++--------------------------------- streaming.c | 3 ++- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/cache.h b/cache.h index 35f254dae4a..d7189aed8fc 100644 --- a/cache.h +++ b/cache.h @@ -1319,7 +1319,22 @@ char *xdg_cache_home(const char *filename); int git_open_cloexec(const char *name, int flags); #define git_open(name) git_open_cloexec(name, O_RDONLY) -int unpack_loose_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz); + +/** + * unpack_loose_header() initializes the data stream needed to unpack + * a loose object header. + * + * Returns 0 on success. Returns negative values on error. + * + * It will only parse up to MAX_HEADER_LEN bytes unless an optional + * "hdrbuf" argument is non-NULL. This is intended for use with + * OBJECT_INFO_ALLOW_UNKNOWN_TYPE to extract the bad type for (error) + * reporting. The full header will be extracted to "hdrbuf" for use + * with parse_loose_header(). + */ +int unpack_loose_header(git_zstream *stream, unsigned char *map, + unsigned long mapsize, void *buffer, + unsigned long bufsiz, struct strbuf *hdrbuf); struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi, unsigned int flags); diff --git a/object-file.c b/object-file.c index 6b91c4edcf6..1327872cbf4 100644 --- a/object-file.c +++ b/object-file.c @@ -1255,11 +1255,12 @@ void *map_loose_object(struct repository *r, return map_loose_object_1(r, NULL, oid, size); } -static int unpack_loose_short_header(git_zstream *stream, - unsigned char *map, unsigned long mapsize, - void *buffer, unsigned long bufsiz) +int unpack_loose_header(git_zstream *stream, + unsigned char *map, unsigned long mapsize, + void *buffer, unsigned long bufsiz, + struct strbuf *header) { - int ret; + int status; /* Get the data stream */ memset(stream, 0, sizeof(*stream)); @@ -1270,35 +1271,8 @@ static int unpack_loose_short_header(git_zstream *stream, git_inflate_init(stream); obj_read_unlock(); - ret = git_inflate(stream, 0); + status = git_inflate(stream, 0); obj_read_lock(); - - return ret; -} - -int unpack_loose_header(git_zstream *stream, - unsigned char *map, unsigned long mapsize, - void *buffer, unsigned long bufsiz) -{ - int status = unpack_loose_short_header(stream, map, mapsize, - buffer, bufsiz); - - if (status < Z_OK) - return -1; - - /* Make sure we have the terminating NUL */ - if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) - return -1; - return 0; -} - -static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map, - unsigned long mapsize, void *buffer, - unsigned long bufsiz, struct strbuf *header) -{ - int status; - - status = unpack_loose_short_header(stream, map, mapsize, buffer, bufsiz); if (status < Z_OK) return -1; @@ -1308,6 +1282,14 @@ static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) return 0; + /* + * We have a header longer than MAX_HEADER_LEN. The "header" + * here is only non-NULL when we run "cat-file + * --allow-unknown-type". + */ + if (!header) + return -1; + /* * buffer[0..bufsiz] was not large enough. Copy the partial * result out to header, and then append the result of further @@ -1457,6 +1439,7 @@ static int loose_object_info(struct repository *r, char hdr[MAX_HEADER_LEN]; struct strbuf hdrbuf = STRBUF_INIT; unsigned long size_scratch; + int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE; if (oi->delta_base_oid) oidclr(oi->delta_base_oid); @@ -1490,11 +1473,9 @@ static int loose_object_info(struct repository *r, if (oi->disk_sizep) *oi->disk_sizep = mapsize; - if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) { - if (unpack_loose_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0) - status = error(_("unable to unpack %s header with --allow-unknown-type"), - oid_to_hex(oid)); - } else if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) + + if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), + allow_unknown ? &hdrbuf : NULL) < 0) status = error(_("unable to unpack %s header"), oid_to_hex(oid)); if (status < 0) @@ -2602,7 +2583,8 @@ int read_loose_object(const char *path, goto out; } - if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) { + if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), + NULL) < 0) { error(_("unable to unpack header of %s"), path); goto out; } diff --git a/streaming.c b/streaming.c index 8beac62cbb7..cb3c3cf6ff6 100644 --- a/streaming.c +++ b/streaming.c @@ -233,7 +233,8 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, st->u.loose.mapped, st->u.loose.mapsize, st->u.loose.hdr, - sizeof(st->u.loose.hdr)) < 0) || + sizeof(st->u.loose.hdr), + NULL) < 0) || (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0)) { git_inflate_end(&st->z); munmap(st->u.loose.mapped, st->u.loose.mapsize); From patchwork Tue Sep 28 02:18:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521485 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 428F4C4332F for ; Tue, 28 Sep 2021 02:19:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D8D761247 for ; Tue, 28 Sep 2021 02:19:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238661AbhI1CVA (ORCPT ); Mon, 27 Sep 2021 22:21:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238652AbhI1CUz (ORCPT ); Mon, 27 Sep 2021 22:20:55 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8B10C061775 for ; Mon, 27 Sep 2021 19:19:11 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id v127so2127979wme.5 for ; Mon, 27 Sep 2021 19:19:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NfUV2yquG+AqMyBAjxwbha1uQU09bZqOYJms+skUKOE=; b=OYko0QfrsCvr92Yp1tMwqtUcXRM+1finsYoDdATkm4vQLYOCFlYOt3SE69/eSvfjTW 7aOcmydzHNtlKem3WSTuCakM7N95ZFFezZEol+ahsF6wNf9SzRiJInnqXzJgTDDKPFvB WYvFZlD1Pgiqb53ehCdwMfWKRmbDxPWnJGmpIctC6J5yTOkJk247r9Hxg0IAkbEIUOMa 6i4R1rEPO33bI3OZ2Dyx4dhfvj9l1LMj+pJSa9fb7/r1WQVzDr8tPDtt1FswaRDy6D05 UHv3sCb8Fs4kvT6eBQQDMr9IBFc8RcKsP2UkOEsC/steV5KH0EFA1dY9ZPywKvfUajLg H6KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NfUV2yquG+AqMyBAjxwbha1uQU09bZqOYJms+skUKOE=; b=bYYt4+b3i/T6tj1DimgGVWKwG6GA+oBbW8YEKhAIwEu7ra16bqQ/QmIZpnY1M4GB6P 6+vquQGskEmnmJbe4Xulq6kBewDcUMHcTmYN7LjCXEXh6S6Nu3dBEu3ehktZrHSK6SVQ jYGx0NEgUdfobAfuYpLUnEjefaHe6iAWkXmIS1ffvBXsWfad4sWc7VxUbPeduyJ9BCR+ UGaQaUxpjnGiS6wXA+UhGe5p77rfOTMjETuLoGHMyaN8WPfSqd84Um3SaX11t14G0Qmm 1f8Qfynws1gaM1SUinPdeXe/7h4+P3B56NOu070ATyCvyEDnnEDUyGTfqxPk26vsWZ5I uCXw== X-Gm-Message-State: AOAM531yLgx+afDV+rxF4d5SIG1acElhKHDzefHQoS5L+i2lSG76mivF q+3HnPs/bwLVBl4no4A/9+f3l9J6VK4qGw== X-Google-Smtp-Source: ABdhPJzo3BVes4+FpjVUUECksByzAE1aOM64/bSzBSggAM7iYSHvszzBY5/rZ2X8+dE10YgvvlIs6A== X-Received: by 2002:a05:600c:354a:: with SMTP id i10mr2140351wmq.77.1632795550259; Mon, 27 Sep 2021 19:19:10 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:09 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 13/17] object-file.c: use "enum" return type for unpack_loose_header() Date: Tue, 28 Sep 2021 04:18:54 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org In a preceding commit we changed and documented unpack_loose_header() from its previous behavior of returning any negative value or zero, to only -1 or 0. Let's add an "enum unpack_loose_header_result" type and use it for these return values, and have the compiler assert that we're exhaustively covering all of them. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 19 +++++++++++++++---- object-file.c | 34 +++++++++++++++++++++------------- streaming.c | 23 +++++++++++++---------- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/cache.h b/cache.h index d7189aed8fc..7239e20a625 100644 --- a/cache.h +++ b/cache.h @@ -1324,7 +1324,10 @@ int git_open_cloexec(const char *name, int flags); * unpack_loose_header() initializes the data stream needed to unpack * a loose object header. * - * Returns 0 on success. Returns negative values on error. + * Returns: + * + * - ULHR_OK on success + * - ULHR_BAD on error * * It will only parse up to MAX_HEADER_LEN bytes unless an optional * "hdrbuf" argument is non-NULL. This is intended for use with @@ -1332,9 +1335,17 @@ int git_open_cloexec(const char *name, int flags); * reporting. The full header will be extracted to "hdrbuf" for use * with parse_loose_header(). */ -int unpack_loose_header(git_zstream *stream, unsigned char *map, - unsigned long mapsize, void *buffer, - unsigned long bufsiz, struct strbuf *hdrbuf); +enum unpack_loose_header_result { + ULHR_OK, + ULHR_BAD, +}; +enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, + unsigned char *map, + unsigned long mapsize, + void *buffer, + unsigned long bufsiz, + struct strbuf *hdrbuf); + struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi, unsigned int flags); diff --git a/object-file.c b/object-file.c index 1327872cbf4..e0f508415dd 100644 --- a/object-file.c +++ b/object-file.c @@ -1255,10 +1255,12 @@ void *map_loose_object(struct repository *r, return map_loose_object_1(r, NULL, oid, size); } -int unpack_loose_header(git_zstream *stream, - unsigned char *map, unsigned long mapsize, - void *buffer, unsigned long bufsiz, - struct strbuf *header) +enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, + unsigned char *map, + unsigned long mapsize, + void *buffer, + unsigned long bufsiz, + struct strbuf *header) { int status; @@ -1274,13 +1276,13 @@ int unpack_loose_header(git_zstream *stream, status = git_inflate(stream, 0); obj_read_lock(); if (status < Z_OK) - return -1; + return ULHR_BAD; /* * Check if entire header is unpacked in the first iteration. */ if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer)) - return 0; + return ULHR_OK; /* * We have a header longer than MAX_HEADER_LEN. The "header" @@ -1288,7 +1290,7 @@ int unpack_loose_header(git_zstream *stream, * --allow-unknown-type". */ if (!header) - return -1; + return ULHR_BAD; /* * buffer[0..bufsiz] was not large enough. Copy the partial @@ -1309,7 +1311,7 @@ int unpack_loose_header(git_zstream *stream, stream->next_out = buffer; stream->avail_out = bufsiz; } while (status != Z_STREAM_END); - return -1; + return ULHR_BAD; } static void *unpack_loose_rest(git_zstream *stream, @@ -1474,13 +1476,19 @@ static int loose_object_info(struct repository *r, if (oi->disk_sizep) *oi->disk_sizep = mapsize; - if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), - allow_unknown ? &hdrbuf : NULL) < 0) + switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), + allow_unknown ? &hdrbuf : NULL)) { + case ULHR_OK: + break; + case ULHR_BAD: status = error(_("unable to unpack %s header"), oid_to_hex(oid)); - if (status < 0) - ; /* Do nothing */ - else if (hdrbuf.len) { + break; + } + + if (status < 0) { + /* Do nothing */ + } else if (hdrbuf.len) { if ((status = parse_loose_header(hdrbuf.buf, oi, flags)) < 0) status = error(_("unable to parse %s header with --allow-unknown-type"), oid_to_hex(oid)); diff --git a/streaming.c b/streaming.c index cb3c3cf6ff6..6df0247a4cb 100644 --- a/streaming.c +++ b/streaming.c @@ -229,17 +229,16 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize); if (!st->u.loose.mapped) return -1; - if ((unpack_loose_header(&st->z, - st->u.loose.mapped, - st->u.loose.mapsize, - st->u.loose.hdr, - sizeof(st->u.loose.hdr), - NULL) < 0) || - (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0)) { - git_inflate_end(&st->z); - munmap(st->u.loose.mapped, st->u.loose.mapsize); - return -1; + switch (unpack_loose_header(&st->z, st->u.loose.mapped, + st->u.loose.mapsize, st->u.loose.hdr, + sizeof(st->u.loose.hdr), NULL)) { + case ULHR_OK: + break; + case ULHR_BAD: + goto error; } + if (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0) + goto error; st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1; st->u.loose.hdr_avail = st->z.total_out; @@ -248,6 +247,10 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, st->read = read_istream_loose; return 0; +error: + git_inflate_end(&st->z); + munmap(st->u.loose.mapped, st->u.loose.mapsize); + return -1; } From patchwork Tue Sep 28 02:18:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521483 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F0FBC433F5 for ; Tue, 28 Sep 2021 02:19:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 870FF61247 for ; Tue, 28 Sep 2021 02:19:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238623AbhI1CVA (ORCPT ); Mon, 27 Sep 2021 22:21:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238655AbhI1CUz (ORCPT ); Mon, 27 Sep 2021 22:20:55 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1A02C061778 for ; Mon, 27 Sep 2021 19:19:12 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id q127-20020a1ca785000000b0030cb71ea4d1so1501141wme.1 for ; Mon, 27 Sep 2021 19:19:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yTfkA9cJmOFeDByXesQFa7t1cpnzmZyfmbqkEDL/CnU=; b=nT8xlLevBqmdGN9gytccI7hCqq799fJsnlstXJvVvxIcRbqMlsxQUq6l7oS7959NbF PFq08N/65fqraGZRu6C6l+timJBncubRFSzxnYzYPlLdMuDeILKcjmq5YM5Dhha625MA ltaKz+HwbXBMICXhl4zqXRJyevOwQZIU+doIj7EOdeEwrwQpMExMXwh+XwhfCZMDOKZG PAqufLRqYUGwEBDUvcUTVh8464a/IsyhAoeUm3eyW3xtvkxtv7SxiwIcpT/nofh+GZiP HfDutyuteNfmoUfvPPo+/ud/YEP8BFJBG6ZDx6YQkrunc0IF3N/+4Gu8IU3nfWwGpLUD Ideg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yTfkA9cJmOFeDByXesQFa7t1cpnzmZyfmbqkEDL/CnU=; b=K/mw3mtRDRJHXxoHHaTRDidt2e9vr1F8SX34EuFKEnlj9jafMsyO1NHGpxyQ1cyXRG kbl4sLKnfGA0xYHhyPVJzsgI0AnIhkofLuynpTKRk4UzGPd89yg4sxaBjbwXvAUm21qn MuxlrZnMtEFPDcCdmH+kTRPtEgkClNLvFL7Xn+ZWyKjrxiGiz9OE/EI1PeviDB2vFZva A98eH30F0OR1AX7/RHJ72VQStJ3E7NYaRaGkoxG4KnLyEP5i6odBLg1w652LQIRE4GZL 2UcFCjd4dmt4d5Jh4N1ym+EXRlW+vgm5efxHjbfZ9sRLpl+LNM/0ftEBhCE00xWnlcr9 DQig== X-Gm-Message-State: AOAM533cFoUKZRJ9H8k9fgf4dL8CJeEQPHNzR1QyGD9/jIDliUP24ueA Glsxm23g+yO5on0cYdvPcP5MF8dcKACSsg== X-Google-Smtp-Source: ABdhPJwMxjY/zNW2HBL9WJM2xwcEwgqdrJC9WZY+eNFEHJOyiVMxYYkQSvGs44Li7LJU8/KnFjOPkQ== X-Received: by 2002:a05:600c:4f54:: with SMTP id m20mr2292150wmq.96.1632795551071; Mon, 27 Sep 2021 19:19:11 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:10 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 14/17] object-file.c: return ULHR_TOO_LONG on "header too long" Date: Tue, 28 Sep 2021 04:18:55 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Split up the return code for "header too long" from the generic negative return value unpack_loose_header() returns, and report via error() if we exceed MAX_HEADER_LEN. As a test added earlier in this series in t1006-cat-file.sh shows we'll correctly emit zlib errors from zlib.c already in this case, so we have no need to carry those return codes further down the stack. Let's instead just return ULHR_TOO_LONG saying we ran into the MAX_HEADER_LEN limit, or other negative values for "unable to unpack header". Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 5 ++++- object-file.c | 8 ++++++-- streaming.c | 1 + t/t1006-cat-file.sh | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cache.h b/cache.h index 7239e20a625..8e05392fda8 100644 --- a/cache.h +++ b/cache.h @@ -1328,16 +1328,19 @@ int git_open_cloexec(const char *name, int flags); * * - ULHR_OK on success * - ULHR_BAD on error + * - ULHR_TOO_LONG if the header was too long * * It will only parse up to MAX_HEADER_LEN bytes unless an optional * "hdrbuf" argument is non-NULL. This is intended for use with * OBJECT_INFO_ALLOW_UNKNOWN_TYPE to extract the bad type for (error) * reporting. The full header will be extracted to "hdrbuf" for use - * with parse_loose_header(). + * with parse_loose_header(), ULHR_TOO_LONG will still be returned + * from this function to indicate that the header was too long. */ enum unpack_loose_header_result { ULHR_OK, ULHR_BAD, + ULHR_TOO_LONG, }; enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, unsigned char *map, diff --git a/object-file.c b/object-file.c index e0f508415dd..3589c5a2e33 100644 --- a/object-file.c +++ b/object-file.c @@ -1290,7 +1290,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, * --allow-unknown-type". */ if (!header) - return ULHR_BAD; + return ULHR_TOO_LONG; /* * buffer[0..bufsiz] was not large enough. Copy the partial @@ -1311,7 +1311,7 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, stream->next_out = buffer; stream->avail_out = bufsiz; } while (status != Z_STREAM_END); - return ULHR_BAD; + return ULHR_TOO_LONG; } static void *unpack_loose_rest(git_zstream *stream, @@ -1484,6 +1484,10 @@ static int loose_object_info(struct repository *r, status = error(_("unable to unpack %s header"), oid_to_hex(oid)); break; + case ULHR_TOO_LONG: + status = error(_("header for %s too long, exceeds %d bytes"), + oid_to_hex(oid), MAX_HEADER_LEN); + break; } if (status < 0) { diff --git a/streaming.c b/streaming.c index 6df0247a4cb..bd89c50e7b3 100644 --- a/streaming.c +++ b/streaming.c @@ -235,6 +235,7 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, case ULHR_OK: break; case ULHR_BAD: + case ULHR_TOO_LONG: goto error; } if (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0) diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 5b16c69c286..a5e7401af8b 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -356,12 +356,12 @@ do if test "$arg2" = "-p" then cat >expect <<-EOF - error: unable to unpack $bogus_long_sha1 header + error: header for $bogus_long_sha1 too long, exceeds 32 bytes fatal: Not a valid object name $bogus_long_sha1 EOF else cat >expect <<-EOF - error: unable to unpack $bogus_long_sha1 header + error: header for $bogus_long_sha1 too long, exceeds 32 bytes fatal: git cat-file: could not get object info EOF fi && From patchwork Tue Sep 28 02:18:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521487 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19B4FC433F5 for ; Tue, 28 Sep 2021 02:19:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 02D0961247 for ; Tue, 28 Sep 2021 02:19:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238665AbhI1CVB (ORCPT ); Mon, 27 Sep 2021 22:21:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238657AbhI1CU4 (ORCPT ); Mon, 27 Sep 2021 22:20:56 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAFB5C06177A for ; Mon, 27 Sep 2021 19:19:13 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id t8so55287858wrq.4 for ; Mon, 27 Sep 2021 19:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qpeBDOTlXCZ9NStOSfP4z4EuvUo4a2+iq9PlGEXAQ3A=; b=KVCCfkwvkjKi/Qt2V8PUjVO+Xo9gfFBZ5N7/0ZtrOKhwxJJ2Ram5eFTljsp5PXGe16 4u9BrBBDBFG6mJDRXVParaJAS21kjujY+coxH0Bc9kOBKBSdqy7o2/8BOiVdLsxrAuti sdrVfOXW5TWSONP67tMMPc9+RZyax9+4TrMYL4DB7sB1kRQbPN14fBNFtLNBMafwuotA /i+xAiRYFY/kaD2OcmU9Zq11fe+LVcpfB1CqSAD6HvWb8Hvad38Jz2If6oFrPBAeLJoE kSxHVnmIZJxnivTywTr/qa11Uem85E3qtPvXmXkzvBb2c74DJ5lDFOceO07owvu6P+gE BjIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qpeBDOTlXCZ9NStOSfP4z4EuvUo4a2+iq9PlGEXAQ3A=; b=pt5Id/axgWlEOcdZSfUmwP2pdrUYAYp1yFaWEzPPj6nHHv2ySkZZ8n7bRKIig+qdcO BPO22zxh/Ip77BYbRZHX0XnLZhcfzkLwol7fa/m67l9tBRF0F4KY6OmQbDM2Cz3LU119 4A2+wmVsfGhDmHnB3aE+vqtOkLfSUQr5s3hp5HK4d/aP+BeNC8V4crfD0qoxFj9HEabd CTM0sjWYBPH7PdPTYe1lrlC45BEwCCbScrHh4ANhPLwkUXZbtg914SFY+yHG+lqk1Nze J7z6x3fSOxUaM057/Q0G6lVEjduAGDyMws572Tdf8VANLiMz17GwtVyDxY1ik52tIz5K iq1w== X-Gm-Message-State: AOAM533HapqhJbzfOUxFOnnpjjy95Sbjw15yeXj1E2kN/5bVXEtvYAbo sDCRLA3iVSNHV6ZmwAMIyjDohqeIJ115vg== X-Google-Smtp-Source: ABdhPJxFpXwXFkBPJVSUS4D2M1puVlJcnulx/ggv+3D+ZF4Qloj8VCU69WEK3nM/WBD6WV2YgSS6rQ== X-Received: by 2002:a5d:4579:: with SMTP id a25mr3617993wrc.222.1632795552104; Mon, 27 Sep 2021 19:19:12 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:11 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 15/17] object-file.c: stop dying in parse_loose_header() Date: Tue, 28 Sep 2021 04:18:56 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Make parse_loose_header() return error codes and data instead of invoking die() by itself. For now we'll move the relevant die() call to loose_object_info() and read_loose_object() to keep this change smaller. In a subsequent commit we'll make read_loose_object() return an error code instead of dying. We should also address the "allow_unknown" case (should be moved to builtin/cat-file.c), but for now I'll be leaving it. For making parse_loose_header() not die() change its prototype to accept a "struct object_info *" instead of the "unsigned long *sizep" it accepted before. Its callers can now check the populated populated "oi->typep". Because of this we don't need to pass in the "unsigned int flags" which we used for OBJECT_INFO_ALLOW_UNKNOWN_TYPE, we can instead do that check in loose_object_info(). This also refactors some confusing control flow around the "status" variable. In some cases we set it to the return value of "error()", i.e. -1, and later checked if "status < 0" was true. Since 93cff9a978e (sha1_loose_object_info: return error for corrupted objects, 2017-04-01) the return value of loose_object_info() (then named sha1_loose_object_info()) had been a "status" variable that be any negative value, as we were expecting to return the "enum object_type". The only negative type happens to be OBJ_BAD, but the code still assumed that more might be added. This was then used later in e.g. c84a1f3ed4d (sha1_file: refactor read_object, 2017-06-21). Now that parse_loose_header() will return 0 on success instead of the type (which it'll stick into the "struct object_info") we don't need to conflate these two cases in its callers. Since parse_loose_header() doesn't need to return an arbitrary "status" we only need to treat its "ret < 0" specially, but can idiomatically overwrite it with our own error() return. This along with having made unpack_loose_header() return an "enum unpack_loose_header_result" in an earlier commit means that we can move the previously nested if/else cases mostly into the "ULHR_OK" branch of the "switch" statement. We should be less silent if we reach that "status = -1" branch, which happens if we've got trailing garbage in loose objects, see f6371f92104 (sha1_file: add read_loose_object() function, 2017-01-13) for a better way to handle it. For now let's punt on it, a subsequent commit will address that edge case. Signed-off-by: Ævar Arnfjörð Bjarmason --- cache.h | 11 +++++++-- object-file.c | 67 +++++++++++++++++++++++++-------------------------- streaming.c | 3 ++- 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/cache.h b/cache.h index 8e05392fda8..6c5f00c82d5 100644 --- a/cache.h +++ b/cache.h @@ -1349,9 +1349,16 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream, unsigned long bufsiz, struct strbuf *hdrbuf); +/** + * parse_loose_header() parses the starting " \0" of an + * object. If it doesn't follow that format -1 is returned. To check + * the validity of the populate the "typep" in the "struct + * object_info". It will be OBJ_BAD if the object type is unknown. The + * parsed can be retrieved via "oi->sizep", and from there + * passed to unpack_loose_rest(). + */ struct object_info; -int parse_loose_header(const char *hdr, struct object_info *oi, - unsigned int flags); +int parse_loose_header(const char *hdr, struct object_info *oi); int check_object_signature(struct repository *r, const struct object_id *oid, void *buf, unsigned long size, const char *type); diff --git a/object-file.c b/object-file.c index 3589c5a2e33..a70669700d0 100644 --- a/object-file.c +++ b/object-file.c @@ -1369,8 +1369,7 @@ static void *unpack_loose_rest(git_zstream *stream, * too permissive for what we want to check. So do an anal * object header parse by hand. */ -int parse_loose_header(const char *hdr, struct object_info *oi, - unsigned int flags) +int parse_loose_header(const char *hdr, struct object_info *oi) { const char *type_buf = hdr; unsigned long size; @@ -1392,15 +1391,6 @@ int parse_loose_header(const char *hdr, struct object_info *oi, type = type_from_string_gently(type_buf, type_len, 1); if (oi->type_name) strbuf_add(oi->type_name, type_buf, type_len); - /* - * Set type to 0 if its an unknown object and - * we're obtaining the type using '--allow-unknown-type' - * option. - */ - if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0)) - type = 0; - else if (type < 0) - die(_("invalid object type")); if (oi->typep) *oi->typep = type; @@ -1427,7 +1417,14 @@ int parse_loose_header(const char *hdr, struct object_info *oi, /* * The length must be followed by a zero byte */ - return *hdr ? -1 : type; + if (*hdr) + return -1; + + /* + * The format is valid, but the type may still be bogus. The + * Caller needs to check its oi->typep. + */ + return 0; } static int loose_object_info(struct repository *r, @@ -1441,6 +1438,7 @@ static int loose_object_info(struct repository *r, char hdr[MAX_HEADER_LEN]; struct strbuf hdrbuf = STRBUF_INIT; unsigned long size_scratch; + enum object_type type_scratch; int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE; if (oi->delta_base_oid) @@ -1472,6 +1470,8 @@ static int loose_object_info(struct repository *r, if (!oi->sizep) oi->sizep = &size_scratch; + if (!oi->typep) + oi->typep = &type_scratch; if (oi->disk_sizep) *oi->disk_sizep = mapsize; @@ -1479,6 +1479,18 @@ static int loose_object_info(struct repository *r, switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr), allow_unknown ? &hdrbuf : NULL)) { case ULHR_OK: + if (parse_loose_header(hdrbuf.len ? hdrbuf.buf : hdr, oi) < 0) + status = error(_("unable to parse %s header"), oid_to_hex(oid)); + else if (!allow_unknown && *oi->typep < 0) + die(_("invalid object type")); + + if (!oi->contentp) + break; + *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); + if (*oi->contentp) + goto cleanup; + + status = -1; break; case ULHR_BAD: status = error(_("unable to unpack %s header"), @@ -1490,31 +1502,16 @@ static int loose_object_info(struct repository *r, break; } - if (status < 0) { - /* Do nothing */ - } else if (hdrbuf.len) { - if ((status = parse_loose_header(hdrbuf.buf, oi, flags)) < 0) - status = error(_("unable to parse %s header with --allow-unknown-type"), - oid_to_hex(oid)); - } else if ((status = parse_loose_header(hdr, oi, flags)) < 0) - status = error(_("unable to parse %s header"), oid_to_hex(oid)); - - if (status >= 0 && oi->contentp) { - *oi->contentp = unpack_loose_rest(&stream, hdr, - *oi->sizep, oid); - if (!*oi->contentp) { - git_inflate_end(&stream); - status = -1; - } - } else - git_inflate_end(&stream); - + git_inflate_end(&stream); +cleanup: munmap(map, mapsize); if (oi->sizep == &size_scratch) oi->sizep = NULL; strbuf_release(&hdrbuf); + if (oi->typep == &type_scratch) + oi->typep = NULL; oi->whence = OI_LOOSE; - return (status < 0) ? status : 0; + return status; } int obj_read_use_lock = 0; @@ -2585,6 +2582,7 @@ int read_loose_object(const char *path, git_zstream stream; char hdr[MAX_HEADER_LEN]; struct object_info oi = OBJECT_INFO_INIT; + oi.typep = type; oi.sizep = size; *contents = NULL; @@ -2601,12 +2599,13 @@ int read_loose_object(const char *path, goto out; } - *type = parse_loose_header(hdr, &oi, 0); - if (*type < 0) { + if (parse_loose_header(hdr, &oi) < 0) { error(_("unable to parse header of %s"), path); git_inflate_end(&stream); goto out; } + if (*type < 0) + die(_("invalid object type")); if (*type == OBJ_BLOB && *size > big_file_threshold) { if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0) diff --git a/streaming.c b/streaming.c index bd89c50e7b3..fe54665d86e 100644 --- a/streaming.c +++ b/streaming.c @@ -225,6 +225,7 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, { struct object_info oi = OBJECT_INFO_INIT; oi.sizep = &st->size; + oi.typep = type; st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize); if (!st->u.loose.mapped) @@ -238,7 +239,7 @@ static int open_istream_loose(struct git_istream *st, struct repository *r, case ULHR_TOO_LONG: goto error; } - if (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0) + if (parse_loose_header(st->u.loose.hdr, &oi) < 0 || *type < 0) goto error; st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1; From patchwork Tue Sep 28 02:18:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521489 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4FBC5C433FE for ; Tue, 28 Sep 2021 02:19:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3120E61266 for ; Tue, 28 Sep 2021 02:19:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238680AbhI1CVD (ORCPT ); Mon, 27 Sep 2021 22:21:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238627AbhI1CU4 (ORCPT ); Mon, 27 Sep 2021 22:20:56 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 951DFC06177F for ; Mon, 27 Sep 2021 19:19:14 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id q127-20020a1ca785000000b0030cb71ea4d1so1501175wme.1 for ; Mon, 27 Sep 2021 19:19:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8UGB+LJv0lA4yyi5RP+Vv9G3QlWK9EHxD2pmw37V0oE=; b=XX6bNeqbpcDD8YlhmU7s9lthSpQV8aZ26i9BjxbEDRSAfLRX6CvMEtzH7UgzpWsFYS 0txsj2XuHEIANnFKUP6ZbUqbmasAbKeRXcuvKTGmBNEpEVTljsYWrXGkIBhqTgh65ex6 zJSb4OMICuPC+1M6XgJKvPRsTyIutzNv5slhscfRmwnt3rRgGQC6o5oZAYsTRMvoM1Vn Qnu4FOMuMHq4v7SwykGAS5t9NP5i04tLe548VJQrZcQpAXTfOIknoA4oMyHOEs4exS6c PNMaZcpvmVFHRa3Kur0GIf8ZOY7G1st7UU+l/S35tM0GHPxnUxUEy4PaPB5iCs2bHzJj sV6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8UGB+LJv0lA4yyi5RP+Vv9G3QlWK9EHxD2pmw37V0oE=; b=KUnRFRv2PuL+ZsO4DsmdeDGeaIlEtBlenLvujpGG0S8G+EPQtnPMa57cv++7Ljm0Y5 /En4bDw+bEox05uzwvSfwObToQUMHjRnnyuToRdvjZoV3MbvLDIn7l4VXVFReSI2ExWv RIRtgQ1tb6BH3ifEWl+cktuyC+GOwg8ToRQDUXo7woHvP4mLOQSnAjD7IC9/l5QlUiQT q016Jex6ZYIvk1t3fowZZpoVCKKk+kXI2C12ccBVoPXtntdAjOHT+tvpKIH9H874dg90 hu7IMQ7gqsG6nTMEVaWS/9yUDs+KBX2oF0UK2xe3h4h4DQvWONfMhk7jUms4TYsNbux7 9Qdg== X-Gm-Message-State: AOAM531VGkLu1x/STVBIfg7SOAtSJfLKUr/duGaoAR4bM5XnJIFbq2/D KgkutQfzYYYbPIvNWkuA+eOz4tNIpKbd2A== X-Google-Smtp-Source: ABdhPJzfO7mCv0P5n5qT4Un75+NCPFWGjtmLtSGFY8TQyZDtg4x3XoaFMZN32xclo1DnmbyyETCryA== X-Received: by 2002:a1c:95:: with SMTP id 143mr2232282wma.29.1632795552910; Mon, 27 Sep 2021 19:19:12 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:12 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 16/17] fsck: don't hard die on invalid object types Date: Tue, 28 Sep 2021 04:18:57 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Change the error fsck emits on invalid object types, such as: $ git hash-object --stdin -w -t garbage --literally From the very ungraceful error of: $ git fsck fatal: invalid object type $ To: $ git fsck error: : object is of unknown type 'garbage': [ other fsck output ] We'll still exit with non-zero, but now we'll finish the rest of the traversal. The tests that's being added here asserts that we'll still complain about other fsck issues (e.g. an unrelated dangling blob). To do this we need to pass down the "OBJECT_INFO_ALLOW_UNKNOWN_TYPE" flag from read_loose_object() through to parse_loose_header(). Since the read_loose_object() function is only used in builtin/fsck.c we can simply change it to accept a "struct object_info" (which contains the OBJECT_INFO_ALLOW_UNKNOWN_TYPE in its flags). See f6371f92104 (sha1_file: add read_loose_object() function, 2017-01-13) for the introduction of read_loose_object(). Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fsck.c | 17 ++++++++++++++--- object-file.c | 18 ++++++------------ object-store.h | 6 +++--- t/t1450-fsck.sh | 17 +++++++++-------- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/builtin/fsck.c b/builtin/fsck.c index b42b6fe21f7..3b046820750 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -600,11 +600,22 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data) unsigned long size; void *contents; int eaten; + struct strbuf sb = STRBUF_INIT; + struct object_info oi = OBJECT_INFO_INIT; + int err = 0; - if (read_loose_object(path, oid, &type, &size, &contents) < 0) { + oi.type_name = &sb; + oi.sizep = &size; + oi.typep = &type; + + if (read_loose_object(path, oid, &contents, &oi) < 0) + err = error(_("%s: object corrupt or missing: %s"), + oid_to_hex(oid), path); + if (type < 0) + err = error(_("%s: object is of unknown type '%s': %s"), + oid_to_hex(oid), sb.buf, path); + if (err) { errors_found |= ERROR_OBJECT; - error(_("%s: object corrupt or missing: %s"), - oid_to_hex(oid), path); return 0; /* keep checking other objects */ } diff --git a/object-file.c b/object-file.c index a70669700d0..fe95285f405 100644 --- a/object-file.c +++ b/object-file.c @@ -2572,18 +2572,15 @@ static int check_stream_oid(git_zstream *stream, int read_loose_object(const char *path, const struct object_id *expected_oid, - enum object_type *type, - unsigned long *size, - void **contents) + void **contents, + struct object_info *oi) { int ret = -1; void *map = NULL; unsigned long mapsize; git_zstream stream; char hdr[MAX_HEADER_LEN]; - struct object_info oi = OBJECT_INFO_INIT; - oi.typep = type; - oi.sizep = size; + unsigned long *size = oi->sizep; *contents = NULL; @@ -2599,15 +2596,13 @@ int read_loose_object(const char *path, goto out; } - if (parse_loose_header(hdr, &oi) < 0) { + if (parse_loose_header(hdr, oi) < 0) { error(_("unable to parse header of %s"), path); git_inflate_end(&stream); goto out; } - if (*type < 0) - die(_("invalid object type")); - if (*type == OBJ_BLOB && *size > big_file_threshold) { + if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) { if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0) goto out; } else { @@ -2618,8 +2613,7 @@ int read_loose_object(const char *path, goto out; } if (check_object_signature(the_repository, expected_oid, - *contents, *size, - type_name(*type))) { + *contents, *size, oi->type_name->buf)) { error(_("hash mismatch for %s (expected %s)"), path, oid_to_hex(expected_oid)); free(*contents); diff --git a/object-store.h b/object-store.h index c5130d8baea..c90c41a07f7 100644 --- a/object-store.h +++ b/object-store.h @@ -245,6 +245,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime); /* * Open the loose object at path, check its hash, and return the contents, + * use the "oi" argument to assert things about the object, or e.g. populate its * type, and size. If the object is a blob, then "contents" may return NULL, * to allow streaming of large blobs. * @@ -252,9 +253,8 @@ int force_object_loose(const struct object_id *oid, time_t mtime); */ int read_loose_object(const char *path, const struct object_id *expected_oid, - enum object_type *type, - unsigned long *size, - void **contents); + void **contents, + struct object_info *oi); /* Retry packed storage after checking packed and loose storage */ #define HAS_OBJECT_RECHECK_PACKED 1 diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index bd696d21dba..167c319823a 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -85,11 +85,10 @@ test_expect_success 'object with hash and type mismatch' ' cmt=$(echo bogus | git commit-tree $tree) && git update-ref refs/heads/bogus $cmt && - cat >expect <<-\EOF && - fatal: invalid object type - EOF - test_must_fail git fsck 2>actual && - test_cmp expect actual + + test_must_fail git fsck 2>out && + grep "^error: hash mismatch for " out && + grep "^error: $oid: object is of unknown type '"'"'garbage'"'"'" out ) ' @@ -910,7 +909,7 @@ test_expect_success 'detect corrupt index file in fsck' ' test_i18ngrep "bad index file" errors ' -test_expect_success 'fsck hard errors on an invalid object type' ' +test_expect_success 'fsck error and recovery on invalid object type' ' git init --bare garbage-type && ( cd garbage-type && @@ -922,8 +921,10 @@ test_expect_success 'fsck hard errors on an invalid object type' ' fatal: invalid object type EOF test_must_fail git fsck >out 2>err && - test_cmp err.expect err && - test_must_be_empty out + grep -e "^error" -e "^fatal" err >errors && + test_line_count = 1 errors && + grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err && + grep "dangling blob $empty_blob" out ) ' From patchwork Tue Sep 28 02:18:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= X-Patchwork-Id: 12521491 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D32FC433F5 for ; Tue, 28 Sep 2021 02:19:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2C1DF61262 for ; Tue, 28 Sep 2021 02:19:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238686AbhI1CVE (ORCPT ); Mon, 27 Sep 2021 22:21:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238662AbhI1CU5 (ORCPT ); Mon, 27 Sep 2021 22:20:57 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81F5FC061780 for ; Mon, 27 Sep 2021 19:19:15 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id t18so55268896wrb.0 for ; Mon, 27 Sep 2021 19:19:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BAkEW/0TY5agHuq8SkgE6ErbZVCtFhtl8ELr1pZqVEc=; b=g4aR3gY3rg0ILG6tyXixfZ6c4TR+6j/AjtrYl/G7AVrgL3J0A5x08M3zWCwiL6s44a pL+GeIgfMsR5G5I/hWy4pFhxQDr6O7d9KgzDP1LsnaWZoKcs1Bba633nDLrify03ek1p so4FNLHICGTanOeTWnvce10Pf/YJyNj7XlY7RUpncXF9rmC/WQ2P23Bgq05cT76fR2gp pfJeGkbk2irv/0p/zVt/cmz5I2Q3SkbZw8dDIr/GYbw3r7QNu9/6mDmp/6W4eNBMrVJ4 1f9wefH9ndK6lxZoyQEoqwW8TsJ3UF+Lp9TAlwjraYOir4qldJg6ogesQZR8QbRLbQ5S NkFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BAkEW/0TY5agHuq8SkgE6ErbZVCtFhtl8ELr1pZqVEc=; b=o/TIe3x+WQQG01hIu3aiSUmGbyKY2inInjTE9cNWm+/xhnZ+J4KNwtIeaIjPsgDEk0 jq5T2PmkJW+A09Gv0kaNjJAkkmL+rv3lIqopZlk5O5bN4EMCksYip6viHVYGuBjI5SI4 +CNatfbDdLTAEvHSl7zgGv3LXhRKgIOMPo7ZR2LT/tHJN3as2o7RUULLhXLzeibW9Mi6 jeYC8r0/DpeVrZYw7rJBuDJEFq0oBEs97zkD7hM56+ZFggBg+BHvg3WPRuNocpp8LxRl iZAy6bEjOYoY3m6NZZZRdeRPo6yblYJpiNjI0HPpt59bnAaqmiC2US0YS9mMdYFPQXzS 7i6A== X-Gm-Message-State: AOAM5310exbmVkJItO2zokn8SD5GXgFhedKURDjK2qhbZ8gLF69lP+VY ZC2V/Uz6W3EGAK4tGlwFQmuh4873uv4MLw== X-Google-Smtp-Source: ABdhPJxzmsp24rDCgg9PQDnnx/N4zkDo6KgtBwTTxzjl2iLMEVs/7WJoK1I9Re9HSH+2cgER1rUnTA== X-Received: by 2002:adf:dd8e:: with SMTP id x14mr3630084wrl.260.1632795553833; Mon, 27 Sep 2021 19:19:13 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id c132sm1126987wma.22.2021.09.27.19.19.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 19:19:13 -0700 (PDT) From: =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFzb24=?= To: git@vger.kernel.org Cc: Junio C Hamano , Jeff King , Jonathan Tan , Andrei Rybak , Taylor Blau , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsCBCamFybWFz?= =?utf-8?b?b24=?= Subject: [PATCH v8 17/17] fsck: report invalid object type-path combinations Date: Tue, 28 Sep 2021 04:18:58 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1327.g9926af6cb02 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Improve the error that's emitted in cases where we find a loose object we parse, but which isn't at the location we expect it to be. Before this change we'd prefix the error with a not-a-OID derived from the path at which the object was found, due to an emergent behavior in how we'd end up with an "OID" in these codepaths. Now we'll instead say what object we hashed, and what path it was found at. Before this patch series e.g.: $ git hash-object --stdin -w -t blob >objects/e6/9de29bb2d1d6434b8b29ae775ad8c2e48c5391 $ git fsck error: garbage at end of loose object 'e69d[...]' error: unable to unpack contents of ./objects/e6/9d[...] error: e69d[...]: object corrupt or missing: ./objects/e6/9d[...] There is currently some weird messaging in the edge case when the two are combined, i.e. because we're not explicitly passing along an error state about this specific scenario from check_stream_oid() via read_loose_object() we'll end up printing the null OID if an object is of an unknown type *and* it can't be unpacked by zlib, e.g.: $ git hash-object --stdin -w -t garbage --literally >objects/83/15a83d2acc4c174aed59430f9a9c4ed926440f $ /usr/bin/git fsck fatal: invalid object type $ ~/g/git/git fsck error: garbage at end of loose object '8315a83d2acc4c174aed59430f9a9c4ed926440f' error: unable to unpack contents of ./objects/83/15a83d2acc4c174aed59430f9a9c4ed926440f error: 8315a83d2acc4c174aed59430f9a9c4ed926440f: object corrupt or missing: ./objects/83/15a83d2acc4c174aed59430f9a9c4ed926440f error: 0000000000000000000000000000000000000000: object is of unknown type 'garbage': ./objects/83/15a83d2acc4c174aed59430f9a9c4ed926440f [...] I think it's OK to leave that for future improvements, which would involve enum-ifying more error state as we've done with "enum unpack_loose_header_result" in preceding commits. In these increasingly more obscure cases the worst that can happen is that we'll get slightly nonsensical or inapplicable error messages. There's other such potential edge cases, all of which might produce some confusing messaging, but still be handled correctly as far as passing along errors goes. E.g. if check_object_signature() returns and oideq(real_oid, null_oid()) is true, which could happen if it returns -1 due to the read_istream() call having failed. Signed-off-by: Ævar Arnfjörð Bjarmason --- builtin/fast-export.c | 2 +- builtin/fsck.c | 23 +++++++++++++++-------- builtin/index-pack.c | 2 +- builtin/mktag.c | 3 ++- cache.h | 3 ++- object-file.c | 21 ++++++++++----------- object-store.h | 1 + object.c | 4 ++-- pack-check.c | 3 ++- t/t1006-cat-file.sh | 2 +- t/t1450-fsck.sh | 8 +++++--- 11 files changed, 42 insertions(+), 30 deletions(-) diff --git a/builtin/fast-export.c b/builtin/fast-export.c index 95e8e89e81f..8e2caf72819 100644 --- a/builtin/fast-export.c +++ b/builtin/fast-export.c @@ -312,7 +312,7 @@ static void export_blob(const struct object_id *oid) if (!buf) die("could not read blob %s", oid_to_hex(oid)); if (check_object_signature(the_repository, oid, buf, size, - type_name(type)) < 0) + type_name(type), NULL) < 0) die("oid mismatch in blob %s", oid_to_hex(oid)); object = parse_object_buffer(the_repository, oid, type, size, buf, &eaten); diff --git a/builtin/fsck.c b/builtin/fsck.c index 3b046820750..d925cdbae5c 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -598,23 +598,30 @@ static int fsck_loose(const struct object_id *oid, const char *path, void *data) struct object *obj; enum object_type type; unsigned long size; - void *contents; + unsigned char *contents = NULL; int eaten; struct strbuf sb = STRBUF_INIT; struct object_info oi = OBJECT_INFO_INIT; - int err = 0; + struct object_id real_oid = *null_oid(); + int ret; oi.type_name = &sb; oi.sizep = &size; oi.typep = &type; - if (read_loose_object(path, oid, &contents, &oi) < 0) - err = error(_("%s: object corrupt or missing: %s"), - oid_to_hex(oid), path); + ret = read_loose_object(path, oid, &real_oid, (void **)&contents, &oi); + if (ret < 0) { + if (contents && !oideq(&real_oid, oid)) + error(_("%s: hash-path mismatch, found at: %s"), + oid_to_hex(&real_oid), path); + else + error(_("%s: object corrupt or missing: %s"), + oid_to_hex(oid), path); + } if (type < 0) - err = error(_("%s: object is of unknown type '%s': %s"), - oid_to_hex(oid), sb.buf, path); - if (err) { + ret = error(_("%s: object is of unknown type '%s': %s"), + oid_to_hex(&real_oid), sb.buf, path); + if (ret < 0) { errors_found |= ERROR_OBJECT; return 0; /* keep checking other objects */ } diff --git a/builtin/index-pack.c b/builtin/index-pack.c index 7ce69c087ec..15ae406e6b7 100644 --- a/builtin/index-pack.c +++ b/builtin/index-pack.c @@ -1415,7 +1415,7 @@ static void fix_unresolved_deltas(struct hashfile *f) if (check_object_signature(the_repository, &d->oid, data, size, - type_name(type))) + type_name(type), NULL)) die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); /* diff --git a/builtin/mktag.c b/builtin/mktag.c index dddcccdd368..3b2dbbb37e6 100644 --- a/builtin/mktag.c +++ b/builtin/mktag.c @@ -62,7 +62,8 @@ static int verify_object_in_tag(struct object_id *tagged_oid, int *tagged_type) repl = lookup_replace_object(the_repository, tagged_oid); ret = check_object_signature(the_repository, repl, - buffer, size, type_name(*tagged_type)); + buffer, size, type_name(*tagged_type), + NULL); free(buffer); return ret; diff --git a/cache.h b/cache.h index 6c5f00c82d5..e2a203073ea 100644 --- a/cache.h +++ b/cache.h @@ -1361,7 +1361,8 @@ struct object_info; int parse_loose_header(const char *hdr, struct object_info *oi); int check_object_signature(struct repository *r, const struct object_id *oid, - void *buf, unsigned long size, const char *type); + void *buf, unsigned long size, const char *type, + struct object_id *real_oidp); int finalize_object_file(const char *tmpfile, const char *filename); diff --git a/object-file.c b/object-file.c index fe95285f405..49561e31551 100644 --- a/object-file.c +++ b/object-file.c @@ -1084,9 +1084,11 @@ void *xmmap(void *start, size_t length, * the streaming interface and rehash it to do the same. */ int check_object_signature(struct repository *r, const struct object_id *oid, - void *map, unsigned long size, const char *type) + void *map, unsigned long size, const char *type, + struct object_id *real_oidp) { - struct object_id real_oid; + struct object_id tmp; + struct object_id *real_oid = real_oidp ? real_oidp : &tmp; enum object_type obj_type; struct git_istream *st; git_hash_ctx c; @@ -1094,8 +1096,8 @@ int check_object_signature(struct repository *r, const struct object_id *oid, int hdrlen; if (map) { - hash_object_file(r->hash_algo, map, size, type, &real_oid); - return !oideq(oid, &real_oid) ? -1 : 0; + hash_object_file(r->hash_algo, map, size, type, real_oid); + return !oideq(oid, real_oid) ? -1 : 0; } st = open_istream(r, oid, &obj_type, &size, NULL); @@ -1120,9 +1122,9 @@ int check_object_signature(struct repository *r, const struct object_id *oid, break; r->hash_algo->update_fn(&c, buf, readlen); } - r->hash_algo->final_oid_fn(&real_oid, &c); + r->hash_algo->final_oid_fn(real_oid, &c); close_istream(st); - return !oideq(oid, &real_oid) ? -1 : 0; + return !oideq(oid, real_oid) ? -1 : 0; } int git_open_cloexec(const char *name, int flags) @@ -2572,6 +2574,7 @@ static int check_stream_oid(git_zstream *stream, int read_loose_object(const char *path, const struct object_id *expected_oid, + struct object_id *real_oid, void **contents, struct object_info *oi) { @@ -2582,8 +2585,6 @@ int read_loose_object(const char *path, char hdr[MAX_HEADER_LEN]; unsigned long *size = oi->sizep; - *contents = NULL; - map = map_loose_object_1(the_repository, path, NULL, &mapsize); if (!map) { error_errno(_("unable to mmap %s"), path); @@ -2613,9 +2614,7 @@ int read_loose_object(const char *path, goto out; } if (check_object_signature(the_repository, expected_oid, - *contents, *size, oi->type_name->buf)) { - error(_("hash mismatch for %s (expected %s)"), path, - oid_to_hex(expected_oid)); + *contents, *size, oi->type_name->buf, real_oid)) { free(*contents); goto out; } diff --git a/object-store.h b/object-store.h index c90c41a07f7..17b072e5a19 100644 --- a/object-store.h +++ b/object-store.h @@ -253,6 +253,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime); */ int read_loose_object(const char *path, const struct object_id *expected_oid, + struct object_id *real_oid, void **contents, struct object_info *oi); diff --git a/object.c b/object.c index 4e85955a941..23a24e678a8 100644 --- a/object.c +++ b/object.c @@ -279,7 +279,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) || (!obj && repo_has_object_file(r, oid) && oid_object_info(r, oid, NULL) == OBJ_BLOB)) { - if (check_object_signature(r, repl, NULL, 0, NULL) < 0) { + if (check_object_signature(r, repl, NULL, 0, NULL, NULL) < 0) { error(_("hash mismatch %s"), oid_to_hex(oid)); return NULL; } @@ -290,7 +290,7 @@ struct object *parse_object(struct repository *r, const struct object_id *oid) buffer = repo_read_object_file(r, oid, &type, &size); if (buffer) { if (check_object_signature(r, repl, buffer, size, - type_name(type)) < 0) { + type_name(type), NULL) < 0) { free(buffer); error(_("hash mismatch %s"), oid_to_hex(repl)); return NULL; diff --git a/pack-check.c b/pack-check.c index c8e560d71ab..3f418e3a6af 100644 --- a/pack-check.c +++ b/pack-check.c @@ -142,7 +142,8 @@ static int verify_packfile(struct repository *r, err = error("cannot unpack %s from %s at offset %"PRIuMAX"", oid_to_hex(&oid), p->pack_name, (uintmax_t)entries[i].offset); - else if (check_object_signature(r, &oid, data, size, type_name(type))) + else if (check_object_signature(r, &oid, data, size, + type_name(type), NULL)) err = error("packed %s from %s is corrupt", oid_to_hex(&oid), p->pack_name); else if (fn) { diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index a5e7401af8b..0f52ca9cc82 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -512,7 +512,7 @@ test_expect_success 'cat-file -t and -s on corrupt loose object' ' # Swap the two to corrupt the repository mv -f "$other_path" "$empty_path" && test_must_fail git fsck 2>err.fsck && - grep "hash mismatch" err.fsck && + grep "hash-path mismatch" err.fsck && # confirm that cat-file is reading the new swapped-in # blob... diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh index 167c319823a..eb0e772f098 100755 --- a/t/t1450-fsck.sh +++ b/t/t1450-fsck.sh @@ -54,6 +54,7 @@ test_expect_success 'object with hash mismatch' ' cd hash-mismatch && oid=$(echo blob | git hash-object -w --stdin) && + oldoid=$oid && old=$(test_oid_to_path "$oid") && new=$(dirname $old)/$(test_oid ff_2) && oid="$(dirname $new)$(basename $new)" && @@ -65,7 +66,7 @@ test_expect_success 'object with hash mismatch' ' git update-ref refs/heads/bogus $cmt && test_must_fail git fsck 2>out && - grep "$oid.*corrupt" out + grep "$oldoid: hash-path mismatch, found at: .*$new" out ) ' @@ -75,6 +76,7 @@ test_expect_success 'object with hash and type mismatch' ' cd hash-type-mismatch && oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) && + oldoid=$oid && old=$(test_oid_to_path "$oid") && new=$(dirname $old)/$(test_oid ff_2) && oid="$(dirname $new)$(basename $new)" && @@ -87,8 +89,8 @@ test_expect_success 'object with hash and type mismatch' ' test_must_fail git fsck 2>out && - grep "^error: hash mismatch for " out && - grep "^error: $oid: object is of unknown type '"'"'garbage'"'"'" out + grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out && + grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out ) '