[v5,34/52] net/i40e: replace strerror with reentrant version

Message ID 20241104111037.3632161-35-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/i40e/i40e_testpmd.c | 109 +++++++++++++++++++++++++-------
 1 file changed, 86 insertions(+), 23 deletions(-)
  

Patch

diff --git a/drivers/net/i40e/i40e_testpmd.c b/drivers/net/i40e/i40e_testpmd.c
index b6ef5d6e42..ef910c1cbc 100644
--- a/drivers/net/i40e/i40e_testpmd.c
+++ b/drivers/net/i40e/i40e_testpmd.c
@@ -33,6 +33,7 @@  cmd_queue_region_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_queue_region_conf region_conf;
 	enum rte_pmd_i40e_queue_region_op op_type;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -52,8 +53,10 @@  cmd_queue_region_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented or supported\n");
 		break;
 	default:
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		fprintf(stderr, "queue region config error: (%s)\n",
-			strerror(-ret));
+			errmsg);
 	}
 }
 
@@ -128,6 +131,7 @@  cmd_region_flowtype_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_queue_region_conf region_conf;
 	enum rte_pmd_i40e_queue_region_op op_type;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -147,8 +151,10 @@  cmd_region_flowtype_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented or supported\n");
 		break;
 	default:
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		fprintf(stderr, "region flowtype config error: (%s)\n",
-			strerror(-ret));
+			errmsg);
 	}
 }
 
@@ -214,6 +220,7 @@  cmd_user_priority_region_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_queue_region_conf region_conf;
 	enum rte_pmd_i40e_queue_region_op op_type;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -232,8 +239,10 @@  cmd_user_priority_region_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented or supported\n");
 		break;
 	default:
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		fprintf(stderr, "user_priority region config error: (%s)\n",
-			strerror(-ret));
+			errmsg);
 	}
 }
 
@@ -299,6 +308,7 @@  cmd_flush_queue_region_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_queue_region_conf region_conf;
 	enum rte_pmd_i40e_queue_region_op op_type;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -319,8 +329,10 @@  cmd_flush_queue_region_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented or supported\n");
 		break;
 	default:
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		fprintf(stderr, "queue region config flush error: (%s)\n",
-			strerror(-ret));
+			errmsg);
 	}
 }
 
@@ -413,6 +425,7 @@  cmd_show_queue_region_info_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_queue_regions rte_pmd_regions;
 	enum rte_pmd_i40e_queue_region_op op_type;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -432,8 +445,10 @@  cmd_show_queue_region_info_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented or supported\n");
 		break;
 	default:
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
 		fprintf(stderr, "queue region config info show error: (%s)\n",
-			strerror(-ret));
+			errmsg);
 	}
 }
 
