[v5,02/25] app/bbdev: replace strtok with reentrant version

Message ID 20241108110404.18317-3-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: 0acdb9866756 ("test/bbdev: add FFT operations cases")
Fixes: f714a18885a6 ("app/testbbdev: add test application for bbdev")

Signed-off-by: Jie Hai <haijie1@huawei.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test-bbdev/test_bbdev_vector.c | 42 +++++++++++++++++-------------
 1 file changed, 24 insertions(+), 18 deletions(-)
  

Patch

diff --git a/app/test-bbdev/test_bbdev_vector.c b/app/test-bbdev/test_bbdev_vector.c
index 8b32850982c3..fbbdde70bdfc 100644
--- a/app/test-bbdev/test_bbdev_vector.c
+++ b/app/test-bbdev/test_bbdev_vector.c
@@ -10,6 +10,7 @@ 
 #include <stdlib.h>
 #include <stdbool.h>
 #include <rte_malloc.h>
+#include <rte_os_shim.h>
 
 #include "test_bbdev_vector.h"
 
@@ -63,8 +64,9 @@  parse_values(char *tokens, uint32_t **data, uint32_t *data_length)
 
 	uint32_t *values, *values_resized;
 	char *tok, *error = NULL;
+	char *sp = NULL;
 
-	tok = strtok(tokens, VALUE_DELIMITER);
+	tok = strtok_r(tokens, VALUE_DELIMITER, &sp);
 	if (tok == NULL)
 		return -1;
 
@@ -98,7 +100,7 @@  parse_values(char *tokens, uint32_t **data, uint32_t *data_length)
 
 		*data_length = *data_length + (strlen(tok) - strlen("0x"))/2;
 
