[PATCH v2 04/22] app/crypto-perf: replace strtok with reentrant version
Jie Hai
haijie1 at huawei.com
Tue Nov 14 09:41:15 CET 2023
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")
Cc: stable at dpdk.org
Signed-off-by: Jie Hai <haijie1 at huawei.com>
Acked-by: Chengwen Feng <fengchengwen at huawei.com>
---
app/test-crypto-perf/cperf_options_parsing.c | 16 +++++++++-------
app/test-crypto-perf/cperf_test_vector_parsing.c | 10 ++++++----
2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/app/test-crypto-perf/cperf_options_parsing.c b/app/test-crypto-perf/cperf_options_parsing.c
index 75afedc7fd6e..81fa4a925291 100644
--- a/app/test-crypto-perf/cperf_options_parsing.c
+++ b/app/test-crypto-perf/cperf_options_parsing.c
@@ -161,6 +161,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);
@@ -168,7 +169,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_s(copy_arg, ":", &sp);
/* Parse minimum value */
if (token != NULL) {
@@ -182,7 +183,7 @@ parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
} else
goto err_range;
- token = strtok(NULL, ":");
+ token = strtok_s(NULL, ":", &sp);
/* Parse increment value */
if (token != NULL) {
@@ -196,7 +197,7 @@ parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
} else
goto err_range;
- token = strtok(NULL, ":");
+ token = strtok_s(NULL, ":", &sp);
/* Parse maximum value */
if (token != NULL) {
@@ -211,7 +212,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_s(NULL, ":", &sp) != NULL)
goto err_range;
free(copy_arg);
@@ -230,6 +231,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);
@@ -237,7 +239,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_s(copy_arg, ",", &sp);
/* Parse first value */
if (token != NULL) {
@@ -253,7 +255,7 @@ parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
} else
goto err_list;
- token = strtok(NULL, ",");
+ token = strtok_s(NULL, ",", &sp);
while (token != NULL) {
if (count == MAX_LIST) {
@@ -275,7 +277,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_s(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..3557d767b55c 100644
--- a/app/test-crypto-perf/cperf_test_vector_parsing.c
+++ b/app/test-crypto-perf/cperf_test_vector_parsing.c
@@ -220,8 +220,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_s(tokens, CPERF_VALUE_DELIMITER, &sp);
if (tok == NULL)
return -1;
@@ -252,7 +253,7 @@ parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
return -1;
}
- tok = strtok(NULL, CPERF_VALUE_DELIMITER);
+ tok = strtok_s(NULL, CPERF_VALUE_DELIMITER, &sp);
if (tok == NULL)
break;
@@ -283,6 +284,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 +292,10 @@ parse_entry(char *entry, struct cperf_test_vector *vector,
}
/* get key */
- token = strtok(entry, CPERF_ENTRY_DELIMITER);
+ token = strtok_s(entry, CPERF_ENTRY_DELIMITER, &sp);
key_token = token;
/* get values for key */
- token = strtok(NULL, CPERF_ENTRY_DELIMITER);
+ token = strtok_s(NULL, CPERF_ENTRY_DELIMITER, &sp);
if (key_token == NULL || token == NULL) {
printf("Expected 'key = values' but was '%.40s'..\n", entry);
--
2.30.0
More information about the dev
mailing list