From patchwork Wed Feb 8 14:37:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 123466 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 258DB41C40; Wed, 8 Feb 2023 15:38:06 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 093294014F; Wed, 8 Feb 2023 15:38:06 +0100 (CET) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mails.dpdk.org (Postfix) with ESMTP id 83CB240141 for ; Wed, 8 Feb 2023 15:38:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675867084; x=1707403084; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YY4arLhWcHmbY8jg/JKXotLgHFDfLxAL/Jo5k3Qe+YE=; b=gCaHy1TR6Em2X/B6b79K0bP/Pw7yZ6EteQj/fq4QMqdi5fejU0iiV68g 7nZxDL/0VAFT4m6QBLoj1cGba5p/Y+6vnHeBgJtRqWRmDrOU8vbFo1PUW 2ZxnEsUcFxbtT0hX0rccwhCclITZOuNw3rOLvewhEWNCA3QGwMStC48Ws SbAo1DgfRlnys6uFdwy/8q42qAIaxphBkAeY37cJKrZ8R3vKgy3WwHuug Bmh97JfKty2G81bsnQBzzY/15JqvqDPX3Cw/tPwWlhJdeAroNp9hwGLBz 3bmsjNow0C3X48ODnZSk3tS+DMg3YQW+qMjpDmsDYZ3SQTRTIAIVe+Yc9 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="357205574" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="357205574" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Feb 2023 06:38:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10615"; a="644880279" X-IronPort-AV: E=Sophos;i="5.97,281,1669104000"; d="scan'208";a="644880279" Received: from silpixa00401385.ir.intel.com ([10.237.214.21]) by orsmga006.jf.intel.com with ESMTP; 08 Feb 2023 06:38:01 -0800 From: Bruce Richardson To: dev@dpdk.org Cc: Huisong Li , Chengwen Feng , =?utf-8?q?Morten_Br=C3=B8rup?= , Bruce Richardson Subject: [PATCH] telemetry: rework code to avoid compiler warnings Date: Wed, 8 Feb 2023 14:37:42 +0000 Message-Id: <20230208143742.493533-1-bruce.richardson@intel.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org When printing values as hex strings, the telemetry code temporarily disabled warnings about non-literal format strings. This was because the actual format string was built-up programmatically to ensure the output was of the desired bitwidth. However, this code can be reworked and shortened by taking advantage of the "*" printf flag, which is used to specify that the output width is given by a separate printf parameter. This allows the format to be a literal string in all cases, and also allows the code in the function to be shortened considerably. Note: the type of the width should be an "int" variable, which is why this patch changes the type of the existing variable. Also, while we could shorten the format string by using the "#" flag in place of an explicit "0x", this would make the code more confusing because it would mean that the "0x" would be included in the specified with, forcing us to add 2 to the existing computed width. Signed-off-by: Bruce Richardson Acked-by: Morten Brørup Signed-off-by: Bruce Richardson Acked-by: Keith Wiles Acked-by: Chengwen Feng Tested-by: Huisong Li --- lib/telemetry/telemetry_data.c | 38 +++++++--------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/lib/telemetry/telemetry_data.c b/lib/telemetry/telemetry_data.c index eb3cb98176..2bac2de2c2 100644 --- a/lib/telemetry/telemetry_data.c +++ b/lib/telemetry/telemetry_data.c @@ -119,45 +119,21 @@ rte_tel_data_add_array_container(struct rte_tel_data *d, return 0; } -/* To suppress compiler warning about format string. */ -#if defined(RTE_TOOLCHAIN_GCC) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-nonliteral" -#elif defined(RTE_TOOLCHAIN_CLANG) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wformat-nonliteral" -#endif - static int rte_tel_uint_to_hex_encoded_str(char *buf, size_t buf_len, uint64_t val, uint8_t display_bitwidth) { -#define RTE_TEL_HEX_FORMAT_LEN 16 - - uint8_t spec_hex_width = (display_bitwidth + 3) / 4; - char format[RTE_TEL_HEX_FORMAT_LEN]; - - if (display_bitwidth != 0) { - if (snprintf(format, RTE_TEL_HEX_FORMAT_LEN, "0x%%0%u" PRIx64, - spec_hex_width) >= RTE_TEL_HEX_FORMAT_LEN) - return -EINVAL; + int spec_hex_width = (display_bitwidth + 3) / 4; + int len; - if (snprintf(buf, buf_len, format, val) >= (int)buf_len) - return -EINVAL; - } else { - if (snprintf(buf, buf_len, "0x%" PRIx64, val) >= (int)buf_len) - return -EINVAL; - } + if (display_bitwidth != 0) + len = snprintf(buf, buf_len, "0x%0*" PRIx64, spec_hex_width, val); + else + len = snprintf(buf, buf_len, "0x%" PRIx64, val); - return 0; + return len < (int)buf_len ? 0 : -EINVAL; } -#if defined(RTE_TOOLCHAIN_GCC) -#pragma GCC diagnostic pop -#elif defined(RTE_TOOLCHAIN_CLANG) -#pragma clang diagnostic pop -#endif - int rte_tel_data_add_array_uint_hex(struct rte_tel_data *d, uint64_t val, uint8_t display_bitwidth)