From patchwork Mon Sep 20 19:04:05 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: 12507157 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD990C433EF for ; Tue, 21 Sep 2021 02:58:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8AC3561211 for ; Tue, 21 Sep 2021 02:58:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231628AbhIUC71 (ORCPT ); Mon, 20 Sep 2021 22:59:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239243AbhIUC0b (ORCPT ); Mon, 20 Sep 2021 22:26:31 -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 BAA5CC1E325F for ; Mon, 20 Sep 2021 12:04:28 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id w29so32591775wra.8 for ; Mon, 20 Sep 2021 12:04:28 -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=rpCLv1vqXilM2NXE7LeVOwnEcRPTEblWhjWeEGaBwnU=; b=RRqfAwI1OH4myppROwRC1CAs2ZIsNlxzfcsZclyXKWiQPPp8T2uGMUWfiDmQ7o5Y3X 9XjzQu4M5PJ9Cs54mNCDvSfWUzO9LIsL8hEZlTeHmdcjGMKLJxwfPF0B+4PHd7YJH8ME KClSUJjCTCZyhdPMwxfemUwIIOXpp3SkKKyOuaVc6iL7N9RsvrYJjQh3FknTh79JA1sv tSvk6UaJd/DlmXbBRzY06Jl/Akt8AOPfsEdP/XYJVpmnyU8HG0f46WpX4FIy/kxBHoCT 7fw7f22lcnPJwgS4zS0keyHfTaUlkPObriyw1Cu0YmRmq6QSHh8GylTvjp5JwJDMpfAq +P8A== 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=rpCLv1vqXilM2NXE7LeVOwnEcRPTEblWhjWeEGaBwnU=; b=SsrwvFKgzJCieWkR02dzOPPB0496Gdfx2RHcO+EZREae12ZS579YmGJ/fc4h5rN78l optSqORRdoLSLuXWrIbhAP2T8kT5gyWaBCsX4mz1m8HcunDyuUvQyB0Q0Tpd7reCKIm3 JofG9ZikjkKqABVKsgdc8AQCCYd24M0Sk+h7F6uxOQbWrcywU2sEM1fHrEZnQ2x+mQ3k MTYe6i/LZ32nsNQ4oBhzlmgLTHqT3bgIJzoNs6OMlZASq1qmbgNZv2nkzy2JQ7MyeLS7 TtpzncKDlNvxCIExFxZKsPH4jL6wWY2JQkVX0lnAmuQcPL/6tYkLSsiDtt8a6Evyu7HY 8uqg== X-Gm-Message-State: AOAM5330nxx4lH3nBqIonB2+brM4U7y1gSFNzVNbWmVCdKDe5sq+gIop 3zwZHxqt3QyseSZvMwCt5MtfeIez7oWqqg== X-Google-Smtp-Source: ABdhPJxs7pCigIEU9OY4DImN+AxgWo32EisRAKFnB227nOl444h2Qh9x1h3WXvMburhTz7FvCHN3jA== X-Received: by 2002:a05:600c:4fc7:: with SMTP id o7mr585670wmq.91.1632164667068; Mon, 20 Sep 2021 12:04:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:26 -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 v7 01/17] fsck tests: add test for fsck-ing an unknown type Date: Mon, 20 Sep 2021 21:04:05 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 Mon Sep 20 19:04:06 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: 12507159 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A39EC433FE for ; Tue, 21 Sep 2021 02:58:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2F6606112D for ; Tue, 21 Sep 2021 02:58:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231682AbhIUC73 (ORCPT ); Mon, 20 Sep 2021 22:59:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239258AbhIUC0b (ORCPT ); Mon, 20 Sep 2021 22:26:31 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B42AC1E3262 for ; Mon, 20 Sep 2021 12:04:29 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id i23so32619104wrb.2 for ; Mon, 20 Sep 2021 12:04:29 -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=Pic6YmM0DWsnEf+G2fMJILTfU4jfWOTt7Tj45Xt3BLI=; b=bzH+zQ+PZluL/Jaxqw5FcAxbNAKfdwY9no96UUDvgT+rJ7qcIcU++qsfZNiSsH1o+C qWPP6WH84ABymFxK54bSziPlsm4W1CBFB9Nblyt6MbqRQeGcs4D6mMQIj9xNJvOl1bwG P1XSskEhSJo2VZ5FQFcrMuV3fXFsRfmBPHRC3ygx51Yj/XqxHf+AuWHgKTF8e44eoOAS JPmDqflwkXLLhEiHyO6haEdJzFllYzWPmrUudac5Fs3DQGFBAsemq0FlgNPqNhuVkEtb /cqn5ynaJQgE3ha7pd4TDVuSux6MiZ6WNvYd3yUBfHHQeqUZpbOWjkBsIGv9Aw3U2CXD BABw== 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=Pic6YmM0DWsnEf+G2fMJILTfU4jfWOTt7Tj45Xt3BLI=; b=GvOFh0WDpkul4VUMLCny+rjyM2MDtwUPWOmGoI9pTcE26QQs+zRSJgQSdqXNUWXL2D bxO4Tafn+tkUyvrHBMxxS2RqO0/7fgueCcEaDn5EnTA635bVIXG+rD3vgU50bnqoLSss VwPBAOk8VkTjDx9qLB5hHavNgENL97DMYPdzgduS7xkyM1IVaiKDLECnayPu514zvAME T0kE6K3uiEoXlFAf9iVViL2BErH1ZJTzguSK+MTwZ/pF/IjCPO3arYhdnbbB0+MJel96 MYT1KtJtQ3IBdXt4YjGFF0k5r0Vm+yHvyLWsTl80GFhJcomM40pdGa660WXzhnI6SaP8 DMXw== X-Gm-Message-State: AOAM532c2deUd5KOq5HUFgyL/FF71VgQo8SEaoluphM+eu+nMi2rjYAT gX8tmocHiW8YHZXfBOuTiq2gX2BzXesB8g== X-Google-Smtp-Source: ABdhPJw5aQPsOr82e1LYVUrIerNy3edIxJz19/Xp+yMGfQC+ouRFZaAZ4eZ8YhkHiiIAvQ1T0e3gCQ== X-Received: by 2002:a5d:6288:: with SMTP id k8mr30596304wru.137.1632164667885; Mon, 20 Sep 2021 12:04:27 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:27 -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 v7 02/17] fsck tests: refactor one test to use a sub-repo Date: Mon, 20 Sep 2021 21:04:06 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 Mon Sep 20 19:04:07 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: 12507161 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEB8BC433EF for ; Tue, 21 Sep 2021 02:58:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 948CA6112D for ; Tue, 21 Sep 2021 02:58:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231714AbhIUC7a (ORCPT ); Mon, 20 Sep 2021 22:59:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349407AbhIUC0b (ORCPT ); Mon, 20 Sep 2021 22:26:31 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5069CC1E3267 for ; Mon, 20 Sep 2021 12:04:30 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id w29so32591983wra.8 for ; Mon, 20 Sep 2021 12:04:30 -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=FtinolxSUiJS6loBlInPlbZmLZzLVWrH8sAer83veO4=; b=gjX61m3Mx07p/0utDW3MQsu9xANGSB8zFKM/e0TTZ2gW5VzH9DNn+6iezhtDwcXwPp Ncoiy5QqUUX2+0YYPEaLnoh9ym1Wmbowntzq/n0jlB5Cp3OU3jIvak3nPNpLYwV3VRS4 EBWMQ3HFhICy//7k2GUdvqpKSiHdieBgQttHzDXmCEmjn7rlCnNKigI/+JEgtDCySNIk 2SbV7uZ50UbJJeh6GNUAYaYGsFo5G+wZ8i/F3cBpFOrq4F8lIgceJ2Isdlewxcowkp1w mrZjLnq0rA9qEJunluVkSleG+k0QEf0IpqxEB7CPjkldubZt+it38Q/a4Rkt44Fi6fKE hmMA== 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=FtinolxSUiJS6loBlInPlbZmLZzLVWrH8sAer83veO4=; b=yK6YyRkyeafun1CGZsuMi07tjefzUz3dZvCyuby6w2iNUxpDo4dV1evcsB690OTpjf wsHaVl8zOiwK6gifx1mJjrEyl8xYNjGiY+JQgKlS5ELP2PVgz3InZJKgI1NM978013kw mNe6hMlLv5h4EvPHXK0nq5jfng1BXL8SBYnEZsCC6p+ley2BMPKkwB6icaOCruRqMhwE aghQJGc3fTMv12MfoHqayY+H3SuUnjx8XyNEawdru2NP5LOfju80nT3NHSa8AWjxwVTo 53VGvOjcx+H84zA0+zxVRR7iO9LP13v9Nh3/KN1480NbBJJMVpdnUuN25fNA5rcpwosE Y10w== X-Gm-Message-State: AOAM530xZOlMHT2/ozX7bi5SWAYx/tukFZ9iRaNZDpZeLcQ/6kJBYad/ PoLg0mB5sWjjZPSPcnK137h5HQa54k5jvg== X-Google-Smtp-Source: ABdhPJzkEIJiJY1+SWD6PWlsK4NbLm093el3w2tk4LTHkm7/OnMydoTx3nQcdzytV+ziflANkyuHSw== X-Received: by 2002:adf:f1c4:: with SMTP id z4mr30456413wro.418.1632164668642; Mon, 20 Sep 2021 12:04:28 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:28 -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 v7 03/17] fsck tests: test current hash/type mismatch behavior Date: Mon, 20 Sep 2021 21:04:07 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 Mon Sep 20 19:04:08 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: 12507171 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B7BBC433EF for ; Tue, 21 Sep 2021 02:58:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 24CFC61050 for ; Tue, 21 Sep 2021 02:58:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234775AbhIUC7i (ORCPT ); Mon, 20 Sep 2021 22:59:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349415AbhIUC0c (ORCPT ); Mon, 20 Sep 2021 22:26:32 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27D26C1E326D for ; Mon, 20 Sep 2021 12:04:31 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id w17so24466989wrv.10 for ; Mon, 20 Sep 2021 12:04:30 -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=O25dEyDSANR/yR1AZkz1pvQ6Kd3IIbCIMKFFNOZOo/c=; b=MRK7wtp7JThnhJptjVNs7biDSukWxJ5nUsD8XiJDz+ad13d/0JGFhkkTXRp7FAvLpT aEy2MjMjd5b+ic6XQWOQaHL6s4QhuhOriNINAGe7HZNz3fRLgzbbOuPLLqJc5JkYmWII cXbqPW7HPN1Chow7rq3TlflvTJtkBOkdkLlXhrhTQy/VR4cLGwXexkg+FnBCvYmi7+ux 7zYBdgf+CK25JOhyecFvcC+yjnJ684rvMzIZwx3OqOb0dgMAJTo+RiKwNwiM8oSIWk1Y yT7+a62TY9TRa9SKPhGjhPEltCdIHzrkgHqkFonYrohb7PRCvUOzsZfP0xiFWDRhxD97 qVhw== 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=O25dEyDSANR/yR1AZkz1pvQ6Kd3IIbCIMKFFNOZOo/c=; b=GWf4C0EBozQgqXPxSutt5eYkYjFovofU67BK/jHnFhg1XPPuTsNMcHlDu0mUJQEgyM xNN3WVHIgWC/3lfElC8GLiwG4lhYNaFihPbG1lRN/RbR0l2o4ZjvMbDuaxZAtFZH1YUt wKxiQ3ujcziQ6o124qiV5WCyIRLprjENYTaF08GZgN8gHaNPXAIq/kWbG4hQZzIAgBQV 4TBq5v+t23gJyNK5pw11tl0JWELyjB7xWmaU4GZawaFjc/LU8ZvPVFc+Af16wDcc89PF V5CHlAh6mMBH1pSWbK/OlkTYtqI6fuSmNjtmH8pNe692+4QKQdmQZjtzH+GHl+Hr2cWv pEFA== X-Gm-Message-State: AOAM5309GGvkEmujtGkbbIKMWKNuZQoPaGRHJiMaxFlOihvna34KE2Io sHRxiiuPglrap/rMYL7DA96ViBosZahBBw== X-Google-Smtp-Source: ABdhPJzA6QSXi5bQxQhkgmO3fjt7R6ex0aILmlqbZb0I6XAWZvQlT6Bi5ln6BZWIM4MoeBnyBnDoLA== X-Received: by 2002:a5d:5147:: with SMTP id u7mr30839671wrt.69.1632164669424; Mon, 20 Sep 2021 12:04:29 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:28 -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 v7 04/17] fsck tests: test for garbage appended to a loose object Date: Mon, 20 Sep 2021 21:04:08 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 Mon Sep 20 19:04:09 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: 12507169 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF9EBC433FE for ; Tue, 21 Sep 2021 02:58:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D787261159 for ; Tue, 21 Sep 2021 02:58:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234298AbhIUC7h (ORCPT ); Mon, 20 Sep 2021 22:59:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349417AbhIUC0c (ORCPT ); Mon, 20 Sep 2021 22:26:32 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3AADC1E326E for ; Mon, 20 Sep 2021 12:04:31 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id d6so32432533wrc.11 for ; Mon, 20 Sep 2021 12:04:31 -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=V0cRBVJ/03yTTIfUEPh6Sf5kKJOmQpC/BMvdrPYyidI=; b=HDDsb66fYC61gM0kwcuAG1OaV/a/iSAlPhgHLU64saSpPdfR2v+o/vZhASYQ2VcnI7 qZZDbrubVrW/erEcSMxOYiJXTIpsxRKdGNU3RT2BGXAmK+dDQW8/M5kV5xTrjm2zllyG byV96iYFnHQOxvlLyd4hD3lHN9qIpb+/qLnOfZ+V+DJy1p1t32VoVQ1HAeEJwgJ0vTXX lLvNcMXeEC3tTWQvf7Ho85tJGXKdzgG/+CY3JugvbDImWMgKXbp26lAOrv0hWxDL0CjZ tPkGwOUROv0qxgF8ok4llVxM7+Rbdx3AvyI57SglJZbjK+SxPpbh/MBSLRetTARomkLg ch2A== 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=V0cRBVJ/03yTTIfUEPh6Sf5kKJOmQpC/BMvdrPYyidI=; b=sGamzG1wT4N3ekLF/PTXOdNVCQd1vpaNO92342vPTm0uV8LHcrrPePmEeMNiVDHwXw PDUhNWq/KFjP+1NZImOnoUXiQq1bOso4J9sZ3YJ5o7AqqHu5oilOIaiQqRgk7HQmEKIQ jw2XEUBSdmH+2S0CQVPAgd5aryHDuCvov0+VVVQrsibSNCTxsnJ+h4hbcJHePtjvYjQJ /tJL/K1TV/Xmjdcq8IAeiIfLdO7c3FPcm/se8khcl5ly48J/CfRf3Bt/zpyRPWSmDhW/ wGa4yIQQRv6qxGENbmrTab17b0RgD+2tFfGUC/y5F567ppeQaldfYkilm1tFaxhv9VZA xrfA== X-Gm-Message-State: AOAM532ZPUNmpeci9BqpxitbA0/Y7zUAlilcTvv0Y+HFPkMc716S6XFA p6bQxqvxcir+lDEMTX4NQ2yQq4aSX2gdlA== X-Google-Smtp-Source: ABdhPJymk4n/FV6Qxrh+/Osn9o1awy5pVCKY31vpz8MsXxJzSi+C0KWn02whLQLQr1wkgL9CgByCAw== X-Received: by 2002:adf:9d45:: with SMTP id o5mr30379607wre.226.1632164670238; Mon, 20 Sep 2021 12:04:30 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:29 -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 v7 05/17] cat-file tests: move bogus_* variable declarations earlier Date: Mon, 20 Sep 2021 21:04:09 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 Mon Sep 20 19:04:10 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: 12507167 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93C2EC433EF for ; Tue, 21 Sep 2021 02:58:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C15D61159 for ; Tue, 21 Sep 2021 02:58:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232321AbhIUC7e (ORCPT ); Mon, 20 Sep 2021 22:59:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349419AbhIUC0c (ORCPT ); Mon, 20 Sep 2021 22:26:32 -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 BD55CC1E326F for ; Mon, 20 Sep 2021 12:04:32 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id w29so32592273wra.8 for ; Mon, 20 Sep 2021 12:04:32 -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=C3IG3iAz3KxBb974bzWBQ0RZEN+n2xGFgcUoNIBvhgk=; b=Vaemjfo3Rt9p2zYnngTYb7wMmTmkQypPpta04Mgri2kUnfmLL8Q4c3eF4brxRQetYF QjPgVGnWBomMw5ep2nfbe2dW4feFwn1VvSwc7yBCNLYsRcr8Wu09IsGSIU8TVPjECiJs cns7TPVXLvoJS6EnwryRscDkxvUSLg4XMIVHfBt3H2YquOnVeTRC7IAbLJ71ze6OZVNq CxCG4k1FK3YDQyALzR52pryjWgLckRmxn9TkCuz5+6ieVl4DOHSwAnkmXaXVEB2xQr9P 1GmQU+W3JF5ctWmMfcKapDLhYom/+bgFjoey+7s1z9FKG4w/Ga5RhHlQY31a1xcqmLtX Lf1Q== 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=C3IG3iAz3KxBb974bzWBQ0RZEN+n2xGFgcUoNIBvhgk=; b=48i2Xg55ho51fJKncgkkCCWRFrzoMrcKOhLAQCyBx+5F0slQCwVHmGwvotO8L/2mmM gCTU6tFTPBOEe3DnsZPXq9SWrXfrESkVifdFLvH2HJ+oAsVtJD2IbaJb1NWeZYmT932b Esiy0B6Dzd6IsLzxHm7ZNuLwgcn4IlpKIuxaIdD1N7sdiN+8zua0i79rG7ygvDQakXwt FGd5UMsIjT5Km19Isfdr3xAM4yKZqPHPKnA/ObHS2GtioVIgYm90U5l1A9bYb1/P1teA rpS27XB6FydYf+Z+hmKHLD/N+5cDYpc1xWNrPHoX3DHJRBrDInfw/S76LTlD10+CkGsm ZTlw== X-Gm-Message-State: AOAM5337F9MbCyoGDx+N9Ea0tr+zJFOtNDtmxGwBWsUcl8N5R0gm9k/n MRimdNL7R+GUM/6tkwUARWby57Ll7eml5g== X-Google-Smtp-Source: ABdhPJwgsizktOQLXJQajycZoYZLk6OBI1ZONT0c99OF30bMrQJqvCxAt4yL5jAZ26BjSHVDP/oNQQ== X-Received: by 2002:a5d:59a4:: with SMTP id p4mr30665819wrr.149.1632164671047; Mon, 20 Sep 2021 12:04:31 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:30 -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 v7 06/17] cat-file tests: test for missing/bogus object with -t, -s and -p Date: Mon, 20 Sep 2021 21:04:10 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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..ecffa9045f9 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 +deadbee_short sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index ea6a53d425b..af59613250b 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 Mon Sep 20 19:04:11 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: 12507163 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3A38C433FE for ; Tue, 21 Sep 2021 02:58:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D90CC61050 for ; Tue, 21 Sep 2021 02:58:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231932AbhIUC7d (ORCPT ); Mon, 20 Sep 2021 22:59:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349420AbhIUC0c (ORCPT ); Mon, 20 Sep 2021 22:26:32 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE955C1E3270 for ; Mon, 20 Sep 2021 12:04:33 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id d6so32432727wrc.11 for ; Mon, 20 Sep 2021 12:04:33 -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=rNBSjKdlf9tK7aWr3iQt2IEQ+I7orn/LbCkM+36hWyk=; b=j3G4LiU81ulgp/b8Fp9wRxkvTLRYcl6pOvhXzx2q6EYmVoQN5pp4gwkxPeX5vky5h0 UvpZTmTUUx65JTdPYYQyG6Y0+PFOpnYAv5sPZ2T/HOcNjmmad7BpLxd5xVhmV02fhHcp 3LCaz1o84pcLqMKOtSoUVRmWo9q5JHWExUxqR3gG9jVKSvf7JmWVN0ow6FAsw+Am7kxO cnuOrsk0cUByvdi8LKh9yQenRuXZKY13m2Zl1P3Gk4V0cq8hjVWCWhxa77MYIwoZ+qTN lT/Ym2pLkEHAkUfmauH8HGqZCvwESNl+uVklWfdlSO80bAedz1JP6YkZ/WD0WddmdrIY 0gNg== 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=rNBSjKdlf9tK7aWr3iQt2IEQ+I7orn/LbCkM+36hWyk=; b=WTZg3nevOZq8p+Vu4Ymat3KVNGkNrcuDSQOnBghnU41LHgww4+JWEUmoX06TKyItyw 5EkaRhhuZHPj3WiCthKak5ImX4ZsOwGFLDeUs/UDQttTVmVJjjbhZI2isVmWdW0g9PFt iTD9Lv3cddie1C6DrKKvcfwseUGwbzjTVgzSECFYV2lR0X/6qFMUlp0X878Tkyko8ca9 84UGoBLfSVx5nibFApdVd+7wM/Y0YOO9DkhHKX7uoODWGliWxh42UR4RXWjusI4cSwbC FIQiuLtz2bC/0ITXxJBwdUL6XOToHI9b+667mDsfg90MQjO0jtOCcvvr3Mgfn+NXqKko FG3Q== X-Gm-Message-State: AOAM530jQi9eDb31pbWVPg5Q6V0o9weZwjtlcx2n7GLqXwK1bvatCx// cjo3KfiP9OMyXTusekvvceeiw91ugvhUVA== X-Google-Smtp-Source: ABdhPJw2rHUmjS7Tke6CPORNq+gGNznZLxA6PrcX8o3a0o4/uVk6q50pHnuDKS8R0ZS1WX+/JOyr0g== X-Received: by 2002:a5d:5351:: with SMTP id t17mr30979220wrv.405.1632164672090; Mon, 20 Sep 2021 12:04:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:31 -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 v7 07/17] cat-file tests: add corrupt loose object test Date: Mon, 20 Sep 2021 21:04:11 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 af59613250b..8bbc34efb0c 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 Mon Sep 20 19:04:12 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: 12507175 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8B44C433EF for ; Tue, 21 Sep 2021 02:58:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F5906112D for ; Tue, 21 Sep 2021 02:58:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239964AbhIUC7y (ORCPT ); Mon, 20 Sep 2021 22:59:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349435AbhIUC0g (ORCPT ); Mon, 20 Sep 2021 22:26:36 -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 8CFF4C1E327B for ; Mon, 20 Sep 2021 12:04:34 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id w17so24467407wrv.10 for ; Mon, 20 Sep 2021 12:04:34 -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=EEujD/U9rZ84o26D9qaaLwC34RmOrNj/wMFAnOc3cq0=; b=m/RSmA/Fsn+SaDIlVIqbraaACNpgufb4L/HS44staz/khAHfk6O14NGaEMQ8krJ//n lKrDfQ9KFiP4XbAfZGZK5urSfWmgl/lQ6sPNueQWLW2avM35HEx8R5Q0tt03pHGWjE9j HaiO85T/Mgo+9bFIPHCAnK+pfPrwFceo5r6MBnuw0Xg+rkPN1W9BLEvSfkYKoeycxqq4 BkuQ4ed+TgEjsKc6CLw4IvpYs/cwMOZFM8jQy2iv8yF6EIg/rObFgtXzHsrcxIXjV2Yo uqvtFn5z0s0X/wXigAyixRPSDowAT2ftZx2s6LbMP3rB/7G/d60JbO33OkVLr7y1p/aX EGJA== 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=EEujD/U9rZ84o26D9qaaLwC34RmOrNj/wMFAnOc3cq0=; b=Kf+JCedvEEk6sw5qkizFBkyOF/0e08ijtZJSs1ApWKU9pCnsw1cmFaY399u4JnO+B4 Tr+dCGxGg6ATC+DZ36DpfQqLMrHrnDcWP2qdrjfu0A00j1oo8hlvNwQZL90TbK8LH91f nmXF8bE6a9guRZ9uNZSq0+CnOazyIaURGa4i/t1dp6qXJ7l6+ITVShC/QNwRnwM7CueA VMOOyZO5C3dPUtUY8MjwubZmLPS9/MhxUOlUowYWlrD5XBbGmd4S7EXQrJVFFw8Dm9Q6 ZEYWZeQjH1tKd8vvZsCVHjLfH4ZsRbXo1GVDhoZnwiurBT4Rl2LwQCfHcKTRiMgBlLuV I5Nw== X-Gm-Message-State: AOAM532dtCB625eFsAviU5ibOK32DmXhtAdz78OXAkT6U+VLuPmX1cXv 9Hmcd1KgfjZSlNRTfRryqg3F510HId77Sg== X-Google-Smtp-Source: ABdhPJydE2Q/hxOZ71IDg/taa9xH6FUC+tXf8NN9v9llqvKDnBLtT95Z5RhQ1Cs2YAkhQ5736GTbuA== X-Received: by 2002:a5d:544c:: with SMTP id w12mr17098152wrv.398.1632164672869; Mon, 20 Sep 2021 12:04:32 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:32 -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 v7 08/17] cat-file tests: test for current --allow-unknown-type behavior Date: Mon, 20 Sep 2021 21:04:12 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 8bbc34efb0c..269ab7e4729 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 Mon Sep 20 19:04:13 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: 12507173 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1E7AC433F5 for ; Tue, 21 Sep 2021 02:58:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BAD7E61050 for ; Tue, 21 Sep 2021 02:58:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236252AbhIUC7k (ORCPT ); Mon, 20 Sep 2021 22:59:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349436AbhIUC0g (ORCPT ); Mon, 20 Sep 2021 22:26:36 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82607C1E327C for ; Mon, 20 Sep 2021 12:04:35 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id d6so32432887wrc.11 for ; Mon, 20 Sep 2021 12:04:35 -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=ICxNleTqXjJVIXAtxmFjocwC4IZYQ1Z6cPnvPLZ7AXA=; b=MaoVnBKZgbvTZiyxpCNnaFxCLwG13WwNu5us5hRoXBuWkRPDesEy/qluwePkWSD6H9 U30DOdHcLpPvsJ+lAPpdlWX//ojEjxV9sylJPyWVl3YrqtIoQIH6oRcJ0kzm/JO461P9 3AwyhWi4ZH8zO376o8Bf0BaZnT5O+BSSLLqOZ4Lp2cuDcFt6Heo05vmTqYrun6kFCjM0 V0XqZyiQzR5Utj4lZebFJ9GfIGfJTdn0ywcNR57+hfaJQbpfAtEazQQoKClmbB8+Tspy OR9m2jlYCt1cQ+8XD35cwlfQIT5fTgYjskv1lv+HpLvUwweCfCWHMJHn4y90rxiolQtG GwWA== 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=ICxNleTqXjJVIXAtxmFjocwC4IZYQ1Z6cPnvPLZ7AXA=; b=i8fW/9bgmVP/smfyoIjcHnfKH9m61QIeTYH/w89rhOGfIrPyRMSQO9tiMnUpp+xPWK NukF2bTDFSq/0lHb3gF7asC4zOCTyC0P0PTHrS/Jgc3ZQlSXxCeRSUh3xtIaHdyGGqo9 kDOHRf10PMn4TeYyOOzwd8SVmPHrU8Rq44dexTUXBy9VU2LOdazduu2ka0mo92eER+HI yCPW9jUfLvTpk339cqBfCe8Qy0JFEIuEirxZXUTELDYmi8G35E2cklOQXdIXN72jppJa uv1AY6o0Tizfh1Zhg2g0Ka/A9jbSZ2LX2AgWBzu1l+AdXreTZnIgRoVs9bmEhDbXQHCG +W4w== X-Gm-Message-State: AOAM531QW2Q23HlJYWBG0wCFu9vlSwfnJMusS1JwuLyNlN9CEedG4HhQ Mq16yA/sW0r4xGK0txTbFbTYn4K6hgjfJA== X-Google-Smtp-Source: ABdhPJyIEr/N8JcEXpRIvs0G9Ocm7DIsgq3AKJDcvzSJjWGyAdAWS87aPzyu+cvpfZ2C6xP0ls/RQA== X-Received: by 2002:adf:d1cf:: with SMTP id b15mr4285984wrd.181.1632164673826; Mon, 20 Sep 2021 12:04:33 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:33 -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 v7 09/17] object-file.c: don't set "typep" when returning non-zero Date: Mon, 20 Sep 2021 21:04:13 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 a8be8994814..bda3497d5ca 100644 --- a/object-file.c +++ b/object-file.c @@ -1503,8 +1503,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 Mon Sep 20 19:04:14 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: 12507177 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B49AC433F5 for ; Tue, 21 Sep 2021 02:58:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4EEC16112D for ; Tue, 21 Sep 2021 02:58:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344142AbhIUC76 (ORCPT ); Mon, 20 Sep 2021 22:59:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349444AbhIUC0i (ORCPT ); Mon, 20 Sep 2021 22:26:38 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D2A4C1E3A40 for ; Mon, 20 Sep 2021 12:04:36 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id i23so32619913wrb.2 for ; Mon, 20 Sep 2021 12:04:36 -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=xrh8pMNZdmY0yM04hWbfQbCM1u7ZT6ZCDCZTSjHyTLI=; b=Pc/cK8BApil/927NqVwWLtKPBsvqFju5tcM+DXaCwSMeSPWxnoRQrxi+rf2O/zKoIU W2aKrrMbKuqnaeunJqmsUsKC4aONFdExPd0awpPwm2iUunmGZ4Lyi4tEceG0G9dyCL4m Y6JiRlX27fFxJIRTJtsDROf/wmgCcSZZPj56Ojzmx8fChrPCiiAZ4lV+01Jov9+9PNmw pceCPnEQiyd0ALwrVeM645vNs2fVhOmcREnqCHvp3b+NBaRG00XglO580DpZKOua2ByY m8eUqkBvPWZUxTs2IElpjTqe5WjXWmn5re493++La2C7xL+cyboFoVm6t/QGnmeLlOMB IkVA== 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=xrh8pMNZdmY0yM04hWbfQbCM1u7ZT6ZCDCZTSjHyTLI=; b=Vre8SJEdqtUUtI+OZGnNDaUv0F3DOgPSCwv4fTAUiUlNeCV91yJtTdy7nTKQiJSGTK F0OlXyHNWoZzfEBMgLEWuwgc0hmYYJAJaQUUaxRPLCxaEGSJ668Jnzjwe0olxUsuGsHP O4OLuhqIVqUgD4aTEK8a7Oqx4XO1Th5j+mu7aAhzEVStO8N2HNs1xmy5PV6byHudxAag C791t35kLXOjpYK+Ksw9QMlxu86cbL5POcxg2Sv2yde1MSEd8GEuy/wPPrce+0IvmW2l 25TAK4s6cPCTBRfax/MBBG+kpEMCmNigEzlsIc9FSNfTB1mh/DqMPBdRlnR9tGECyqfs u1/w== X-Gm-Message-State: AOAM530CWg8qwe4zyabykUaAIZvry2R8XchppnrUaStY2LsTyn5KX7WP T1rkrhqfA9YiCsDgXwYnXLEcd6w7shH4Cw== X-Google-Smtp-Source: ABdhPJw0YeDfKRakZqdSis3sNpCtSyKgIefCR/8CU5Arq8O79KU45Neg6xwEW0bf0shWNlLlqI7AUw== X-Received: by 2002:adf:ed82:: with SMTP id c2mr30461268wro.203.1632164674811; Mon, 20 Sep 2021 12:04:34 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:34 -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 v7 10/17] object-file.c: return -1, not "status" from unpack_loose_header() Date: Mon, 20 Sep 2021 21:04:14 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 bda3497d5ca..774ec8c866f 100644 --- a/object-file.c +++ b/object-file.c @@ -1262,7 +1262,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 Mon Sep 20 19:04:15 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: 12507179 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33F29C433EF for ; Tue, 21 Sep 2021 02:58:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1DB9C6112D for ; Tue, 21 Sep 2021 02:58:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346780AbhIUDAA (ORCPT ); Mon, 20 Sep 2021 23:00:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349450AbhIUC0j (ORCPT ); Mon, 20 Sep 2021 22:26:39 -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 59C31C1E3A42 for ; Mon, 20 Sep 2021 12:04:37 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id d21so32543905wra.12 for ; Mon, 20 Sep 2021 12:04:37 -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=NDhCyLnuBM/HSiv6/4E472OAQAfaxAUxMXejT46RBto=; b=SzauP3VNTACjvvZzWET7O86bHQDGFcVDb81y46h5VPDRG+puW03ppfxv/LOouW/9PJ JzdE/38tg3l9TK0sg0kHaNf5BueEBN6Zqk8l/Po9gJzDR5mneglaBOKboHzQOi2sFpZI Ckv5WySVKaZ2BHTnR2QHF5b4DZPWb9jNGg8g3AdDXCd91x/B1v3zd8MrBDHgHeDFqAY7 +OfNfY503ZieyqV5Vdn6ZXjNSTF6PGI7boW/1BBn2rgSA4Ax5at+h2gdJHk2vQwSs0xb 6vzIT6mJlXu8038rcl29xW1i3sTY9snuAYhDkdKJJuTUTTU4EGVoJNqSsJI5hysdsXZ0 SQDQ== 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=NDhCyLnuBM/HSiv6/4E472OAQAfaxAUxMXejT46RBto=; b=RiOeAZx/ZK+kMVEVco4VrznyVsRRoey/poAnFuF5jD2yJVVrmJCoQ4oJ7keJ1at4oV 6Z5l6tVBJuuIbHC835zGlbTjy662GXJrxuahxzHT0cpUyS8zNp42SB7U1t9ayXzzkrQN meDVBhjlDOELPLvWkQehP1D7v0/vzyo6jwXT0g4qa8ZLQ5IT+/F7PrfL7JLIR0WkAtMX 1Zw53qF5mQSfAfRZ1x4AwGkKhaVOWakgbFtuFyW7CDftpMVRokXkZhpOC8URcLuKojC+ +Iky/86Ej95A2J3480lzjLQ9Xn4AP6x8yOax99QCt2wr4CDrvwG4lHN5S/XwU8XikObb UeRA== X-Gm-Message-State: AOAM533+RX737q3kG8h8dGLq5bc7gcecaPnocciWXZhpKSxwb2sYIfpC gOcfsVAJerrlFh+gckr5Ty1yvmUxPRePHA== X-Google-Smtp-Source: ABdhPJw3xSETz7JHZpB9ehb/M8nCUNhcKDNheAzULx/wPsIlh/uSnBQTCOywli2fhEsOwvQRmwmlfA== X-Received: by 2002:a05:600c:190c:: with SMTP id j12mr619187wmq.122.1632164675721; Mon, 20 Sep 2021 12:04:35 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:35 -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 v7 11/17] object-file.c: make parse_loose_header_extended() public Date: Mon, 20 Sep 2021 21:04:15 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 d23de693680..33cacbd22ac 100644 --- a/cache.h +++ b/cache.h @@ -1314,7 +1314,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 774ec8c866f..33a01ac203f 100644 --- a/object-file.c +++ b/object-file.c @@ -1363,8 +1363,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; @@ -1424,14 +1424,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) @@ -1486,10 +1478,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) { @@ -2573,6 +2565,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; @@ -2587,7 +2581,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 Mon Sep 20 19:04:16 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: 12507183 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 817E0C433EF for ; Tue, 21 Sep 2021 02:58:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6B2AA61050 for ; Tue, 21 Sep 2021 02:58:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348601AbhIUDAI (ORCPT ); Mon, 20 Sep 2021 23:00:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349458AbhIUC0k (ORCPT ); Mon, 20 Sep 2021 22:26:40 -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 42E8BC1E3A49 for ; Mon, 20 Sep 2021 12:04:38 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id d21so32544000wra.12 for ; Mon, 20 Sep 2021 12:04:38 -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=GigY7gpiGAm1xnK5QvKmahCwWxqwOs4NVCBncTxLzug=; b=kbO4Eu6Qup51kcxZay8vVh9XsVITmZhlQznub9oJoJiEQWd5tVHZmBgX9u70OUrf6W khN4zLYDgfXCJvCt2rcevnWnDFBT3Xo0prfTLonVmBRj7N1hoLtZ3LMwKT+BOlWkpjec VW4LKk+37DME1nZSC8caoxSB8zukwDZ/Ebgm6qlbkFLsgvncFGLjRnE7zyhcPoPsJFGU UU8MosBkmq26F4TpCRB+miXA3mU+Aro2J8yl+WBFbolwDCgHcku+IceoWpXkka8LDemW 4mtaqUY+2EUU1UZe9RU28nfljw0x1Se29PSXWn3Jop+xAvOFmZPEHajrxgfTNHSeBv1d JXdg== 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=GigY7gpiGAm1xnK5QvKmahCwWxqwOs4NVCBncTxLzug=; b=xWIEU/NcE/b1m9MwBQIWOs9gdowHO3z8Cl56aODs+y8Nq2DmypqjSiDFCYOEVvGwIn ncgUmZKwQidPFjA7aGrQsZzlbJBcw+t/Ow/nZNnmxNfkr7SwHR5yOE+ZSWIUkJ6YqICh wtkjL83jA5kcq3EJuzDt58pSgdrgzJRUFqc/YE+LiCXPE5/NYBz6Vp4JFpWlB1nrtyzV KSteAXT6jrPYlddK2v476HF1kBRVpQcx7B1t6xAFrz4ZhPzrTECvjl1SI9E4uA53A+Qj foGTjxXZoTLoGM7nibjYfwIZWPQBnjEt+RbUSC6Sk0nzsqmajgKZiF4XC1/exIdfnBDb qbgA== X-Gm-Message-State: AOAM530UCd8sK968t1sbJ4OYYzarXKh04wb71+bonjNoqPtd7nfVpA6H G8bBHxSjDoH5DJ06eqWM9rO7SFgOCEBUDQ== X-Google-Smtp-Source: ABdhPJyNOS8j+EjtWVQ3lgCiGh106SycjVsdzfNYde6+o8/3ON5mAUXyfmTj3zOsYIMXIsR/TrPg0g== X-Received: by 2002:a05:600c:3588:: with SMTP id p8mr591867wmq.168.1632164676576; Mon, 20 Sep 2021 12:04:36 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:35 -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 v7 12/17] object-file.c: simplify unpack_loose_short_header() Date: Mon, 20 Sep 2021 21:04:16 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 33cacbd22ac..9ad81d452ad 100644 --- a/cache.h +++ b/cache.h @@ -1313,7 +1313,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 33a01ac203f..8dd35f768bb 100644 --- a/object-file.c +++ b/object-file.c @@ -1233,11 +1233,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)); @@ -1248,35 +1249,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; @@ -1286,6 +1260,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 @@ -1435,6 +1417,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); @@ -1468,11 +1451,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) @@ -2576,7 +2557,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 Mon Sep 20 19:04:17 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: 12507187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96219C433EF for ; Tue, 21 Sep 2021 02:58:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 73E0760E9B for ; Tue, 21 Sep 2021 02:58:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349420AbhIUDAX (ORCPT ); Mon, 20 Sep 2021 23:00:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349465AbhIUC0k (ORCPT ); Mon, 20 Sep 2021 22:26:40 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A792C1E3A4D for ; Mon, 20 Sep 2021 12:04:39 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id t8so25606613wri.1 for ; Mon, 20 Sep 2021 12:04:39 -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=+7ySqHFXMX8wu3pEPJ8kINGE2FfV+eNQZ1IXAeboVH8=; b=fGfxffl878swgsHTBpnT/LvS2/cCrVmaxlbBtr8TDGjlZ8W6NJ+0C7TNT2UQeMnowQ lKLTMa9TrrtYz3KsRvDLnYGax8aA5I7ct+tteWeIjd/l5e2BUI8ub9rUdBlneV1oW/Gn GN4yggwzmFWgtJWtPnTCZAoH7ZPzKx/EzYO1K8PQmGGbcNcBdgnMEzi22drvGKPxbCFP s+mwM6TUKghjMJUZnhXXLxoWAJ43gbHnkZMi1wzmfTSiTjeP857RMo+hukj/6Vi8qaWk BlA1tvFe5MbxXbkFii1qu1TqHfuw8SY42hBx3YJqKcaYBTp7nMzPwl5EE1XUGtpDU34N shzw== 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=+7ySqHFXMX8wu3pEPJ8kINGE2FfV+eNQZ1IXAeboVH8=; b=evWkZoacE3OeRIPGP24YAB5/2y2jXSuWhddsCENY6TugrEH435r3bzJlG9affmVB2F FrsNHgi9nDQCdInA5w7+s7XKHOhhnb5VfXpkZCvHIX5GUsT5RSrKb0vXJk4uXYNeJR8Q R1josZnOq89+d4BOc8+1IZewVy6xsyEo5s8Redd9VxaQGg0Yyl/mo/JCkiqQCaepHtHa qFdwpUGljUJSPP5uoJcSrGDCsAJGIXfXsLUkkiOWOqSeovv0bYSOrfhRjkiEx68mEsOD 0wwbSXklUFeeS1kATjmkleF0nBZc63yiVTC7xN3gVQApUUHpelsOskMwjIue5GIM9s5p FN0Q== X-Gm-Message-State: AOAM533HyJPHILLw3uEcCGN3ohrT4B4dPMVECmzTGfz+Z1rMiCH2q3oj yv9D5zYcMFJEA1OTZNNHAAdTC4K8i77M3A== X-Google-Smtp-Source: ABdhPJze3PEl/Gfex/9VLYamqEXVastwV1Mx94TVxjQkM/bm5ufJGPInD/YcZ8yuZn7uhAtyOZdmGA== X-Received: by 2002:a1c:7413:: with SMTP id p19mr632432wmc.98.1632164677435; Mon, 20 Sep 2021 12:04:37 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:36 -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 v7 13/17] object-file.c: use "enum" return type for unpack_loose_header() Date: Mon, 20 Sep 2021 21:04:17 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 9ad81d452ad..90dde86828e 100644 --- a/cache.h +++ b/cache.h @@ -1318,7 +1318,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 @@ -1326,9 +1329,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 8dd35f768bb..b214a152ca8 100644 --- a/object-file.c +++ b/object-file.c @@ -1233,10 +1233,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; @@ -1252,13 +1254,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" @@ -1266,7 +1268,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 @@ -1287,7 +1289,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, @@ -1452,13 +1454,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 Mon Sep 20 19:04:18 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: 12507181 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 967F2C433F5 for ; Tue, 21 Sep 2021 02:58:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7A52060E9B for ; Tue, 21 Sep 2021 02:58:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240043AbhIUDAB (ORCPT ); Mon, 20 Sep 2021 23:00:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349468AbhIUC0k (ORCPT ); Mon, 20 Sep 2021 22:26:40 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FCF3C1E3A4E for ; Mon, 20 Sep 2021 12:04:40 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id x6so32565496wrv.13 for ; Mon, 20 Sep 2021 12:04:40 -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=/2DN0Xuw/aKGv6qAM1UCbcaHEea2KjpY3awCCXYW5wY=; b=R3T63oRLowUTHcExZRV3Coa//EANBn7m6A2jWz5nP6CBT0zPzCmKo4PyStPofkCErU YgKwxEl9lFD/Hfp1AZqxmMxpDEXD79mVJ/d0VK0vZlTh2IvCZQP/Z6mb7X0zt1owZIGY uCCIzjKz+P7VYa5JognupbbjYyEmQ14I3Vz0uzwOBGKJsxqQ6EqFySbquY8wR5AZXGXM rcU73E/Zp/GpGF0qcBTyJSSJd7xXuFAbvjKCMFyGL4fIAYNsPpLOoqx7T5/NAPOo6OvX VFivnaNUKI74Rd1hgFD3Aqv9pfidyVioJBRexKt1V5UHfln5GUWhLLmCQPeCPwlSUlMV B5TQ== 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=/2DN0Xuw/aKGv6qAM1UCbcaHEea2KjpY3awCCXYW5wY=; b=sqh4J7jbEnksC6tyki7/TzKKMEGSHTOz6Kob9n6Pwck0etm9781u2Gc8iqtzIZU/95 N+qiy8CMwUf+8+q7+w30Q+SGmVCLshUveXX/s1lLSazo9MApiTtia523QYm+wlMLKnaL rzWeDU5ELFp/8PMXX2hy26ALyuYmDXSsuXXtbKfjNQzB1nFKwuSQ3XIeuCap+4W4PmHR XBntTS2YZqeXGVVE5Mdin1cKMny1KFdMPSCxEO0jel8FEY7cGoRSYmvMU8e0DrD2JoLF NULnSYT8UqyasBDUwqPxD7nO98VxsQWzcUgANSnBGjPd1XaTqNrDpI0M/t3jsGtH6hTd jUnQ== X-Gm-Message-State: AOAM532vW3LUNVToVxNIVdRijzAUDAdvaE1eH30v3nZbWx3D6MqiUWsc hp5agkubSv9vizELKCpX9plofiidO9M+Pg== X-Google-Smtp-Source: ABdhPJwuKCJRT9c5019nJ/6LkcllQrV2+UqShOGVgOh60XCvF8GonsuAXNq+wPbyeAQ+9dkUpfftjA== X-Received: by 2002:a05:6000:1090:: with SMTP id y16mr17667749wrw.208.1632164678454; Mon, 20 Sep 2021 12:04:38 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:37 -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 v7 14/17] object-file.c: return ULHR_TOO_LONG on "header too long" Date: Mon, 20 Sep 2021 21:04:18 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 90dde86828e..49b18f2755c 100644 --- a/cache.h +++ b/cache.h @@ -1322,16 +1322,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 b214a152ca8..ca4abe172ce 100644 --- a/object-file.c +++ b/object-file.c @@ -1268,7 +1268,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 @@ -1289,7 +1289,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, @@ -1462,6 +1462,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 269ab7e4729..711dcc6d795 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 Mon Sep 20 19:04:19 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: 12507185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C581C433F5 for ; Tue, 21 Sep 2021 02:58:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 06EBA60E9B for ; Tue, 21 Sep 2021 02:58:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349264AbhIUDAR (ORCPT ); Mon, 20 Sep 2021 23:00:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349467AbhIUC0k (ORCPT ); Mon, 20 Sep 2021 22:26:40 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 210E3C1E3A4F for ; Mon, 20 Sep 2021 12:04:41 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id w17so24468115wrv.10 for ; Mon, 20 Sep 2021 12:04:41 -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=Rsym80GlWe9syuSJcJIUbO0cpzz6ZXgN45AD0ydMUUQ=; b=HG0au+UJMXsvWWgqWKOW933NWmuvifuZt1/hDmJk+/v0GojyMPLmlD1aTDB5LKANbr Txw08DiCAOWucUlnc2Af+bZLj6VytqUy2+Uj+H0grjCHfxFhsnSIyZV1pZoDqkHmh+dT vygH7WifmWZmCrdwni6ePiWXh+dCIJiuTamwPHVdnKdyG81NdbP1Z7BSsLKgpJbGIXzf yeGWr2KRHMolN0tY53l+vL9yNoez9fcDHFvCUNPaVG/OdYHC3Ow+XiMLQVQn5D4zDxx2 i9lx92+aqM00UkMop9mVyyoIR9Pr01IAnhpQypWkE7cZef8ujdjpt4R+cYwqnOFqmAJo JQVQ== 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=Rsym80GlWe9syuSJcJIUbO0cpzz6ZXgN45AD0ydMUUQ=; b=ticzIH4gXrLOTHTm2feq1tdNHqCARr2NlnPj16uMi3nRYEAsalu8uqMLM1UnIKAANn vnIEwJ16fmZ5Jt88+mUIeFWQvmNGg/C/Mv/kwmvOoeH9Y2D0ug6Op/SM6lIWbCMF4sjg WLCkQXCNCEc5lNUDtHU5vhzoOekH9noHohEjon+owhBOphp8QL+qGUtEES/Fcj12zfA1 DmYyFgfw7wMt000FQSK1WypKpfpwp4QfI4drqVZtxvXoMPti9sETEIX/u24sM+kK5+pH k9BY7m8zXzjd8meQhOkRFQkGNpjWDBHjsCc2Dp37FSlYB+VyBdOPw7vOSBZczA3R8yhx LNFA== X-Gm-Message-State: AOAM530IMiAIswr+rBX0lARXsiZNofm9zADfj/qhyEV9peJNMARk8QQf GjnLEGWsIPgwkYztEhRAOxXmFH39LzNpbg== X-Google-Smtp-Source: ABdhPJxH5ZrKMM+F3NidQC9u2oNZ9FB3ET2LOVSneoqVdIZOjZV6f32bcB+dCU0mcsLEWjKQTUS2Nw== X-Received: by 2002:a7b:cb97:: with SMTP id m23mr633948wmi.70.1632164679354; Mon, 20 Sep 2021 12:04:39 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:38 -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 v7 15/17] object-file.c: stop dying in parse_loose_header() Date: Mon, 20 Sep 2021 21:04:19 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 49b18f2755c..23f0534b70e 100644 --- a/cache.h +++ b/cache.h @@ -1343,9 +1343,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 ca4abe172ce..1af914c19c6 100644 --- a/object-file.c +++ b/object-file.c @@ -1347,8 +1347,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; @@ -1370,15 +1369,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; @@ -1405,7 +1395,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, @@ -1419,6 +1416,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) @@ -1450,6 +1448,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; @@ -1457,6 +1457,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"), @@ -1468,31 +1480,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; @@ -2559,6 +2556,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; @@ -2575,12 +2573,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 Mon Sep 20 19:04:20 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: 12507189 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17680C433FE for ; Tue, 21 Sep 2021 02:58:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F0E3161050 for ; Tue, 21 Sep 2021 02:58:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238536AbhIUDAY (ORCPT ); Mon, 20 Sep 2021 23:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349469AbhIUC0k (ORCPT ); Mon, 20 Sep 2021 22:26:40 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34CE8C1E3A52 for ; Mon, 20 Sep 2021 12:04:42 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id w29so32593313wra.8 for ; Mon, 20 Sep 2021 12:04:42 -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=z9nL+vhlbp5uEg8zinIJxPwi6bXN8vgV0Ajgo4TUWVY=; b=iR2mMnNxWs6owfSIRTCFHeRLRPNtvsNxor9JxgFN9iYfodsmWn4qMXClCle/COXFNs JVaBX1dguU4bDr/JeMw9djaTKpfiwLQY21aFuSgkDnjq9Rp/96QOqMJd1feBeTDPl9Ag 6LPfjbkdq7r1IeX71GPrGOFeaBrYP7gSZXf+JweQjtWOmbNwv3E1rdInTAOUY2Q0NTca OeMboASI4DzdWpk8f5rdBz7M2g9o7EQoiAyyIH3Fc/X4PtDqIhJSOSMLIQV51F58PB3D tbrlTjWvo8YRXlgZTlYNBfJGkr+X+3xxAuh24zgROtOUjSIb2U707oYNb4deA38vVSvE 44KA== 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=z9nL+vhlbp5uEg8zinIJxPwi6bXN8vgV0Ajgo4TUWVY=; b=C9poGj943gVUFlFjYwIXkI4LQzcMZAHlH6tj0OpnK7RNwh8z+l/OnTF4S51nB3VTYB 4KDCWWXjAwuC6xdoE6nfDRnh3YyVvMN3GJITJMdY+JHqHNu9y24uw9Rog/kEqD9/U479 14dwHjJhfJjD0UtP2+biqxuqxNH4b84lOYvJuijnl6YIEHrcafhxn3sVQLioWvXhQSvz iRA0yBdelwNqZx+QUZIwVWxIiomaPVSHLVUoUjKU1OrvgBENDSVuCM42T5qWDIHRYCNB f0sQZESI3yaLT6UWzOdJvIjIltvdAeffi1XpC9Ms9LyyyNw721SjgE4tHetiUJ9wWXIp RnOw== X-Gm-Message-State: AOAM533mFvuqUYOtWO224JnQRBYOkLzd1FC1MOcTTOj6xpY0lWphlw70 5HAcLLcgzENz8ouFD2CIBC12QSjbdGkCEw== X-Google-Smtp-Source: ABdhPJxhorTp4wUoaOqsww4GHp9NxDBwnSku3qnLzPVyyC3rdApoEihD6Q+dnyF6OzBYale+nznG7Q== X-Received: by 2002:a7b:c242:: with SMTP id b2mr605888wmj.182.1632164680413; Mon, 20 Sep 2021 12:04:40 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:39 -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 v7 16/17] fsck: don't hard die on invalid object types Date: Mon, 20 Sep 2021 21:04:20 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 1af914c19c6..be568ade95b 100644 --- a/object-file.c +++ b/object-file.c @@ -2546,18 +2546,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; @@ -2573,15 +2570,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 { @@ -2592,8 +2587,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 b4dc6668aa2..e8b4d87b898 100644 --- a/object-store.h +++ b/object-store.h @@ -244,6 +244,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. * @@ -251,9 +252,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 Mon Sep 20 19:04:21 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: 12507191 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D6E0C433F5 for ; Tue, 21 Sep 2021 02:59:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E0CFB60E9B for ; Tue, 21 Sep 2021 02:58:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349635AbhIUDA0 (ORCPT ); Mon, 20 Sep 2021 23:00:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349481AbhIUC0l (ORCPT ); Mon, 20 Sep 2021 22:26:41 -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 42C54C1E3A58 for ; Mon, 20 Sep 2021 12:04:43 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id w29so32593429wra.8 for ; Mon, 20 Sep 2021 12:04:43 -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=eZnPIGN3ypvcn49wFseZ0M+LcawnkNCz0pUXDw0GQwA=; b=WWSn81tLLfHXJTWBFMlh1EUHMpmEZbO5CX4zJUcBDVBXxJG2w/Y1Tm64VThTBmIaY2 CuFmJTV+aM3nuNRsboV1Qx0lGkvMSadmr53gDOLWvU2dLZ50sGWttI6gRulFB1f280tn ppjNFQsPArK1Q+tQXGs2Lys95WfUTf7gC+O2xDME1E94fQj7B9TjkLx8zm0j/OAgAh43 9QLZxE9K0SOSs94d5rBRDFfjJnAAyL8rVwt+wimCBHmhsVQtu9AMjRn55o0gXgeWzzgT coKQZyuijYo5ubiXBcN5NJGEfOemZFKs9qD42c+dDqUOm9HaLwxdvCiuvHJRa6RIFKpc 3Weg== 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=eZnPIGN3ypvcn49wFseZ0M+LcawnkNCz0pUXDw0GQwA=; b=rrqcUJlo+X5AC1Jer50tx3n0U0JPkHBgr0DioeKPEXI4BCPIv/K7Mdi/gVpi6Xpyar p4sH+eDhWHIz0WBBCZe0s/ErMEkwQMXRuRbQHgNEqU9G/fEJ5lrWS2lJx99Bbju7jKW2 4PxJKh9853wSOBDZ4kL6VEvLmqF2IlaWb0gYPNF+Y7T1XYbOiBLMTVyJ0rCg8oslRMiP dmB/FJfn5yi3oBRmrQINBJ5dGAMxQ2zoPW0CKkzN+6FmBXBBIMy8M0mlxqLttSrO3mM3 K7+7UWl/HXOvSVGE0/d3iW0vuuvryLafrm4ML0hn3K8K7auM939x9FwPjU/JT9EkNjXf sf4A== X-Gm-Message-State: AOAM532mYapBr2pN9wNGDzn7l+hVq2ktFN3ltstJdDnmKFxUgQxin6zF I8V4Q7maILkkDRwLdk2Gcc/zW7aeLxP0nw== X-Google-Smtp-Source: ABdhPJxZGV+fEoA41kVPofeWQrLXKtMHtKfHLsNBlmr7fi0kKbjGwFyvXduWWMvc14RtzaUWJWkxIg== X-Received: by 2002:a05:600c:4c09:: with SMTP id d9mr600119wmp.194.1632164681463; Mon, 20 Sep 2021 12:04:41 -0700 (PDT) Received: from vm.nix.is (vm.nix.is. [2a01:4f8:120:2468::2]) by smtp.gmail.com with ESMTPSA id o19sm8157772wrg.60.2021.09.20.12.04.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Sep 2021 12:04:40 -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 v7 17/17] fsck: report invalid object type-path combinations Date: Mon, 20 Sep 2021 21:04:21 +0200 Message-Id: X-Mailer: git-send-email 2.33.0.1098.g29a6526ae47 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 6cc48902170..17c4b1d3ead 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 23f0534b70e..44b11f52362 100644 --- a/cache.h +++ b/cache.h @@ -1355,7 +1355,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 be568ade95b..ff0e465d556 100644 --- a/object-file.c +++ b/object-file.c @@ -1062,9 +1062,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; @@ -1072,8 +1074,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); @@ -1098,9 +1100,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) @@ -2546,6 +2548,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) { @@ -2556,8 +2559,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); @@ -2587,9 +2588,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 e8b4d87b898..77aa3d857cc 100644 --- a/object-store.h +++ b/object-store.h @@ -252,6 +252,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 711dcc6d795..1f7cc0717b7 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 ) '