-		tok = strtok(NULL, VALUE_DELIMITER);
+		tok = strtok_r(NULL, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			break;
 
@@ -324,8 +326,9 @@  parse_turbo_flags(char *tokens, uint32_t *op_flags,
 {
 	char *tok = NULL;
 	uint32_t op_flag_value = 0;
+	char *sp = NULL;
 
-	tok = strtok(tokens, VALUE_DELIMITER);
+	tok = strtok_r(tokens, VALUE_DELIMITER, &sp);
 	if (tok == NULL)
 		return -1;
 
@@ -359,7 +362,7 @@  parse_turbo_flags(char *tokens, uint32_t *op_flags,
 
 		*op_flags = *op_flags | op_flag_value;
 
-		tok = strtok(NULL, VALUE_DELIMITER);
+		tok = strtok_r(NULL, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			break;
 	}
@@ -399,9 +402,10 @@  static int
 parse_expected_status(char *tokens, int *status, enum rte_bbdev_op_type op_type)
 {
 	char *tok = NULL;
+	char *sp = NULL;
 	bool status_ok = false;
 
-	tok = strtok(tokens, VALUE_DELIMITER);
+	tok = strtok_r(tokens, VALUE_DELIMITER, &sp);
 	if (tok == NULL)
 		return -1;
 
@@ -432,7 +436,7 @@  parse_expected_status(char *tokens, int *status, enum rte_bbdev_op_type op_type)
 			return -1;
 		}
 
-		tok = strtok(NULL, VALUE_DELIMITER);
+		tok = strtok_r(NULL, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			break;
 	}
@@ -932,6 +936,7 @@  parse_fft_params(const char *key_token, char *token,
 	int ret = 0, status = 0, i, shift;
 	uint32_t op_flags = 0;
 	char *tok, *err = NULL;
+	char *sp = NULL;
 
 	struct rte_bbdev_op_fft *fft = &vector->fft;
 
@@ -964,7 +969,7 @@  parse_fft_params(const char *key_token, char *token,
 		fft->output_leading_depadding = (uint32_t) strtoul(token, &err, 0);
 		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
 	} else if (!strcmp(key_token, "window_index")) {
-		tok = strtok(token, VALUE_DELIMITER);
+		tok = strtok_r(token, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			return -1;
 		for (i = 0; i < FFT_WIN_SIZE; i++) {
@@ -972,7 +977,7 @@  parse_fft_params(const char *key_token, char *token,
 			fft->window_index[i / 2] |= (uint32_t) strtoul(tok, &err, 0)
 					<< shift;
 			if (i < (FFT_WIN_SIZE - 1)) {
-				tok = strtok(NULL, VALUE_DELIMITER);
+				tok = strtok_r(NULL, VALUE_DELIMITER, &sp);
 				if (tok == NULL)
 					return -1;
 			}
@@ -1016,53 +1021,53 @@  parse_fft_params(const char *key_token, char *token,
 		fft->output_depadded_size = (uint32_t) strtoul(token, &err, 0);
 		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
 	} else if (!strcmp(key_token, "cs_theta_0")) {
-		tok = strtok(token, VALUE_DELIMITER);
+		tok = strtok_r(token, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			return -1;
 		for (i = 0; i < FFT_WIN_SIZE; i++) {
 			fft->cs_theta_0[i] = (uint32_t) strtoul(tok, &err, 0);
 			if (i < (FFT_WIN_SIZE - 1)) {
-				tok = strtok(NULL, VALUE_DELIMITER);
+				tok = strtok_r(token, VALUE_DELIMITER, &sp);
 				if (tok == NULL)
 					return -1;
 			}
 		}
 		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
 	} else if (!strcmp(key_token, "cs_theta_d")) {
-		tok = strtok(token, VALUE_DELIMITER);
+		tok = strtok_r(token, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			return -1;
 		for (i = 0; i < FFT_WIN_SIZE; i++) {
 			fft->cs_theta_d[i] = (uint32_t) strtoul(tok, &err, 0);
 			if (i < (FFT_WIN_SIZE - 1)) {
-				tok = strtok(NULL, VALUE_DELIMITER);
+				tok = strtok_r(token, VALUE_DELIMITER, &sp);
 				if (tok == NULL)
 					return -1;
 			}
 		}
 		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
 	} else if (!strcmp(key_token, "time_offset")) {
-		tok = strtok(token, VALUE_DELIMITER);
+		tok = strtok_r(token, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			return -1;
 		for (i = 0; i < FFT_WIN_SIZE; i++) {
 			fft->time_offset[i] = (uint32_t) strtoul(tok, &err, 0);
 			if (i < (FFT_WIN_SIZE - 1)) {
-				tok = strtok(NULL, VALUE_DELIMITER);
+				tok = strtok_r(token, VALUE_DELIMITER, &sp);
 				if (tok == NULL)
 					return -1;
 			}
 		}
 		ret = ((err == NULL) || (*err != '\0')) ? -1 : 0;
 	} else if (!strcmp(key_token, "fft_window_width")) {
-		tok = strtok(token, VALUE_DELIMITER);
+		tok = strtok_r(token, VALUE_DELIMITER, &sp);
 		if (tok == NULL)
 			return -1;
 		for (i = 0; i < FFT_WIN_SIZE; i++) {
 			if (i == 0)
 				vector->fft_window_width_vec = (uint32_t) strtoul(tok, &err, 0);
 			if (i < (FFT_WIN_SIZE - 1)) {
-				tok = strtok(NULL, VALUE_DELIMITER);
+				tok = strtok_r(token, VALUE_DELIMITER, &sp);
 				if (tok == NULL)
 					return -1;
 			}
@@ -1163,6 +1168,7 @@  static int
 parse_entry(char *entry, struct test_bbdev_vector *vector)
 {
 	int ret = 0;
+	char *sp = NULL;
 	char *token, *key_token;
 	enum rte_bbdev_op_type op_type = RTE_BBDEV_OP_NONE;
 
@@ -1172,10 +1178,10 @@  parse_entry(char *entry, struct test_bbdev_vector *vector)
 	}
 
 	/* get key */
-	token = strtok(entry, ENTRY_DELIMITER);
+	token = strtok_r(entry, ENTRY_DELIMITER, &sp);
 	key_token = token;
 	/* get values for key */
-	token = strtok(NULL, ENTRY_DELIMITER);
+	token = strtok_r(NULL, ENTRY_DELIMITER, &sp);
 
 	if (key_token == NULL || token == NULL) {
 		printf("Expected 'key = values' but was '%.40s'..\n", entry);