From patchwork Mon Apr 26 01:03:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "brian m. carlson" X-Patchwork-Id: 12223639 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=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,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 7A606C43460 for ; Mon, 26 Apr 2021 01:04:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 58188611CA for ; Mon, 26 Apr 2021 01:04:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231615AbhDZBEu (ORCPT ); Sun, 25 Apr 2021 21:04:50 -0400 Received: from injection.crustytoothpaste.net ([192.241.140.119]:41842 "EHLO injection.crustytoothpaste.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231603AbhDZBEq (ORCPT ); Sun, 25 Apr 2021 21:04:46 -0400 Received: from camp.crustytoothpaste.net (unknown [IPv6:2001:470:b978:101:b610:a2f0:36c1:12e3]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by injection.crustytoothpaste.net (Postfix) with ESMTPSA id 7860961478; Mon, 26 Apr 2021 01:03:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=crustytoothpaste.net; s=default; t=1619399015; bh=+olbTD51KsH9pvMu2ZfXjzWAIu4Iqz3BCHZ4gtMQgYM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From:Reply-To: Subject:Date:To:CC:Resent-Date:Resent-From:Resent-To:Resent-Cc: In-Reply-To:References:Content-Type:Content-Disposition; b=iXzYe7O5fVCJarEHP+nG7q0Y+Px0W0ty91lk/pSWHAucUMQYCsNPbU9WPd9sh8DJ2 fQHSjdQ01uXq9kFp+K1H4TUIjtc+0yP5L4FRsDuRQwk7tHm8cIDHZ0JHwm9TOBIJES YxM0pFEOqck3F45Eka16/gLIT8thNsHL1LmYgbebdM/WxJucZgkAhZm2bJAXOeQ8dx C8N+X6u+L0tM4l1vLp4itRZ4Vkd3xmU+KXvEl47wGN11icQt+lh3dQUHSwmL8tBVP+ +f7/FP9rpuudABBJoANQYb01MWJdqXwIaBa3OMhLlYS3RILRndW6Mj0jhtCcqYvKjy ocfsLOVlOzB6Wm80PBP2zPsXEaxjw5w3f+RHABvAXg7FsNIYOs9hiASLH7aoCJzeqm CJ8fzoGGLEfKN/c/fEXYiWhhNfDIRCqwqV+5ShfvAXebk2ZbgKjVvJcVDHehO/xO8K xFlj79FFS1g/mQnzW9vyw/jMMMbWQUoz2O51fj4FczUTdTbHcFy From: "brian m. carlson" To: Cc: Derrick Stolee , =?utf-8?b?w4Z2YXIgQXJuZmrDtnI=?= =?utf-8?b?w7AgQmphcm1hc29u?= Subject: [PATCH v2 12/13] hex: default to the_hash_algo on zero algorithm value Date: Mon, 26 Apr 2021 01:03:00 +0000 Message-Id: <20210426010301.1093562-13-sandals@crustytoothpaste.net> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d In-Reply-To: <20210426010301.1093562-1-sandals@crustytoothpaste.net> References: <20210426010301.1093562-1-sandals@crustytoothpaste.net> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org There are numerous places in the codebase where we assume we can initialize data by zeroing all its bytes. However, when we do that with a struct object_id, it leaves the structure with a zero value for the algorithm, which is invalid. We could forbid this pattern and require that all struct object_id instances be initialized using oidclr, but this seems burdensome and it's unnatural to most C programmers. Instead, if the algorithm is zero, assume we wanted to use the default hash algorithm instead. Signed-off-by: brian m. carlson --- hex.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hex.c b/hex.c index e7af18fe55..74d256f239 100644 --- a/hex.c +++ b/hex.c @@ -124,6 +124,13 @@ char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash, char *buf = buffer; int i; + /* + * Our struct object_id has been memset to 0, so default to printing + * using the default hash. + */ + if (algop == &hash_algos[0]) + algop = the_hash_algo; + for (i = 0; i < algop->rawsz; i++) { unsigned int val = *hash++; *buf++ = hex[val >> 4];