[PATCH] kernelbase: Always remove trailing spaces in PathRemoveBlanks.

Esme Povirk esme at codeweavers.com
Sat Apr 24 14:12:59 CDT 2021


Signed-off-by: Esme Povirk <esme at codeweavers.com>
---
The intent of the changed if statement was probably to prevent reading
past the start of a string consisting only of spaces, but it didn't
account for path being changed. In practice, this meant that trailing
spaces would only be removed if there were leading spaces, and it
would read past the start of a string containing only spaces.

 dlls/kernelbase/path.c    | 12 ++++++-----
 dlls/shlwapi/tests/path.c | 45 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/dlls/kernelbase/path.c b/dlls/kernelbase/path.c
index f0c79bcc94c..14e892bcc42 100644
--- a/dlls/kernelbase/path.c
+++ b/dlls/kernelbase/path.c
@@ -1902,14 +1902,14 @@ BOOL WINAPI PathIsUNCServerW(const WCHAR *path)
 
 void WINAPI PathRemoveBlanksA(char *path)
 {
-    char *start;
+    char *start, *first;
 
     TRACE("%s\n", wine_dbgstr_a(path));
 
     if (!path || !*path)
         return;
 
-    start = path;
+    start = first = path;
 
     while (*path == ' ')
         path = CharNextA(path);
@@ -1917,7 +1917,7 @@ void WINAPI PathRemoveBlanksA(char *path)
     while (*path)
         *start++ = *path++;
 
-    if (start != path)
+    if (start != first)
         while (start[-1] == ' ')
             start--;
 
@@ -1926,20 +1926,22 @@ void WINAPI PathRemoveBlanksA(char *path)
 
 void WINAPI PathRemoveBlanksW(WCHAR *path)
 {
-    WCHAR *start = path;
+    WCHAR *start, *first;
 
     TRACE("%s\n", wine_dbgstr_w(path));
 
     if (!path || !*path)
         return;
 
+    start = first = path;
+
     while (*path == ' ')
         path++;
 
     while (*path)
         *start++ = *path++;
 
-    if (start != path)
+    if (start != first)
         while (start[-1] == ' ')
             start--;
 
diff --git a/dlls/shlwapi/tests/path.c b/dlls/shlwapi/tests/path.c
index c5142b992aa..e9f08a902b0 100644
--- a/dlls/shlwapi/tests/path.c
+++ b/dlls/shlwapi/tests/path.c
@@ -1712,6 +1712,50 @@ static void test_PathUndecorate(void)
     PathUndecorateW(NULL);
 }
 
+static void test_PathRemoveBlanks(void)
+{
+    struct remove_blanks_test {
+        const char* input;
+        const char* expected;
+    };
+    struct remove_blanks_test tests[] = {
+        {"", ""},
+        {" ", ""},
+        {"test", "test"},
+        {" test", "test"},
+        {"  test", "test"},
+        {"test ", "test"},
+        {"test  ", "test"},
+        {" test  ", "test"},
+        {"  test ", "test"}};
+    char pathA[MAX_PATH];
+    WCHAR pathW[MAX_PATH];
+    int i, ret;
+    const UINT CP_ASCII = 20127;
+
+    PathRemoveBlanksW(NULL);
+    PathRemoveBlanksA(NULL);
+
+    for (i=0; i < ARRAY_SIZE(tests); i++)
+    {
+        strcpy(pathA, tests[i].input);
+        PathRemoveBlanksA(pathA);
+        ok(strcmp(pathA, tests[i].expected) == 0, "input string '%s', expected '%s', got '%s'\n",
+            tests[i].input, tests[i].expected, pathA);
+
+        ret = MultiByteToWideChar(CP_ASCII, MB_ERR_INVALID_CHARS, tests[i].input, -1, pathW, MAX_PATH);
+        ok(ret != 0, "MultiByteToWideChar failed for '%s'\n", tests[i].input);
+
+        PathRemoveBlanksW(pathW);
+
+        ret = WideCharToMultiByte(CP_ASCII, 0, pathW, -1, pathA, MAX_PATH, NULL, NULL);
+        ok(ret != 0, "WideCharToMultiByte failed for %s from test string '%s'\n", wine_dbgstr_w(pathW), tests[i].input);
+
+        ok(strcmp(pathA, tests[i].expected) == 0, "input string '%s', expected '%s', got '%s'\n",
+            tests[i].input, tests[i].expected, pathA);
+    }
+}
+
 START_TEST(path)
 {
     HMODULE hShlwapi = GetModuleHandleA("shlwapi.dll");
@@ -1759,4 +1803,5 @@ START_TEST(path)
     test_PathIsRelativeW();
     test_PathStripPathA();
     test_PathUndecorate();
+    test_PathRemoveBlanks();
 }
-- 
2.25.1




More information about the wine-devel mailing list