Fixed smartcard_convert_string_list with 0 length
authorArmin Novak <armin.novak@thincast.com>
Wed, 3 Mar 2021 10:39:55 +0000 (11:39 +0100)
committerakallabeth <akallabeth@users.noreply.github.com>
Wed, 3 Mar 2021 11:06:15 +0000 (12:06 +0100)
(cherry picked from commit e9904e286f43dea5940182b25740730c55e7c8a6)

channels/smartcard/client/smartcard_pack.c

index ed0301b..83c9d30 100644 (file)
@@ -351,8 +351,11 @@ static char* smartcard_convert_string_list(const void* in, size_t bytes, BOOL un
 
        if (unicode)
        {
-               length = (bytes / 2);
-               if (ConvertFromUnicode(CP_UTF8, 0, string.wz, (int)length, &mszA, 0, NULL, NULL) !=
+               length = (bytes / sizeof(WCHAR)) - 1;
+               mszA = (char*)calloc(length + 1, sizeof(WCHAR));
+               if (!mszA)
+                       return NULL;
+               if (ConvertFromUnicode(CP_UTF8, 0, string.wz, (int)length, &mszA, length + 1, NULL, NULL) !=
                    (int)length)
                {
                        free(mszA);
@@ -362,10 +365,11 @@ static char* smartcard_convert_string_list(const void* in, size_t bytes, BOOL un
        else
        {
                length = bytes;
-               mszA = (char*)malloc(length);
+               mszA = (char*)calloc(length, sizeof(char));
                if (!mszA)
                        return NULL;
-               CopyMemory(mszA, string.sz, length);
+               CopyMemory(mszA, string.sz, length - 1);
+               mszA[length - 1] = '\0';
        }
 
        for (index = 0; index < length - 1; index++)