DPDK patches and discussions
 help / color / mirror / Atom feed
* Use of strtok() in dpdk code
@ 2024-10-21 18:30 Isaac Boukris
  2024-10-22  1:08 ` Stephen Hemminger
  0 siblings, 1 reply; 5+ messages in thread
From: Isaac Boukris @ 2024-10-21 18:30 UTC (permalink / raw)
  To: dev, Stephen Hemminger

Hello,

I was debugging a crash resulting from strtok() returning NULL
unexpectedly (string still had tokens and delimiters), and the only
explanation I could come up with was that strtok is thread-unsafe and
another thread could have been calling it at the same time, and so I
changed it to use strtok_r().

That said, the only other possible use of strtok() that I could find
was in the dpdk code (telemetry), which brings me to my question,
should we consider changing all occurrences to strtok_r() or am I
missing something? there seem to be quite some in non-initialization
code.

Thanks!

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

* Re: Use of strtok() in dpdk code
  2024-10-21 18:30 Use of strtok() in dpdk code Isaac Boukris
@ 2024-10-22  1:08 ` Stephen Hemminger
  2024-10-22  6:51   ` fengchengwen
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Hemminger @ 2024-10-22  1:08 UTC (permalink / raw)
  To: Isaac Boukris; +Cc: dev

On Mon, 21 Oct 2024 21:30:02 +0300
Isaac Boukris <iboukris@gmail.com> wrote:

> Hello,
> 
> I was debugging a crash resulting from strtok() returning NULL
> unexpectedly (string still had tokens and delimiters), and the only
> explanation I could come up with was that strtok is thread-unsafe and
> another thread could have been calling it at the same time, and so I
> changed it to use strtok_r().
> 
> That said, the only other possible use of strtok() that I could find
> was in the dpdk code (telemetry), which brings me to my question,
> should we consider changing all occurrences to strtok_r() or am I
> missing something? there seem to be quite some in non-initialization
> code.
> 
> Thanks!


Most of the uses are in tests and other single threaded code.
In general, simpler just to use strtok_r everywhere and not worry about it.
Similar to not using sprintf() and instead using snprintf().

Some code scanners like codeql also flag this.

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

* Re: Use of strtok() in dpdk code
  2024-10-22  1:08 ` Stephen Hemminger
@ 2024-10-22  6:51   ` fengchengwen
  2024-10-22 12:27     ` Isaac Boukris
  2024-10-22 15:25     ` Stephen Hemminger
  0 siblings, 2 replies; 5+ messages in thread
From: fengchengwen @ 2024-10-22  6:51 UTC (permalink / raw)
  To: Stephen Hemminger, Isaac Boukris, Thomas Monjalon
  Cc: dev, haijie1@huawei.com >> Jie Hai, huangdengdui

On 2024/10/22 9:08, Stephen Hemminger wrote:
> On Mon, 21 Oct 2024 21:30:02 +0300
> Isaac Boukris <iboukris@gmail.com> wrote:
> 
>> Hello,
>>
>> I was debugging a crash resulting from strtok() returning NULL
>> unexpectedly (string still had tokens and delimiters), and the only
>> explanation I could come up with was that strtok is thread-unsafe and
>> another thread could have been calling it at the same time, and so I
>> changed it to use strtok_r().
>>
>> That said, the only other possible use of strtok() that I could find
>> was in the dpdk code (telemetry), which brings me to my question,
>> should we consider changing all occurrences to strtok_r() or am I
>> missing something? there seem to be quite some in non-initialization
>> code.
>>
>> Thanks!
> 
> 
> Most of the uses are in tests and other single threaded code.
> In general, simpler just to use strtok_r everywhere and not worry about it.
> Similar to not using sprintf() and instead using snprintf().

I'm afraid I can't agree.

DPDK is just a SDK, it's not an application (although DPDK provided simple examples).
Many code will developped based on DPDK, we can't predict how it was implemented.
So there maybe a DPDK thread and a application thread both invoke strtok().

From this point of view, I hope that DPDK solves some of the reentrant problems of
such C functions (e.g. strtok()\strerror()).

Actually, we've try to solve before, but unfortunately it wasn't merged.
1\ strtok():   https://inbox.dpdk.org/dev/20231114110006.91148-1-haijie1@huawei.com/T/#u
2\ strerror(): https://inbox.dpdk.org/dev/20231114123552.398072-1-huangdengdui@huawei.com/T/#u

> 
> Some code scanners like codeql also flag this.


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

* Re: Use of strtok() in dpdk code
  2024-10-22  6:51   ` fengchengwen
