From patchwork Thu Jan 14 14:51:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 1426417 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=fwts-devel-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DGnM66hsbz9sVt for ; Fri, 15 Jan 2021 01:51:29 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1l03y8-0007Oe-C1; Thu, 14 Jan 2021 14:51:24 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l03y6-0007OJ-MD for fwts-devel@lists.ubuntu.com; Thu, 14 Jan 2021 14:51:22 +0000 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1l03y6-0008OQ-Ed; Thu, 14 Jan 2021 14:51:22 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH] tpmevlog: clean up some minor cppcheck style warnings Date: Thu, 14 Jan 2021 14:51:22 +0000 Message-Id: <20210114145122.217683-1-colin.king@canonical.com> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: "fwts-devel" From: Colin Ian King Reduce teh scope of some variables and only initialize them when really required. Cleans up the following cppcheck style warnings: src/tpm/tpmevlog/tpmevlog.c:156:11: style: The scope of the variable 'event_size' can be reduced. [variableScope] uint32_t event_size = 0; src/tpm/tpmevlog/tpmevlog.c:442:13: style: The scope of the variable 'data' can be reduced. [variableScope] uint8_t *data; src/tpm/tpmevlog/tpmevlog.c:155:20: style: Variable 'hash_size' is assigned a value that is never used. [unreadVariable] uint8_t hash_size = 0; src/tpm/tpmevlog/tpmevlog.c:156:22: style: Variable 'event_size' is assigned a value that is never used. [unreadVariable] uint32_t event_size = 0; src/tpm/tpmevlog/tpmevlog.c:340:10: style: Variable 'ret' is assigned a value that is never used. [unreadVariable] int ret = FWTS_OK; Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Ivan Hu --- src/tpm/tpmevlog/tpmevlog.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tpm/tpmevlog/tpmevlog.c b/src/tpm/tpmevlog/tpmevlog.c index d712e131..1b952820 100644 --- a/src/tpm/tpmevlog/tpmevlog.c +++ b/src/tpm/tpmevlog/tpmevlog.c @@ -152,8 +152,6 @@ static int tpmevlog_v2_check(fwts_framework *fw, uint8_t *data, size_t len) uint8_t *pdata = data; int i = 0; uint8_t vendor_info_size = 0; - uint8_t hash_size = 0; - uint32_t event_size = 0; /* specid_event_check */ if (len < sizeof(fwts_pc_client_pcr_event)) { @@ -270,6 +268,8 @@ static int tpmevlog_v2_check(fwts_framework *fw, uint8_t *data, size_t len) /* Check the Crypto agile log format event */ while (len_remain > 0) { + uint32_t event_size; + if (len_remain < sizeof(fwts_tcg_pcr_event2)) { fwts_failed(fw, LOG_LEVEL_MEDIUM, "EventV2Length", "The length of the event2 is %zd bytes " @@ -290,8 +290,8 @@ static int tpmevlog_v2_check(fwts_framework *fw, uint8_t *data, size_t len) pdata += sizeof(fwts_tcg_pcr_event2); len_remain -= sizeof(fwts_tcg_pcr_event2); for (i = 0; i < pcr_event2->digests_count; i++) { + uint8_t hash_size; - hash_size = 0; TPM2_ALG_ID alg_id = *(TPM2_ALG_ID *)pdata; ret = tpmevlog_algid_check(fw, alg_id); @@ -337,10 +337,11 @@ static int tpmevlog_check(fwts_framework *fw, uint8_t *data, size_t len) { uint8_t *pdata = data; - int ret = FWTS_OK; fwts_pc_client_pcr_event *pc_event = NULL; do { + int ret; + if (len < sizeof(fwts_pc_client_pcr_event)) { fwts_failed(fw, LOG_LEVEL_MEDIUM, "EventLength", "The length of the event is %zd bytes " @@ -439,7 +440,6 @@ static int tpmevlog_test1(fwts_framework *fw) tpmdir = readdir(dir); if (tpmdir && strstr(tpmdir->d_name, "tpm")) { char path[PATH_MAX]; - uint8_t *data; int fd; size_t length; @@ -449,6 +449,8 @@ static int tpmevlog_test1(fwts_framework *fw) snprintf(path, sizeof(path), FWTS_TPM_LOG_DIR_PATH "/%s/binary_bios_measurements", tpmdir->d_name); if ((fd = open(path, O_RDONLY)) >= 0) { + uint8_t *data; + data = tpmevlog_load_file(fd, &length); tpm_logfile_found = true; if (data == NULL) {