Various memory access fixes to prevent crashes (#14459) (#15000)

Prevent double free and null dereference when the controller is quickly reconnected.
Handle error when controller device query returns null instead of crashing.
This commit is contained in:
serakeri 2023-02-21 00:09:36 -05:00 committed by GitHub
parent 24dc83cbda
commit 347ea8157c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -544,6 +544,7 @@ static void iohidmanager_hid_device_remove(IOHIDDeviceRef device, iohidmanager_h
free(tmp); free(tmp);
} }
free(adapter); free(adapter);
adapter = NULL;
} }
RARCH_LOG("Device removed from port %d\n", slot); RARCH_LOG("Device removed from port %d\n", slot);
} }
@ -702,6 +703,10 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_
/* scan for buttons, axis, hats */ /* scan for buttons, axis, hats */
elements_raw = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone); elements_raw = IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
if (!elements_raw)
goto error;
count = (int)CFArrayGetCount(elements_raw); count = (int)CFArrayGetCount(elements_raw);
elements = CFArrayCreateMutableCopy( elements = CFArrayCreateMutableCopy(
kCFAllocatorDefault,(CFIndex)count,elements_raw); kCFAllocatorDefault,(CFIndex)count,elements_raw);
@ -940,6 +945,7 @@ error:
free(tmp); free(tmp);
} }
free(adapter); free(adapter);
adapter = NULL;
} }
} }

View File

@ -63,6 +63,9 @@ typedef struct
static void free_autoconfig_handle(autoconfig_handle_t *autoconfig_handle) static void free_autoconfig_handle(autoconfig_handle_t *autoconfig_handle)
{ {
if (!autoconfig_handle)
return;
if (autoconfig_handle->dir_autoconfig) if (autoconfig_handle->dir_autoconfig)
{ {
free(autoconfig_handle->dir_autoconfig); free(autoconfig_handle->dir_autoconfig);
@ -743,9 +746,7 @@ error:
task = NULL; task = NULL;
} }
if (autoconfig_handle) free_autoconfig_handle(autoconfig_handle);
free_autoconfig_handle(autoconfig_handle);
autoconfig_handle = NULL;
return false; return false;
} }
@ -928,7 +929,6 @@ error:
} }
free_autoconfig_handle(autoconfig_handle); free_autoconfig_handle(autoconfig_handle);
autoconfig_handle = NULL;
return false; return false;
} }