@ 2024-10-22 12:27     ` Isaac Boukris
  2024-10-22 15:25     ` Stephen Hemminger
  1 sibling, 0 replies; 5+ messages in thread
From: Isaac Boukris @ 2024-10-22 12:27 UTC (permalink / raw)
  To: fengchengwen
  Cc: Stephen Hemminger, Thomas Monjalon, dev,
	haijie1@huawei.com >> Jie Hai, huangdengdui

Hi

On Tue, Oct 22, 2024 at 9:51 AM fengchengwen <fengchengwen@huawei.com> wrote:
>
> DPDK is just a SDK, it's not an application (although DPDK provided simple examples).
> Many code will developped based on DPDK, we can't predict how it was implemented.
> So there maybe a DPDK thread and a application thread both invoke strtok().

I think even dpdk threads such as the telemetry ones might invoke (the
same) strtok() at the same time.

> From this point of view, I hope that DPDK solves some of the reentrant problems of
> such C functions (e.g. strtok()\strerror()).
>
> Actually, we've try to solve before, but unfortunately it wasn't merged.
> 1\ strtok():   https://inbox.dpdk.org/dev/20231114110006.91148-1-haijie1@huawei.com/T/#u
> 2\ strerror(): https://inbox.dpdk.org/dev/20231114123552.398072-1-huangdengdui@huawei.com/T/#u

+1

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

* Re: Use of strtok() in dpdk code
  2024-10-22  6:51   ` fengchengwen
  2024-10-22 12:27     ` Isaac Boukris
@ 2024-10-22 15:25     ` Stephen Hemminger
  1 sibling, 0 replies; 5+ messages in thread
From: Stephen Hemminger @ 2024-10-22 15:25 UTC (permalink / raw)
  To: fengchengwen
  Cc: Isaac Boukris, Thomas Monjalon, dev,
	haijie1@huawei.com >> Jie Hai, huangdengdui

On Tue, 22 Oct 2024 14:51:39 +0800
fengchengwen <fengchengwen@huawei.com> wrote:

> On 2024/10/22 9:08, Stephen Hemminger wrote:
> > On Mon, 21 Oct 2024 21:30:02 +0300
> > Isaac Boukris <iboukris@gmail.com> wrote:
> >   
> >> Hello,
> >>
> >> I was debugging a crash resulting from strtok() returning NULL
> >> unexpectedly (string still had tokens and delimiters), and the only
> >> explanation I could come up with was that strtok is thread-unsafe and
> >> another thread could have been calling it at the same time, and so I
> >> changed it to use strtok_r().
> >>
> >> That said, the only other possible use of strtok() that I could find
> >> was in the dpdk code (telemetry), which brings me to my question,
> >> should we consider changing all occurrences to strtok_r() or am I
> >> missing something? there seem to be quite some in non-initialization
> >> code.
> >>
> >> Thanks!  
> > 
> > 
> > Most of the uses are in tests and other single threaded code.
> > In general, simpler just to use strtok_r everywhere and not worry about it.
> > Similar to not using sprintf() and instead using snprintf().  
> 
> I'm afraid I can't agree.
> 
> DPDK is just a SDK, it's not an application (although DPDK provided simple examples).
> Many code will developped based on DPDK, we can't predict how it was implemented.
> So there maybe a DPDK thread and a application thread both invoke strtok().
> 
> From this point of view, I hope that DPDK solves some of the reentrant problems of
> such C functions (e.g. strtok()\strerror()).
> 
> Actually, we've try to solve before, but unfortunately it wasn't merged.
> 1\ strtok():   https://inbox.dpdk.org/dev/20231114110006.91148-1-haijie1@huawei.com/T/#u
> 2\ strerror(): https://inbox.dpdk.org/dev/20231114123552.398072-1-huangdengdui@huawei.com/T/#u
> 
> > 
> > Some code scanners like codeql also flag this.  
> 

The usages of strtok() and strerror() in init code are fine. Stuff only called from
eal_init() like devargs should be safe, but fixing it makes sense.

Perhaps a coccinelle script could help here.

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

end of thread, other threads:[~2024-10-22 15:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-21 18:30 Use of strtok() in dpdk code Isaac Boukris
2024-10-22  1:08 ` Stephen Hemminger
2024-10-22  6:51   ` fengchengwen
2024-10-22 12:27     ` Isaac Boukris
2024-10-22 15:25     ` Stephen Hemminger

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