Implementation of strtok

Jayakrishna Vadayath jvadayat@asu.edu
Fri Jun 2 05:22:29 GMT 2023


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


More information about the Libc-alpha mailing list