[v5,01/25] app/graph: replace strtok with reentrant version

Message ID 20241108110404.18317-2-haijie1@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series [v5,01/25] app/graph: replace strtok with reentrant version |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Jie Hai Nov. 8, 2024, 11:03 a.m. UTC
Multiple threads calling the same function may cause condition
race issues, which often leads to abnormal behavior and can cause
more serious vulnerabilities such as abnormal termination, denial
of service, and compromised data integrity.

The strtok() is non-reentrant, it is better to replace it with a
reentrant version.

Fixes: 5c59002a34f3 ("app/graph: add graph commands")
Fixes: 984a315a5804 ("app/graph: add parser utility")

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/graph/graph.c |  6 ++++--
 app/graph/utils.c | 16 ++++++++++------
 2 files changed, 14 insertions(+), 8 deletions(-)
  

Patch

diff --git a/app/graph/graph.c b/app/graph/graph.c
index 17717738d7d9..6849b8da5da4 100644
--- a/app/graph/graph.c
+++ b/app/graph/graph.c
@@ -13,6 +13,7 @@ 
 #include <rte_ethdev.h>
 #include <rte_graph_worker.h>
 #include <rte_log.h>
+#include <rte_os_shim.h>
 
 #include "graph_priv.h"
 #include "module_api.h"
@@ -103,9 +104,10 @@  parser_usecases_read(char *usecases)
 {
 	bool valid = false;
 	uint32_t i, j = 0;
+	char *sp = NULL;
 	char *token;
 
-	token = strtok(usecases, ",");
+	token = strtok_r(usecases, ",", &sp);
 	while (token != NULL) {
 		for (i = 0; i < RTE_DIM(supported_usecases); i++) {
 			if (strcmp(supported_usecases[i], token) == 0) {
@@ -116,7 +118,7 @@  parser_usecases_read(char *usecases)
 				break;
 			}
 		}
-		token = strtok(NULL, ",");
+		token = strtok_r(NULL, ",", &sp);
 	}
 
 	return valid;
diff --git a/app/graph/utils.c b/app/graph/utils.c
index 3e8099ea8821..6c53948aa277 100644
--- a/app/graph/utils.c
+++ b/app/graph/utils.c
@@ -10,6 +10,7 @@ 
 
 #include <rte_common.h>
 #include <rte_string_fns.h>
+#include <rte_os_shim.h>
 
 #include "module_api.h"
 
@@ -94,13 +95,14 @@  int
 parser_ip4_read(uint32_t *value, char *p)
 {
 	uint8_t shift = 24;
+	char *sp = NULL;
 	uint32_t ip = 0;
 	char *token;
 
-	token = strtok(p, ".");
+	token = strtok_r(p, ".", &sp);
 	while (token != NULL) {
 		ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
-		token = strtok(NULL, ".");
+		token = strtok_r(NULL, ".", &sp);
 		shift -= 8;
 	}
 
@@ -113,13 +115,14 @@  int
 parser_ip6_read(uint8_t *value, char *p)
 {
 	uint64_t val = 0;
+	char *sp = NULL;
 	char *token;
 
-	token = strtok(p, ":");
+	token = strtok_r(p, ":", &sp);
 	while (token != NULL) {
 		hex_string_to_uint64(&val, token);
 		*value = val;
-		token = strtok(NULL, ":");
+		token = strtok_r(NULL, ":", &sp);
 		value++;
 		val = 0;
 	}
@@ -132,13 +135,14 @@  parser_mac_read(uint64_t *value, char *p)
 {
 	uint64_t mac = 0, val = 0;
 	uint8_t shift = 40;
+	char *sp = NULL;
 	char *token;
 
-	token = strtok(p, ":");
+	token = strtok_r(p, ":", &sp);
 	while (token != NULL) {
 		hex_string_to_uint64(&val, token);
 		mac |= val << shift;
-		token = strtok(NULL, ":");
+		token = strtok_r(NULL, ":", &sp);
 		shift -= 8;
 		val = 0;
 	}