From 715f8d42a2bd554c9f4960a45594dcc5f9236ebc Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 14 Feb 2020 16:15:46 -0800 Subject: [PATCH] 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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c index a2e91a320..de87fbcb8 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick.c +++ b/src/joystick/hidapi/SDL_hidapijoystick.c @@ -616,9 +616,13 @@ HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID HIDAPI_JoystickClose(joystick); } - SDL_memcpy(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1); + SDL_memmove(&device->joysticks[i], &device->joysticks[i+1], device->num_joysticks - i - 1); --device->num_joysticks; --SDL_HIDAPI_numjoysticks; + if (device->num_joysticks == 0) { + SDL_free(device->joysticks); + device->joysticks = NULL; + } if (!shutting_down) { SDL_PrivateJoystickRemoved(joystickID);