DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] graph: replace strtok with strtok_r
@ 2024-09-12  0:55 Stephen Hemminger
  2024-09-12  1:29 ` fengchengwen
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen Hemminger @ 2024-09-12  0:55 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger, Sunil Kumar Kori, Rakesh Kudurumalla

The function strtok is not thread safe, better to use strtok_r.
This patch was found by running semgrep on the DPDK repository.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/graph/graph.c |  6 +++---
 app/graph/utils.c | 18 +++++++++---------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/app/graph/graph.c b/app/graph/graph.c
index 17717738d7..3af031cbaf 100644
--- a/app/graph/graph.c
+++ b/app/graph/graph.c
@@ -103,9 +103,9 @@ parser_usecases_read(char *usecases)
 {
 	bool valid = false;
 	uint32_t i, j = 0;
-	char *token;
+	char *token, *saveptr = NULL;
 
-	token = strtok(usecases, ",");
+	token = strtok_r(usecases, ",", &saveptr);
 	while (token != NULL) {
 		for (i = 0; i < RTE_DIM(supported_usecases); i++) {
 			if (strcmp(supported_usecases[i], token) == 0) {
@@ -116,7 +116,7 @@ parser_usecases_read(char *usecases)
 				break;
 			}
 		}
-		token = strtok(NULL, ",");
+		token = strtok_r(NULL, ",", &saveptr);
 	}
 
 	return valid;
diff --git a/app/graph/utils.c b/app/graph/utils.c
index 3e8099ea88..5b1f865dd8 100644
--- a/app/graph/utils.c
+++ b/app/graph/utils.c
@@ -95,12 +95,12 @@ parser_ip4_read(uint32_t *value, char *p)
 {
 	uint8_t shift = 24;
 	uint32_t ip = 0;
-	char *token;
+	char *token, *saveptr = NULL;
 
-	token = strtok(p, ".");
+	token = strtok_r(p, ".", &saveptr);
 	while (token != NULL) {
 		ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
-		token = strtok(NULL, ".");
+		token = strtok_r(NULL, ".", &saveptr);
 		shift -= 8;
 	}
 
@@ -113,13 +113,13 @@ int
 parser_ip6_read(uint8_t *value, char *p)
 {
 	uint64_t val = 0;
-	char *token;
+	char *token, *saveptr = NULL;
 
-	token = strtok(p, ":");
+	token = strtok_r(p, ":", &saveptr);
 	while (token != NULL) {
 		hex_string_to_uint64(&val, token);
 		*value = val;
-		token = strtok(NULL, ":");
+		token = strtok_r(NULL, ":", &saveptr);
 		value++;
 		val = 0;
 	}
@@ -132,13 +132,13 @@ parser_mac_read(uint64_t *value, char *p)
 {
 	uint64_t mac = 0, val = 0;
 	uint8_t shift = 40;
-	char *token;
+	char *token, *saveptr = NULL;
 
-	token = strtok(p, ":");
+	token = strtok_r(p, ":", &saveptr);
 	while (token != NULL) {
 		hex_string_to_uint64(&val, token);
 		mac |= val << shift;
-		token = strtok(NULL, ":");
+		token = strtok_r(NULL, ":", &saveptr);
 		shift -= 8;
 		val = 0;
 	}
-- 
2.45.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] graph: replace strtok with strtok_r
  2024-09-12  0:55 [PATCH] graph: replace strtok with strtok_r Stephen Hemminger
@ 2024-09-12  1:29 ` fengchengwen
  0 siblings, 0 replies; 2+ messages in thread
From: fengchengwen @ 2024-09-12  1:29 UTC (permalink / raw)
  To: Stephen Hemminger, dev; +Cc: Sunil Kumar Kori, Rakesh Kudurumalla

Acked-by: Chengwen Feng <fengchengwen@huawei.com>

There are many strtok invoking in DPDK current, and Hai Jie already submitted a patchset [1], and it seemed interrupted.
Perhaps it's time to re-submit.

[1] https://inbox.dpdk.org/dev/20231114110006.91148-1-haijie1@huawei.com/

On 2024/9/12 8:55, Stephen Hemminger wrote:
> The function strtok is not thread safe, better to use strtok_r.
> This patch was found by running semgrep on the DPDK repository.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  app/graph/graph.c |  6 +++---
>  app/graph/utils.c | 18 +++++++++---------
>  2 files changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/app/graph/graph.c b/app/graph/graph.c
> index 17717738d7..3af031cbaf 100644
> --- a/app/graph/graph.c
> +++ b/app/graph/graph.c
> @@ -103,9 +103,9 @@ parser_usecases_read(char *usecases)
>  {
>  	bool valid = false;
>  	uint32_t i, j = 0;
> -	char *token;
> +	char *token, *saveptr = NULL;
>  
> -	token = strtok(usecases, ",");
> +	token = strtok_r(usecases, ",", &saveptr);
>  	while (token != NULL) {
>  		for (i = 0; i < RTE_DIM(supported_usecases); i++) {
>  			if (strcmp(supported_usecases[i], token) == 0) {
> @@ -116,7 +116,7 @@ parser_usecases_read(char *usecases)
>  				break;
>  			}
>  		}
> -		token = strtok(NULL, ",");
> +		token = strtok_r(NULL, ",", &saveptr);
>  	}
>  
>  	return valid;
> diff --git a/app/graph/utils.c b/app/graph/utils.c
> index 3e8099ea88..5b1f865dd8 100644
> --- a/app/graph/utils.c
> +++ b/app/graph/utils.c
> @@ -95,12 +95,12 @@ parser_ip4_read(uint32_t *value, char *p)
>  {
>  	uint8_t shift = 24;
>  	uint32_t ip = 0;
> -	char *token;
> +	char *token, *saveptr = NULL;
>  
> -	token = strtok(p, ".");
> +	token = strtok_r(p, ".", &saveptr);
>  	while (token != NULL) {
>  		ip |= (((uint32_t)strtoul(token, NULL, 10)) << shift);
> -		token = strtok(NULL, ".");
> +		token = strtok_r(NULL, ".", &saveptr);
>  		shift -= 8;
>  	}
>  
> @@ -113,13 +113,13 @@ int
>  parser_ip6_read(uint8_t *value, char *p)
>  {
>  	uint64_t val = 0;
> -	char *token;
> +	char *token, *saveptr = NULL;
>  
> -	token = strtok(p, ":");
> +	token = strtok_r(p, ":", &saveptr);
>  	while (token != NULL) {
>  		hex_string_to_uint64(&val, token);
>  		*value = val;
> -		token = strtok(NULL, ":");
> +		token = strtok_r(NULL, ":", &saveptr);
>  		value++;
>  		val = 0;
>  	}
> @@ -132,13 +132,13 @@ parser_mac_read(uint64_t *value, char *p)
>  {
>  	uint64_t mac = 0, val = 0;
>  	uint8_t shift = 40;
> -	char *token;
> +	char *token, *saveptr = NULL;
>  
> -	token = strtok(p, ":");
> +	token = strtok_r(p, ":", &saveptr);
>  	while (token != NULL) {
>  		hex_string_to_uint64(&val, token);
>  		mac |= val << shift;
> -		token = strtok(NULL, ":");
> +		token = strtok_r(NULL, ":", &saveptr);
>  		shift -= 8;
>  		val = 0;
>  	}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-09-12  1:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-12  0:55 [PATCH] graph: replace strtok with strtok_r Stephen Hemminger
2024-09-12  1:29 ` fengchengwen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).