[PATCH] msvcrt: Set next_token when str is empty in wcstok_s.

Rémi Bernon rbernon at codeweavers.com
Tue May 25 08:59:46 CDT 2021


Otherwise, when str is empty, an invalid parameter exception is raised.

Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=30244
Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---

The WineHQ bug is also about some additional issue with missing DSS
crypto, but the game initially crashes on wcstok unless some workaround
steps are used.

 dlls/msvcrt/wcs.c            | 11 +++++++----
 dlls/ucrtbase/tests/string.c | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/dlls/msvcrt/wcs.c b/dlls/msvcrt/wcs.c
index 343dec70477..615b4f36a33 100644
--- a/dlls/msvcrt/wcs.c
+++ b/dlls/msvcrt/wcs.c
@@ -1910,10 +1910,13 @@ wchar_t * CDECL wcstok_s( wchar_t *str, const wchar_t *delim,
     if (!str) str = *next_token;
 
     while (*str && wcschr( delim, *str )) str++;
-    if (!*str) return NULL;
-    ret = str++;
-    while (*str && !wcschr( delim, *str )) str++;
-    if (*str) *str++ = 0;
+    if (!*str) ret = NULL;
+    else
+    {
+        ret = str++;
+        while (*str && !wcschr( delim, *str )) str++;
+        if (*str) *str++ = 0;
+    }
     *next_token = str;
     return ret;
 }
diff --git a/dlls/ucrtbase/tests/string.c b/dlls/ucrtbase/tests/string.c
index 1a4fbbd76f0..9b1d21b801c 100644
--- a/dlls/ucrtbase/tests/string.c
+++ b/dlls/ucrtbase/tests/string.c
@@ -418,6 +418,24 @@ static void test_wcstok(void)
     ok(!wcscmp(L"words", token), "expected \"words\", got \"%ls\"\n", token);
     token = wcstok(NULL, L" ", NULL);
     ok(!token, "expected NULL, got %p\n", token);
+
+    next = NULL;
+    wcscpy(buffer, input);
+    token = wcstok(buffer, L"=", &next);
+    ok(!wcscmp(token, input), "expected \"%ls\", got \"%ls\"\n", input, token);
+    ok(next == buffer + wcslen(input), "expected %p, got %p\n", buffer + wcslen(input), next);
+    token = wcstok(NULL, L"=", &next);
+    ok(!token, "expected NULL, got \"%ls\"\n", token);
+    ok(next == buffer + wcslen(input), "expected %p, got %p\n", buffer + wcslen(input), next);
+
+    next = NULL;
+    wcscpy(buffer, L"");
+    token = wcstok(buffer, L"=", &next);
+    ok(token == NULL, "expected NULL, got \"%ls\"\n", token);
+    ok(next == buffer, "expected %p, got %p\n", buffer, next);
+    token = wcstok(NULL, L"=", &next);
+    ok(!token, "expected NULL, got \"%ls\"\n", token);
+    ok(next == buffer, "expected %p, got %p\n", buffer, next);
 }
 
 static void test__strnicmp(void)
-- 
2.31.0




More information about the wine-devel mailing list