Skip to content

Commit

Permalink
string: fix strtok
Browse files Browse the repository at this point in the history
After a call to `strtok`, the internal state is left to \0; this leads
to returning NULL on the next call regardless of the string.

Signed-off-by: Daniele Ahmed <ahmeddan amazon c;0m >
  • Loading branch information
82marbag authored and wipawel committed Jul 6, 2021
1 parent ae07d46 commit 103dd3f
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static inline char *strtok(char *s, const char *delim) {
int ch;

if (NULL == s)
s = lasts;
s = lasts + 1;

This comment has been minimized.

Copy link
@dkgupta-amzn

dkgupta-amzn Jul 6, 2021

Contributor

If s is NULL first time strtok is called then it would be returning 1.
Sidenote: lasts should be initialized to NULL (I think compiler would ensure that but doing explicit assignment doesn't hurt)

This comment has been minimized.

Copy link
@82marbag

82marbag Jul 6, 2021

Author Contributor

We panic at the line below by accessing 1 (now) instead of 0 (before). I can make this return null if it's better

This comment has been minimized.

Copy link
@82marbag

82marbag Jul 6, 2021

Author Contributor

Changed not to panic anyway: #178


do {
if ((ch = *s++) == '\0')
Expand Down

0 comments on commit 103dd3f

Please sign in to comment.