[v5,26/52] net/bonding: replace strerror with reentrant version

Message ID 20241104111037.3632161-27-huangdengdui@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series replace strerror |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

huangdengdui Nov. 4, 2024, 11:10 a.m. UTC
The function strerror() is insecure in a multi-thread environment.
This patch uses strerror_r() to replace it.

Signed-off-by: Dengdui Huang <huangdengdui@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Huisong Li <lihuisong@huawei.com>
---
 drivers/net/bonding/bonding_testpmd.c  | 18 ++++++++++++------
 drivers/net/bonding/rte_eth_bond_api.c |  5 ++++-
 drivers/net/bonding/rte_eth_bond_pmd.c | 16 ++++++++++++----
 3 files changed, 28 insertions(+), 11 deletions(-)
  

Patch

diff --git a/drivers/net/bonding/bonding_testpmd.c b/drivers/net/bonding/bonding_testpmd.c
index 8fcd6cadd0..56cca3d447 100644
--- a/drivers/net/bonding/bonding_testpmd.c
+++ b/drivers/net/bonding/bonding_testpmd.c
@@ -541,6 +541,7 @@  static void cmd_set_bond_mac_addr_parsed(void *parsed_result,
 	__rte_unused struct cmdline *cl, __rte_unused void *data)
 {
 	struct cmd_set_bond_mac_addr_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	if (port_id_is_invalid(res->port_num, ENABLED_WARN))
@@ -549,9 +550,11 @@  static void cmd_set_bond_mac_addr_parsed(void *parsed_result,
 	ret = rte_eth_bond_mac_address_set(res->port_num, &res->address);
 
 	/* check the return value and print it if is < 0 */
-	if (ret < 0)
-		fprintf(stderr, "set_bond_mac_addr error: (%s)\n",
-			strerror(-ret));
+	if (ret < 0) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "set_bond_mac_addr error: (%s)\n", errmsg);
+	}
 }
 
 static cmdline_parse_token_string_t cmd_set_bond_mac_addr_set =
@@ -597,14 +600,17 @@  static void cmd_set_bond_mon_period_parsed(void *parsed_result,
 	__rte_unused struct cmdline *cl, __rte_unused void *data)
 {
 	struct cmd_set_bond_mon_period_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	ret = rte_eth_bond_link_monitoring_set(res->port_num, res->period_ms);
 
 	/* check the return value and print it if is < 0 */
-	if (ret < 0)
-		fprintf(stderr, "set_bond_mac_addr error: (%s)\n",
-			strerror(-ret));
+	if (ret < 0) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "set_bond_mac_addr error: (%s)\n", errmsg);
+	}
 }
 
 static cmdline_parse_token_string_t cmd_set_bond_mon_period_set =
diff --git a/drivers/net/bonding/rte_eth_bond_api.c b/drivers/net/bonding/rte_eth_bond_api.c
index ffc1322047..11a37dd142 100644
--- a/drivers/net/bonding/rte_eth_bond_api.c
+++ b/drivers/net/bonding/rte_eth_bond_api.c
@@ -465,6 +465,7 @@  __eth_bond_member_add_lock_free(uint16_t bonding_port_id, uint16_t member_port_i
 	struct bond_dev_private *internals;
 	struct rte_eth_link link_props;
 	struct rte_eth_dev_info dev_info;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	bonding_eth_dev = &rte_eth_devices[bonding_port_id];
@@ -481,9 +482,11 @@  __eth_bond_member_add_lock_free(uint16_t bonding_port_id, uint16_t member_port_i
 
 	ret = rte_eth_dev_info_get(member_port_id, &dev_info);
 	if (ret != 0) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		RTE_BOND_LOG(ERR,
 			"%s: Error during getting device (port %u) info: %s",
-			__func__, member_port_id, strerror(-ret));
+			__func__, member_port_id, errmsg);
 
 		return ret;
 	}
diff --git a/drivers/net/bonding/rte_eth_bond_pmd.c b/drivers/net/bonding/rte_eth_bond_pmd.c
index 91bf2c2345..9849e0ecb0 100644
--- a/drivers/net/bonding/rte_eth_bond_pmd.c
+++ b/drivers/net/bonding/rte_eth_bond_pmd.c
@@ -165,6 +165,7 @@  bond_ethdev_8023ad_flow_verify(struct rte_eth_dev *bond_dev,
 	struct rte_eth_dev_info member_info;
 	struct rte_flow_error error;
 	struct bond_dev_private *internals = bond_dev->data->dev_private;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	const struct rte_flow_action_queue lacp_queue_conf = {
 		.index = 0,
@@ -191,9 +192,11 @@  bond_ethdev_8023ad_flow_verify(struct rte_eth_dev *bond_dev,
 
 	ret = rte_eth_dev_info_get(member_port, &member_info);
 	if (ret != 0) {
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		RTE_BOND_LOG(ERR,
 			"%s: Error during getting device (port %u) info: %s",
-			__func__, member_port, strerror(-ret));
+			__func__, member_port, errmsg);
 
 		return ret;
 	}
@@ -214,6 +217,7 @@  bond_8023ad_slow_pkt_hw_filter_supported(uint16_t port_id) {
 	struct rte_eth_dev *bond_dev = &rte_eth_devices[port_id];
 	struct bond_dev_private *internals = bond_dev->data->dev_private;
 	struct rte_eth_dev_info bond_info;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint16_t idx;
 	int ret;
 
@@ -221,10 +225,11 @@  bond_8023ad_slow_pkt_hw_filter_supported(uint16_t port_id) {
 	if (internals->member_count > 0) {
 		ret = rte_eth_dev_info_get(bond_dev->data->port_id, &bond_info);
 		if (ret != 0) {
+			if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+				snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 			RTE_BOND_LOG(ERR,
 				"%s: Error during getting device (port %u) info: %s",
-				__func__, bond_dev->data->port_id,
-				strerror(-ret));
+				__func__, bond_dev->data->port_id, errmsg);
 
 			return ret;
 		}
@@ -2264,6 +2269,7 @@  bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 {
 	struct bond_dev_private *internals = dev->data->dev_private;
 	struct bond_member_details member;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret;
 
 	uint16_t max_nb_rx_queues = UINT16_MAX;
@@ -2287,11 +2293,13 @@  bond_ethdev_info(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 			member = internals->members[idx];
 			ret = rte_eth_dev_info_get(member.port_id, &member_info);
 			if (ret != 0) {
+				if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+					snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 				RTE_BOND_LOG(ERR,
 					"%s: Error during getting device (port %u) info: %s",
 					__func__,
 					member.port_id,
-					strerror(-ret));
+					errmsg);
 
 				return ret;
 			}