[v5,03/25] app/compress-perf: replace strtok with reentrant version

Message ID 20241108110404.18317-4-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: e0b6287c035d ("app/compress-perf: add parser")

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 .../comp_perf_options_parse.c                   | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
  

Patch

diff --git a/app/test-compress-perf/comp_perf_options_parse.c b/app/test-compress-perf/comp_perf_options_parse.c
index 6d8c370fc2ea..6f3f907162e6 100644
--- a/app/test-compress-perf/comp_perf_options_parse.c
+++ b/app/test-compress-perf/comp_perf_options_parse.c
@@ -12,6 +12,7 @@ 
 
 #include <rte_string_fns.h>
 #include <rte_comp.h>
+#include <rte_os_shim.h>
 
 #include "comp_perf_options.h"
 
@@ -177,6 +178,7 @@  parse_range(const char *arg, uint8_t *min, uint8_t *max, uint8_t *inc)
 {
 	char *token;
 	uint8_t number;
+	char *sp = NULL;
 
 	char *copy_arg = strdup(arg);
 
@@ -184,7 +186,7 @@  parse_range(const char *arg, uint8_t *min, uint8_t *max, uint8_t *inc)
 		return -1;
 
 	errno = 0;
-	token = strtok(copy_arg, ":");
+	token = strtok_r(copy_arg, ":", &sp);
 
 	/* Parse minimum value */
 	if (token != NULL) {
@@ -197,7 +199,7 @@  parse_range(const char *arg, uint8_t *min, uint8_t *max, uint8_t *inc)
 	} else
 		goto err_range;
 
-	token = strtok(NULL, ":");
+	token = strtok_r(NULL, ":", &sp);
 
 	/* Parse increment value */
 	if (token != NULL) {
@@ -211,7 +213,7 @@  parse_range(const char *arg, uint8_t *min, uint8_t *max, uint8_t *inc)
 	} else
 		goto err_range;
 
-	token = strtok(NULL, ":");
+	token = strtok_r(NULL, ":", &sp);
 
 	/* Parse maximum value */
 	if (token != NULL) {
@@ -225,7 +227,7 @@  parse_range(const char *arg, uint8_t *min, uint8_t *max, uint8_t *inc)
 	} else
 		goto err_range;
 
-	if (strtok(NULL, ":") != NULL)
+	if (strtok_r(NULL, ":", &sp) != NULL)
 		goto err_range;
 
 	free(copy_arg);
@@ -244,6 +246,7 @@  parse_list(const char *arg, uint8_t *list, uint8_t *min, uint8_t *max)
 	uint8_t count = 0;
 	uint32_t temp_min;
 	uint32_t temp_max;
+	char *sp = NULL;
 
 	char *copy_arg = strdup(arg);
 
@@ -251,7 +254,7 @@  parse_list(const char *arg, uint8_t *list, uint8_t *min, uint8_t *max)
 		return -1;
 
 	errno = 0;
-	token = strtok(copy_arg, ",");
+	token = strtok_r(copy_arg, ",", &sp);
 
 	/* Parse first value */
 	if (token != NULL) {
@@ -266,7 +269,7 @@  parse_list(const char *arg, uint8_t *list, uint8_t *min, uint8_t *max)
 	} else
 		goto err_list;
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 
 	while (token != NULL) {
 		if (count == MAX_LIST) {
@@ -288,7 +291,7 @@  parse_list(const char *arg, uint8_t *list, uint8_t *min, uint8_t *max)
 		if (number > temp_max)
 			temp_max = number;
 
-		token = strtok(NULL, ",");
+		token = strtok_r(NULL, ",", &sp);
 	}
 
 	if (min)