[08/21] app/test-mldev: replace strtok with strtok_r

Message ID 20231113104550.2138654-9-haijie1@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series replace strtok with strtok_r |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Jie Hai Nov. 13, 2023, 10:45 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 function.

Signed-off-by: Jie Hai <haijie1@huawei.com>
---
 app/test-mldev/ml_options.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
  

Patch

diff --git a/app/test-mldev/ml_options.c b/app/test-mldev/ml_options.c
index 72357c1c393d..3f3dc8f69890 100644
--- a/app/test-mldev/ml_options.c
+++ b/app/test-mldev/ml_options.c
@@ -75,12 +75,12 @@  ml_parse_models(struct ml_options *opt, const char *arg)
 {
 	const char *delim = ",";
 	char models[PATH_MAX];
-	char *token;
+	char *token, *sp = NULL;
 	int ret = 0;
 
 	strlcpy(models, arg, PATH_MAX);
 
-	token = strtok(models, delim);
+	token = strtok_r(models, delim, &sp);
 	while (token != NULL) {
 		strlcpy(opt->filelist[opt->nb_filelist].model, token, PATH_MAX);
 		opt->nb_filelist++;
@@ -90,7 +90,7 @@  ml_parse_models(struct ml_options *opt, const char *arg)
 			ret = -EINVAL;
 			break;
 		}
-		token = strtok(NULL, delim);
+		token = strtok_r(NULL, delim, &sp);
 	}
 
 	if (opt->nb_filelist == 0) {
@@ -106,7 +106,7 @@  ml_parse_filelist(struct ml_options *opt, const char *arg)
 {
 	const char *delim = ",";
 	char filelist[PATH_MAX];
-	char *token;
+	char *token, *sp = NULL;
 
 	if (opt->nb_filelist >= ML_TEST_MAX_MODELS) {
 		ml_err("Exceeded filelist count, max = %d\n", ML_TEST_MAX_MODELS);
@@ -116,7 +116,7 @@  ml_parse_filelist(struct ml_options *opt, const char *arg)
 	strlcpy(filelist, arg, PATH_MAX);
 
 	/* model */
-	token = strtok(filelist, delim);
+	token = strtok_r(filelist, delim, &sp);
 	if (token == NULL) {
 		ml_err("Invalid filelist, model not specified = %s\n", arg);
 		return -EINVAL;
@@ -124,7 +124,7 @@  ml_parse_filelist(struct ml_options *opt, const char *arg)
 	strlcpy(opt->filelist[opt->nb_filelist].model, token, PATH_MAX);
 
 	/* input */
-	token = strtok(NULL, delim);
+	token = strtok_r(NULL, delim, &sp);
 	if (token == NULL) {
 		ml_err("Invalid filelist, input not specified = %s\n", arg);
 		return -EINVAL;
@@ -132,7 +132,7 @@  ml_parse_filelist(struct ml_options *opt, const char *arg)
 	strlcpy(opt->filelist[opt->nb_filelist].input, token, PATH_MAX);
 
 	/* output */
-	token = strtok(NULL, delim);
+	token = strtok_r(NULL, delim, &sp);
 	if (token == NULL) {
 		ml_err("Invalid filelist, output not specified = %s\n", arg);
 		return -EINVAL;
@@ -140,14 +140,14 @@  ml_parse_filelist(struct ml_options *opt, const char *arg)
 	strlcpy(opt->filelist[opt->nb_filelist].output, token, PATH_MAX);
 
 	/* reference - optional */
-	token = strtok(NULL, delim);
+	token = strtok_r(NULL, delim, &sp);
 	if (token != NULL)
 		strlcpy(opt->filelist[opt->nb_filelist].reference, token, PATH_MAX);
 	else
 		memset(opt->filelist[opt->nb_filelist].reference, 0, PATH_MAX);
 
 	/* check for extra tokens */
-	token = strtok(NULL, delim);
+	token = strtok_r(NULL, delim, &sp);
 	if (token != NULL) {
 		ml_err("Invalid filelist. Entries > 4\n.");
 		return -EINVAL;