[v3,05/22] app/dma-perf: replace strtok with reentrant version

Message ID 20231114110006.91148-6-haijie1@huawei.com (mailing list archive)
State Changes Requested, archived
Delegated to: David Marchand
Headers
Series replace strtok with reentrant version |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Jie Hai Nov. 14, 2023, 10:59 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: 623dc9364dc6 ("app/dma-perf: introduce DMA performance test")
Cc: stable@dpdk.org

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test-dma-perf/main.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
  

Patch

diff --git a/app/test-dma-perf/main.c b/app/test-dma-perf/main.c
index e5bccc27da5e..38780939578e 100644
--- a/app/test-dma-perf/main.c
+++ b/app/test-dma-perf/main.c
@@ -164,6 +164,7 @@  parse_lcore(struct test_configure *test_case, const char *value)
 	uint16_t len;
 	char *input;
 	struct lcore_dma_map_t *lcore_dma_map;
+	char *sp = NULL;
 
 	if (test_case == NULL || value == NULL)
 		return -1;
@@ -175,7 +176,7 @@  parse_lcore(struct test_configure *test_case, const char *value)
 
 	memset(lcore_dma_map, 0, sizeof(struct lcore_dma_map_t));
 
-	char *token = strtok(input, ", ");
+	char *token = strtok_r(input, ", ", &sp);
 	while (token != NULL) {
 		if (lcore_dma_map->cnt >= MAX_LCORE_NB) {
 			free(input);
@@ -185,7 +186,7 @@  parse_lcore(struct test_configure *test_case, const char *value)
 		uint16_t lcore_id = atoi(token);
 		lcore_dma_map->lcores[lcore_dma_map->cnt++] = lcore_id;
 
-		token = strtok(NULL, ", ");
+		token = strtok_r(NULL, ", ", &sp);
 	}
 
 	free(input);
@@ -201,6 +202,7 @@  parse_lcore_dma(struct test_configure *test_case, const char *value)
 	char *start, *end, *substr;
 	uint16_t lcore_id;
 	int ret = 0;
+	char *sp = NULL;
 
 	if (test_case == NULL || value == NULL)
 		return -1;
@@ -216,7 +218,7 @@  parse_lcore_dma(struct test_configure *test_case, const char *value)
 		goto out;
 	}
 
-	substr = strtok(addrs, ",");
+	substr = strtok_r(addrs, ",", &sp);
 	if (substr == NULL) {
 		fprintf(stderr, "No input DMA address\n");
 		ret = -1;
@@ -258,7 +260,7 @@  parse_lcore_dma(struct test_configure *test_case, const char *value)
 		strlcpy(lcore_dma_map->dma_names[lcore_dma_map->cnt], ptrs[1],
 				RTE_DEV_NAME_MAX_LEN);
 		lcore_dma_map->cnt++;
-		substr = strtok(NULL, ",");
+		substr = strtok_r(NULL, ",", &sp);
 	} while (substr != NULL);
 
 out:
@@ -486,6 +488,7 @@  main(int argc, char *argv[])
 	char *rst_path_ptr = NULL;
 	char rst_path[PATH_MAX];
 	int new_argc;
+	char *sp = NULL;
 
 	memset(args, 0, sizeof(args));
 
@@ -504,7 +507,7 @@  main(int argc, char *argv[])
 	}
 	if (rst_path_ptr == NULL) {
 		strlcpy(rst_path, cfg_path_ptr, PATH_MAX);
-		char *token = strtok(basename(rst_path), ".");
+		char *token = strtok_r(basename(rst_path), ".", &sp);
 		if (token == NULL) {
 			printf("Config file error.\n");
 			return -1;