Fixed bug 4986 - Memory leak in HIDAPI_JoystickConnected
authorSam Lantinga
Fri, 14 Feb 2020 16:15:46 -0800
changeset 1352330bc6f3e1ec6
parent 13522 3cd477fca37e
child 13524 9761858bd6a3
Fixed bug 4986 - Memory leak in HIDAPI_JoystickConnected

meyraud705

Memory allocated for device->joysticks on line 589 of SDL_hidapijoystick.c is never freed.

Also, use memmove because memory is overlapping.
src/joystick/hidapi/SDL_hidapijoystick.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Fri Feb 14 15:19:34 2020 -0800
     1.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Fri Feb 14 16:15:46 2020 -0800
     1.3 @@ -616,9 +616,13 @@
     1.4                  HIDAPI_JoystickClose(joystick);
     1.5              }
     1.6  
     1.7 -            SDL_memcpy(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1);
     1.8 +            SDL_memmove(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1);
     1.9              --device->num_joysticks;
    1.10              --SDL_HIDAPI_numjoysticks;
    1.11 +            if (device->num_joysticks == 0) {
    1.12 +                SDL_free(device->joysticks);
    1.13 +                device->joysticks = NULL;
    1.14 +            }
    1.15  
    1.16              if (!shutting_down) {
    1.17                  SDL_PrivateJoystickRemoved(joystickID);