public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Implementation of strtok
@ 2023-06-02  5:22 Jayakrishna Vadayath
  2023-06-02  5:40 ` Florian Weimer
  0 siblings, 1 reply; 5+ messages in thread
From: Jayakrishna Vadayath @ 2023-06-02  5:22 UTC (permalink / raw)
  To: libc-alpha

[-- Attachment #1: Type: text/plain, Size: 1282 bytes --]

Hi,

I had a few questions regarding the implementation and documentation of the
strtok function.
I've noticed the following snippet of code in different applications that
parse HTTP packets.

```
char delim[2] = "\r";
void foo(char *p) {
    char *tmp = p;
    if (strchr(p, '\r'))
        tmp = strtok(tmp, delim);
    if (tmp == NULL)
        puts("Y"); // (1) This shouldn't happen
    else
        puts("N"); // (2) This is the expected case
}
```
According to the documentation of the strchr and strtok functions, it would
appear that (1) will never be executed.

However, I've found one situation that leads to strtok returning a NULL
value.
If the function foo is invoked as follows, (1) will be executed and the
string "Y" will be printed out.
```
char buf[2] = "\r\0";
foo(&buf);
```

After looking at the implementation, I find that this line of code is
responsible for it :
https://elixir.bootlin.com/glibc/glibc-2.7/source/string/strtok.c#L51

I was wondering why the implementation of strtok tries to skip the leading
delimiters and why this edge case is not mentioned in the documentation.

I'm looking forward to your answers on this topic.
Additionally, I'd be happy to help with fixing the documentation or code if
I can.

Thank you.
-- 
Regards
Jayakrishna Menon

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

end of thread, other threads:[~2023-06-02  6:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-02  5:22 Implementation of strtok Jayakrishna Vadayath
2023-06-02  5:40 ` Florian Weimer
2023-06-02  5:52   ` Jayakrishna Vadayath
2023-06-02  6:05     ` Florian Weimer
2023-06-02  6:17       ` Jayakrishna Vadayath

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 read-only IMAP folder(s) and NNTP newsgroup(s).