From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 5358442C4A; Wed, 7 Jun 2023 09:45:54 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 096A442D33; Wed, 7 Jun 2023 09:45:32 +0200 (CEST) Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mails.dpdk.org (Postfix) with ESMTP id 286F9427F2 for ; Wed, 7 Jun 2023 09:45:27 +0200 (CEST) Received: from kwepemi500020.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4QbfQX0jpCz18Lyd; Wed, 7 Jun 2023 15:40:36 +0800 (CST) Received: from localhost.localdomain (10.69.192.56) by kwepemi500020.china.huawei.com (7.221.188.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Wed, 7 Jun 2023 15:45:23 +0800 From: Jie Hai To: Thomas Monjalon , Ferruh Yigit , Andrew Rybchenko CC: , Subject: [PATCH v2 03/13] ethdev: extract codes parsing port ID as a function Date: Wed, 7 Jun 2023 15:41:58 +0800 Message-ID: <20230607074209.4798-4-haijie1@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20230607074209.4798-1-haijie1@huawei.com> References: <20230530090510.56812-1-haijie1@huawei.com> <20230607074209.4798-1-haijie1@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemi500020.china.huawei.com (7.221.188.8) X-CFilter-Loop: Reflected 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 This patch extracts codes parsing port_id as a function. The port id of 'int' or 'unsigned long' type passing as 'uint16_t' may cause truncation, fix it. Signed-off-by: Jie Hai --- lib/ethdev/rte_ethdev_telemetry.c | 95 ++++++++++++++++--------------- lib/ethdev/sff_telemetry.c | 4 +- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/lib/ethdev/rte_ethdev_telemetry.c b/lib/ethdev/rte_ethdev_telemetry.c index ec8e48877187..5e70d0c2a66a 100644 --- a/lib/ethdev/rte_ethdev_telemetry.c +++ b/lib/ethdev/rte_ethdev_telemetry.c @@ -11,6 +11,29 @@ #include "ethdev_driver.h" #include "sff_telemetry.h" +static int +eth_dev_parse_port_params(const char *params __rte_unused, uint16_t *port_id, + char **end_param, bool has_next) +{ + uint64_t pi; + + if (params == NULL || strlen(params) == 0 || + !isdigit(*params) || port_id == NULL) + return -EINVAL; + + pi = strtoul(params, end_param, 0); + if (**end_param != '\0' && !has_next) + RTE_ETHDEV_LOG(NOTICE, + "Extra parameters passed to ethdev telemetry command, ignoring\n"); + + if (pi >= UINT16_MAX || !rte_eth_dev_is_valid_port(pi)) + return -EINVAL; + + *port_id = pi; + + return 0; +} + static int eth_dev_handle_port_list(const char *cmd __rte_unused, const char *params __rte_unused, @@ -64,14 +87,13 @@ eth_dev_handle_port_stats(const char *cmd __rte_unused, struct rte_tel_data *d) { struct rte_eth_stats stats; - int port_id, ret; - - if (params == NULL || strlen(params) == 0 || !isdigit(*params)) - return -1; + uint16_t port_id; + char *end_param; + int ret; - port_id = atoi(params); - if (!rte_eth_dev_is_valid_port(port_id)) - return -1; + ret = eth_dev_parse_port_params(params, &port_id, &end_param, false); + if (ret < 0) + return ret; ret = rte_eth_stats_get(port_id, &stats); if (ret < 0) @@ -104,17 +126,15 @@ eth_dev_handle_port_xstats(const char *cmd __rte_unused, struct rte_eth_xstat *eth_xstats; struct rte_eth_xstat_name *xstat_names; struct rte_kvargs *kvlist; - int port_id, num_xstats; bool hide_zero = false; + uint16_t port_id; char *end_param; + int num_xstats; int i, ret; - if (params == NULL || strlen(params) == 0 || !isdigit(*params)) - return -1; - - port_id = strtoul(params, &end_param, 0); - if (!rte_eth_dev_is_valid_port(port_id)) - return -1; + ret = eth_dev_parse_port_params(params, &port_id, &end_param, true); + if (ret < 0) + return ret; if (*end_param != '\0') { kvlist = rte_kvargs_parse(end_param, valid_keys); @@ -166,18 +186,13 @@ eth_dev_handle_port_dump_priv(const char *cmd __rte_unused, struct rte_tel_data *d) { char *buf, *end_param; - int port_id, ret; + uint16_t port_id; + int ret; FILE *f; - if (params == NULL || strlen(params) == 0 || !isdigit(*params)) - return -EINVAL; - - port_id = strtoul(params, &end_param, 0); - if (*end_param != '\0') - RTE_ETHDEV_LOG(NOTICE, - "Extra parameters passed to ethdev telemetry command, ignoring"); - if (!rte_eth_dev_is_valid_port(port_id)) - return -EINVAL; + ret = eth_dev_parse_port_params(params, &port_id, &end_param, false); + if (ret < 0) + return ret; buf = calloc(RTE_TEL_MAX_SINGLE_STRING_LEN, sizeof(char)); if (buf == NULL) @@ -207,19 +222,14 @@ eth_dev_handle_port_link_status(const char *cmd __rte_unused, struct rte_tel_data *d) { static const char *status_str = "status"; - int ret, port_id; struct rte_eth_link link; + uint16_t port_id; char *end_param; + int ret; - if (params == NULL || strlen(params) == 0 || !isdigit(*params)) - return -1; - - port_id = strtoul(params, &end_param, 0); - if (*end_param != '\0') - RTE_ETHDEV_LOG(NOTICE, - "Extra parameters passed to ethdev telemetry command, ignoring"); - if (!rte_eth_dev_is_valid_port(port_id)) - return -1; + ret = eth_dev_parse_port_params(params, &port_id, &end_param, false); + if (ret < 0) + return ret; ret = rte_eth_link_get_nowait(port_id, &link); if (ret < 0) @@ -246,19 +256,14 @@ eth_dev_handle_port_info(const char *cmd __rte_unused, struct rte_tel_data *rxq_state, *txq_state; char mac_addr[RTE_ETHER_ADDR_FMT_SIZE]; struct rte_eth_dev *eth_dev; + uint16_t port_id; char *end_param; - int port_id, i; - - if (params == NULL || strlen(params) == 0 || !isdigit(*params)) - return -1; + int ret; + int i; - port_id = strtoul(params, &end_param, 0); - if (*end_param != '\0') - RTE_ETHDEV_LOG(NOTICE, - "Extra parameters passed to ethdev telemetry command, ignoring"); - - if (!rte_eth_dev_is_valid_port(port_id)) - return -EINVAL; + ret = eth_dev_parse_port_params(params, &port_id, &end_param, false); + if (ret < 0) + return ret; eth_dev = &rte_eth_devices[port_id]; diff --git a/lib/ethdev/sff_telemetry.c b/lib/ethdev/sff_telemetry.c index 5923350424c0..f29e7fa882ac 100644 --- a/lib/ethdev/sff_telemetry.c +++ b/lib/ethdev/sff_telemetry.c @@ -126,7 +126,7 @@ eth_dev_handle_port_module_eeprom(const char *cmd __rte_unused, const char *para struct rte_tel_data *d) { char *end_param; - int port_id; + uint64_t port_id; if (params == NULL || strlen(params) == 0 || !isdigit(*params)) return -1; @@ -134,7 +134,7 @@ eth_dev_handle_port_module_eeprom(const char *cmd __rte_unused, const char *para errno = 0; port_id = strtoul(params, &end_param, 0); - if (errno != 0) { + if (errno != 0 || port_id >= UINT16_MAX) { RTE_ETHDEV_LOG(ERR, "Invalid argument, %d\n", errno); return -1; } -- 2.33.0