@@ -492,6 +507,7 @@  cmd_flow_director_filter_parsed(void *parsed_result,
 			mapping[RTE_PMD_I40E_FLOW_TYPE_MAX];
 	struct rte_pmd_i40e_pkt_template_conf conf;
 	uint16_t flow_type = str_to_flowtype(res->flow_type);
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint16_t i, port = res->port_id;
 	uint8_t add;
 
@@ -534,9 +550,11 @@  cmd_flow_director_filter_parsed(void *parsed_result,
 	ret = rte_pmd_i40e_flow_add_del_packet_template(port,
 							&conf,
 							add);
-	if (ret < 0)
-		fprintf(stderr, "flow director config 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, "flow director config error: (%s)\n", errmsg);
+	}
 	close_file(conf.input.packet);
 }
 
@@ -645,6 +663,7 @@  cmd_set_vf_promisc_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_promisc_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	__rte_unused int is_on = (strcmp(res->on_off, "on") == 0) ? 1 : 0;
@@ -666,7 +685,9 @@  cmd_set_vf_promisc_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -724,6 +745,7 @@  cmd_set_vf_allmulti_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_allmulti_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	__rte_unused int is_on = (strcmp(res->on_off, "on") == 0) ? 1 : 0;
@@ -745,7 +767,9 @@  cmd_set_vf_allmulti_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -803,6 +827,7 @@  cmd_set_vf_broadcast_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_set_vf_broadcast_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	__rte_unused int is_on = (strcmp(res->on_off, "on") == 0) ? 1 : 0;
@@ -825,7 +850,9 @@  cmd_set_vf_broadcast_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -886,6 +913,7 @@  cmd_set_vf_vlan_tag_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_set_vf_vlan_tag_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	__rte_unused int is_on = (strcmp(res->on_off, "on") == 0) ? 1 : 0;
@@ -908,7 +936,9 @@  cmd_set_vf_vlan_tag_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -992,6 +1022,7 @@  cmd_vf_max_bw_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_tc_bw_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -1013,7 +1044,9 @@  cmd_vf_max_bw_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -1083,6 +1116,7 @@  cmd_vf_tc_min_bw_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_tc_bw_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint8_t tc_num;
 	uint8_t bw[16];
 	int ret = -ENOTSUP;
@@ -1108,7 +1142,9 @@  cmd_vf_tc_min_bw_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -1137,6 +1173,7 @@  cmd_vf_tc_max_bw_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_tc_bw_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -1158,7 +1195,9 @@  cmd_vf_tc_max_bw_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -1188,6 +1227,7 @@  cmd_strict_link_prio_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_vf_tc_bw_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -1207,7 +1247,9 @@  cmd_strict_link_prio_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -1912,6 +1954,7 @@  cmd_pctype_mapping_reset_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_pctype_mapping_reset_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -1928,7 +1971,9 @@  cmd_pctype_mapping_reset_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -1984,6 +2029,7 @@  cmd_pctype_mapping_get_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_flow_type_mapping
 				mapping[RTE_PMD_I40E_FLOW_TYPE_MAX];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int i, j, first_pctype;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -2000,7 +2046,9 @@  cmd_pctype_mapping_get_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		return;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 		return;
 	}
 
@@ -2085,6 +2133,7 @@  cmd_pctype_mapping_update_parsed(void *parsed_result,
 	unsigned int i;
 	unsigned int nb_item;
 	unsigned int pctype_list[RTE_PMD_I40E_PCTYPE_MAX];
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -2111,7 +2160,9 @@  cmd_pctype_mapping_update_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -2170,6 +2221,7 @@  cmd_ptype_mapping_get_parsed(void *parsed_result,
 	int ret = -ENOTSUP;
 	int max_ptype_num = 256;
 	struct rte_pmd_i40e_ptype_mapping mapping[max_ptype_num];
+	char errmsg[RTE_STRERR_BUFSIZE];
 	uint16_t count;
 	int i;
 
@@ -2191,7 +2243,9 @@  cmd_ptype_mapping_get_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 
 	if (!ret) {
@@ -2257,6 +2311,7 @@  cmd_ptype_mapping_replace_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_ptype_mapping_replace_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -2280,7 +2335,9 @@  cmd_ptype_mapping_replace_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -2331,6 +2388,7 @@  cmd_ptype_mapping_reset_parsed(void *parsed_result,
 	__rte_unused void *data)
 {
 	struct cmd_ptype_mapping_reset_result *res = parsed_result;
+	char errmsg[RTE_STRERR_BUFSIZE];
 	int ret = -ENOTSUP;
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
@@ -2347,7 +2405,9 @@  cmd_ptype_mapping_reset_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }
 
@@ -2404,6 +2464,7 @@  cmd_ptype_mapping_update_parsed(void *parsed_result,
 	struct cmd_ptype_mapping_update_result *res = parsed_result;
 	int ret = -ENOTSUP;
 	struct rte_pmd_i40e_ptype_mapping mapping;
+	char errmsg[RTE_STRERR_BUFSIZE];
 
 	if (port_id_is_invalid(res->port_id, ENABLED_WARN))
 		return;
@@ -2427,7 +2488,9 @@  cmd_ptype_mapping_update_parsed(void *parsed_result,
 		fprintf(stderr, "function not implemented\n");
 		break;
 	default:
-		fprintf(stderr, "programming error: (%s)\n", strerror(-ret));
+		if (strerror_r(-ret, errmsg, sizeof(errmsg)) != 0)
+			snprintf(errmsg, sizeof(errmsg), "Unknown error %d", -ret);
+		fprintf(stderr, "programming error: (%s)\n", errmsg);
 	}
 }