From e15684cb1477e2fac6170835ac9132443dc79970 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Tue, 9 Feb 2021 15:48:20 +0100 Subject: [PATCH] Fixed a ConvertToUnicode issue and added a unit test --- .../libwinpr/crt/test/TestUnicodeConversion.c | 25 +++++++++++++++++++ winpr/libwinpr/crt/unicode.c | 3 +-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/winpr/libwinpr/crt/test/TestUnicodeConversion.c b/winpr/libwinpr/crt/test/TestUnicodeConversion.c index 395be3200..e60b2b8bf 100644 --- a/winpr/libwinpr/crt/test/TestUnicodeConversion.c +++ b/winpr/libwinpr/crt/test/TestUnicodeConversion.c @@ -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 */ diff --git a/winpr/libwinpr/crt/unicode.c b/winpr/libwinpr/crt/unicode.c index 136df98c8..a49197971 100644 --- a/winpr/libwinpr/crt/unicode.c +++ b/winpr/libwinpr/crt/unicode.c @@ -429,9 +429,8 @@ int ConvertToUnicode(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cb { free(*lpWideCharStr); *lpWideCharStr = NULL; + status = 0; } - - status = 0; } return status;