Skip to content

Commit 4fc9801

Browse files
authored
[libc] Fix wcstok() "subsequent searches" behavior.
You're allowed to keep calling wcstok() after the first nullptr, and should keep getting nullptr.
1 parent 506834d commit 4fc9801

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

libc/src/wchar/wcstok.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,22 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
2727
wchar_t *tok_start = str;
2828
while (*tok_start != L'\0' && internal::wcschr(delims, *tok_start))
2929
++tok_start;
30+
if (*tok_start == L'\0') {
31+
*context = nullptr;
32+
return nullptr;
33+
}
3034

3135
wchar_t *tok_end = tok_start;
3236
while (*tok_end != L'\0' && !internal::wcschr(delims, *tok_end))
3337
++tok_end;
3438

35-
if (*tok_end != L'\0') {
39+
if (*tok_end == L'\0') {
40+
*context = nullptr;
41+
} else {
3642
*tok_end = L'\0';
37-
++tok_end;
43+
*context = tok_end + 1;
3844
}
39-
*context = tok_end;
40-
return *tok_start == L'\0' ? nullptr : tok_start;
45+
return tok_start;
4146
}
4247

4348
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)