From f568aed2e768ab469d99b2d79f535bdc231f912e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 14 Nov 2020 11:24:42 +0100 Subject: [PATCH] LibC: strtok_r() should not go past the last token When we hit the last token, make the saved pointer point to the null terminator instead of to the next token. This ensures that the next call to strtok_r() returns null as expected. Found by running GCC in UE. :^) --- Libraries/LibC/string.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Libraries/LibC/string.cpp b/Libraries/LibC/string.cpp index 56eadb2c26..a6fcc8d6e9 100644 --- a/Libraries/LibC/string.cpp +++ b/Libraries/LibC/string.cpp @@ -457,7 +457,11 @@ char* strtok_r(char* str, const char* delim, char** saved_str) return &str[token_start]; } - *saved_str = &str[token_end + 1]; + if (str[token_end] == '\0') + *saved_str = &str[token_end]; + else + *saved_str = &str[token_end + 1]; + str[token_end] = '\0'; return &str[token_start]; }