[10/21] lib/eal: replace strtok with strtok_r

Message ID 20231113104550.2138654-11-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>
---
 lib/eal/common/eal_common_memory.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
  

Comments

Stephen Hemminger Nov. 13, 2023, 4:27 p.m. UTC | #1
On Mon, 13 Nov 2023 18:45:39 +0800
Jie Hai <haijie1@huawei.com> wrote:

> 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.

This code is only called in startup and can not be called by
multiple threads.
  
fengchengwen Nov. 14, 2023, 1:05 a.m. UTC | #2
Hi Stephen,

On 2023/11/14 0:27, Stephen Hemminger wrote:
> On Mon, 13 Nov 2023 18:45:39 +0800
> Jie Hai <haijie1@huawei.com> wrote:
> 
>> 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.
> 
> This code is only called in startup and can not be called by
> multiple threads.

For the DPDK examples and apps, I think it's OK if it called in startup then may not need replace.

But as for lib and drivers, I think we shouldn't make such an assumption.

At last I also recommend fix the examples and apps, because people may refer it.

Thanks
Chengwen

> 
> .
>
  
Stephen Hemminger Nov. 14, 2023, 1:08 a.m. UTC | #3
On Tue, 14 Nov 2023 09:05:10 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> Hi Stephen,
> 
> On 2023/11/14 0:27, Stephen Hemminger wrote:
> > On Mon, 13 Nov 2023 18:45:39 +0800
> > Jie Hai <haijie1@huawei.com> wrote:
> >   
> >> 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.  
> > 
> > This code is only called in startup and can not be called by
> > multiple threads.  
> 
> For the DPDK examples and apps, I think it's OK if it called in startup then may not need replace.
> 
> But as for lib and drivers, I think we shouldn't make such an assumption.
> 
> At last I also recommend fix the examples and apps, because people may refer it.

For startup of libs and driver initialization the code is always single threaded.
If not many, many other things would break.
  

Patch

diff --git a/lib/eal/common/eal_common_memory.c b/lib/eal/common/eal_common_memory.c
index d9433db62345..a05eb0442b0b 100644
--- a/lib/eal/common/eal_common_memory.c
+++ b/lib/eal/common/eal_common_memory.c
@@ -1273,22 +1273,22 @@  parse_params(const char *params, uint32_t *vals, size_t n_vals)
 	char dlim[2] = ",";
 	char *params_args;
 	size_t count = 0;
-	char *token;
+	char *token, *sp = NULL;
 
 	if (vals == NULL || params == NULL || strlen(params) == 0)
 		return -1;
 
-	/* strtok expects char * and param is const char *. Hence on using
+	/* strtok_r expects char * and param is const char *. Hence on using
 	 * params as "const char *" compiler throws warning.
 	 */
 	params_args = strdup(params);
 	if (params_args == NULL)
 		return -1;
 
-	token = strtok(params_args, dlim);
+	token = strtok_r(params_args, dlim, &sp);
 	while (token && isdigit(*token) && count < n_vals) {
 		vals[count++] = strtoul(token, NULL, 10);
-		token = strtok(NULL, dlim);
+		token = strtok_r(NULL, dlim, &sp);
 	}
 
 	free(params_args);