[v5,04/25] app/crypto-perf: replace strtok with reentrant version

Message ID 20241108110404.18317-5-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: f6cefe253cc8 ("app/crypto-perf: add range/list of sizes")
Fixes: f8be1786b1b8 ("app/crypto-perf: introduce performance test application")

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
Acked-by: Ciara Power <ciara.power@intel.com>
---
 app/test-crypto-perf/cperf_options_parsing.c    | 17 ++++++++++-------
 .../cperf_test_vector_parsing.c                 | 11 +++++++----
 2 files changed, 17 insertions(+), 11 deletions(-)
  

Patch

diff --git a/app/test-crypto-perf/cperf_options_parsing.c b/app/test-crypto-perf/cperf_options_parsing.c
index 8abee2d68826..07130e2c27f7 100644
--- a/app/test-crypto-perf/cperf_options_parsing.c
+++ b/app/test-crypto-perf/cperf_options_parsing.c
@@ -9,6 +9,7 @@ 
 #include <rte_cryptodev.h>
 #include <rte_malloc.h>
 #include <rte_ether.h>
+#include <rte_os_shim.h>
 
 #include "cperf_options.h"
 #include "cperf_test_common.h"
@@ -166,6 +167,7 @@  parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
 {
 	char *token;
 	uint32_t number;
+	char *sp = NULL;
 
 	char *copy_arg = strdup(arg);
 
@@ -173,7 +175,7 @@  parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
 		return -1;
 
 	errno = 0;
-	token = strtok(copy_arg, ":");
+	token = strtok_r(copy_arg, ":", &sp);
 
 	/* Parse minimum value */
 	if (token != NULL) {
@@ -187,7 +189,7 @@  parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
 	} else
 		goto err_range;
 
-	token = strtok(NULL, ":");
+	token = strtok_r(NULL, ":", &sp);
 
 	/* Parse increment value */
 	if (token != NULL) {
@@ -201,7 +203,7 @@  parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
 	} else
 		goto err_range;
 
-	token = strtok(NULL, ":");
+	token = strtok_r(NULL, ":", &sp);
 
 	/* Parse maximum value */
 	if (token != NULL) {
@@ -216,7 +218,7 @@  parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
 	} else
 		goto err_range;
 
-	if (strtok(NULL, ":") != NULL)
+	if (strtok_r(NULL, ":", &sp) != NULL)
 		goto err_range;
 
 	free(copy_arg);
@@ -235,6 +237,7 @@  parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
 	uint8_t count = 0;
 	uint32_t temp_min;
 	uint32_t temp_max;
+	char *sp = NULL;
 
 	char *copy_arg = strdup(arg);
 
@@ -242,7 +245,7 @@  parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
 		return -1;
 
 	errno = 0;
-	token = strtok(copy_arg, ",");
+	token = strtok_r(copy_arg, ",", &sp);
 
 	/* Parse first value */
 	if (token != NULL) {
@@ -258,7 +261,7 @@  parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
 	} else
 		goto err_list;
 
-	token = strtok(NULL, ",");
+	token = strtok_r(NULL, ",", &sp);
 
 	while (token != NULL) {
 		if (count == MAX_LIST) {
@@ -280,7 +283,7 @@  parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
 		if (number > temp_max)
 			temp_max = number;
 
-		token = strtok(NULL, ",");
+		token = strtok_r(NULL, ",", &sp);
 	}
 
 	if (min)
diff --git a/app/test-crypto-perf/cperf_test_vector_parsing.c b/app/test-crypto-perf/cperf_test_vector_parsing.c
index 737d61d4af6b..b1cc1ef3c91d 100644
--- a/app/test-crypto-perf/cperf_test_vector_parsing.c
+++ b/app/test-crypto-perf/cperf_test_vector_parsing.c
@@ -9,6 +9,7 @@ 
 #include <stdlib.h>
 
 #include <rte_malloc.h>
+#include <rte_os_shim.h>
 
 #include "cperf_options.h"
 #include "cperf_test_vectors.h"
@@ -220,8 +221,9 @@  parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
 
 	uint8_t *values, *values_resized;
 	char *tok, *error = NULL;
+	char *sp = NULL;
 
-	tok = strtok(tokens, CPERF_VALUE_DELIMITER);
+	tok = strtok_r(tokens, CPERF_VALUE_DELIMITER, &sp);
 	if (tok == NULL)
 		return -1;
 
@@ -252,7 +254,7 @@  parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
 			return -1;
 		}
 
-		tok = strtok(NULL, CPERF_VALUE_DELIMITER);
+		tok = strtok_r(NULL, CPERF_VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			break;
 
@@ -283,6 +285,7 @@  parse_entry(char *entry, struct cperf_test_vector *vector,
 
 	uint8_t *data = NULL;
 	char *token, *key_token;
+	char *sp = NULL;
 
 	if (entry == NULL) {
 		printf("Expected entry value\n");
@@ -290,10 +293,10 @@  parse_entry(char *entry, struct cperf_test_vector *vector,
 	}
 
 	/* get key */
-	token = strtok(entry, CPERF_ENTRY_DELIMITER);
+	token = strtok_r(entry, CPERF_ENTRY_DELIMITER, &sp);
 	key_token = token;
 	/* get values for key */
-	token = strtok(NULL, CPERF_ENTRY_DELIMITER);
+	token = strtok_r(NULL, CPERF_ENTRY_DELIMITER, &sp);
 
 	if (key_token == NULL || token == NULL) {
 		printf("Expected 'key = values' but was '%.40s'..\n", entry);