Fixed crash if manufacturer or product string is NULL
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Mar 2020 19:00:22 -0700
changeset 13619217a7d8a5fef
parent 13618 2ef60abd9132
child 13620 317366b2c172
Fixed crash if manufacturer or product string is NULL
src/joystick/hidapi/SDL_hidapijoystick.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Fri Mar 13 13:28:33 2020 -0700
     1.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Fri Mar 13 19:00:22 2020 -0700
     1.3 @@ -699,15 +699,27 @@
     1.4  
     1.5      /* Need the device name before getting the driver to know whether to ignore this device */
     1.6      {
     1.7 -        char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
     1.8 -        char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
     1.9 -        if (!manufacturer_string && !product_string) {
    1.10 -            if (sizeof(wchar_t) == sizeof(Uint16)) {
    1.11 -                manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
    1.12 -                product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
    1.13 -            } else if (sizeof(wchar_t) == sizeof(Uint32)) {
    1.14 -                manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
    1.15 -                product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
    1.16 +        char *manufacturer_string = NULL;
    1.17 +        char *product_string = NULL;
    1.18 +
    1.19 +        if (info->manufacturer_string) {
    1.20 +            manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
    1.21 +            if (!manufacturer_string) {
    1.22 +                if (sizeof(wchar_t) == sizeof(Uint16)) {
    1.23 +                    manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
    1.24 +                } else if (sizeof(wchar_t) == sizeof(Uint32)) {
    1.25 +                    manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
    1.26 +                }
    1.27 +            }
    1.28 +        }
    1.29 +        if (info->product_string) {
    1.30 +            product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
    1.31 +            if (!product_string) {
    1.32 +                if (sizeof(wchar_t) == sizeof(Uint16)) {
    1.33 +                    product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
    1.34 +                } else if (sizeof(wchar_t) == sizeof(Uint32)) {
    1.35 +                    product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
    1.36 +                }
    1.37              }
    1.38          }
    1.39