[v5,07/25] app/test-mldev: replace strtok with reentrant version

Message ID 20241108110404.18317-8-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: bbd272edcb14 ("app/mldev: add ordered inferences")
Fixes: 28a4a819c850 ("app/mldev: improve checks for invalid options")
Fixes: da6793390596 ("app/mldev: support inference validation")
Fixes: f6661e6d9a3a ("app/mldev: validate model operations")

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

Patch

diff --git a/app/test-mldev/ml_options.c b/app/test-mldev/ml_options.c
index 320f6325ae67..1033444de0e1 100644
--- a/app/test-mldev/ml_options.c
+++ b/app/test-mldev/ml_options.c
@@ -9,6 +9,7 @@ 
 #include <rte_memory.h>
 #include <rte_mldev.h>
 #include <rte_string_fns.h>
+#include <rte_os_shim.h>
 
 #include "ml_common.h"
 #include "ml_test.h"
@@ -76,12 +77,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) {
 		if (opt->nb_filelist >= ML_TEST_MAX_MODELS) {
 			ml_err("Exceeded model count, max = %d\n", ML_TEST_MAX_MODELS);
@@ -92,7 +93,7 @@  ml_parse_models(struct ml_options *opt, const char *arg)
 		strlcpy(opt->filelist[opt->nb_filelist].model, token, PATH_MAX);
 		opt->nb_filelist++;
 
-		token = strtok(NULL, delim);
+		token = strtok_r(NULL, delim, &sp);
 	}
 
 	if (opt->nb_filelist == 0) {
@@ -108,7 +109,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);
@@ -118,7 +119,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;
@@ -126,7 +127,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;
@@ -134,7 +135,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;
@@ -142,14 +143,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;