Fixed a ConvertToUnicode issue and added a unit test
authorakallabeth <akallabeth@posteo.net>
Tue, 9 Feb 2021 14:48:20 +0000 (15:48 +0100)
committerakallabeth <akallabeth@users.noreply.github.com>
Thu, 25 Feb 2021 08:51:41 +0000 (09:51 +0100)
(cherry picked from commit e15684cb1477e2fac6170835ac9132443dc79970)

winpr/libwinpr/crt/test/TestUnicodeConversion.c
winpr/libwinpr/crt/unicode.c

index 395be32..e60b2b8 100644 (file)
@@ -411,6 +411,31 @@ static BOOL test_ConvertToUnicode_wrapper(void)
        int ii;
        size_t i;
 
+       /* Test static string buffers of differing sizes */
+       {
+               char name[] = "someteststring";
+               const WCHAR cmp[] = { L's', L'o', L'm', L'e', L't', L'e', L's', L't',
+                                         L's', L't', L'r', L'i', L'n', L'g', 0 };
+               WCHAR xname[128] = { 0 };
+               LPWSTR aname = NULL;
+               LPWSTR wname = &xname[0];
+               const size_t len = strnlen(name, ARRAYSIZE(name) - 1);
+               ii = ConvertToUnicode(CP_UTF8, 0, name, len, &wname, ARRAYSIZE(xname));
+               if (ii != len)
+                       goto fail;
+
+               if (memcmp(wname, cmp, sizeof(cmp)) != 0)
+                       goto fail;
+
+               ii = ConvertToUnicode(CP_UTF8, 0, name, len, &aname, 0);
+               if (ii != len)
+                       goto fail;
+               ii = memcmp(aname, cmp, sizeof(cmp));
+               free(aname);
+               if (ii != 0)
+                       goto fail;
+       }
+
        /* Test unterminated unicode string:
         * ConvertToUnicode must always null-terminate, even if the src string isn't
         */
index 136df98..a491979 100644 (file)
@@ -429,9 +429,8 @@ int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cb
                {
                        free(*lpWideCharStr);
                        *lpWideCharStr = NULL;
+                       status = 0;
                }
-
-               status = 0;
        }
 
        return status;