From ee571d76db63279326960222e090761b7937eff0 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Thu, 3 Oct 2024 19:00:30 +0200 Subject: [PATCH] Add missing ptr checks for malloc/calloc/CloseHandle --- libusb/hid.c | 8 ++++++++ windows/hid.c | 32 ++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/libusb/hid.c b/libusb/hid.c index a48ea9bf..29df4ce8 100644 --- a/libusb/hid.c +++ b/libusb/hid.c @@ -1272,6 +1272,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) return NULL; dev = new_hid_device(); + if (!dev) { + LOG("hid_open_path failed: Couldn't allocate memory\n"); + return NULL; + } libusb_get_device_list(usb_context, &devs); while ((usb_dev = devs[d++]) != NULL && !good_open) { @@ -1343,6 +1347,10 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_libusb_wrap_sys_device(intptr_t sys return NULL; dev = new_hid_device(); + if (!dev) { + LOG("libusb_wrap_sys_device failed: Couldn't allocate memory\n"); + return NULL; + } res = libusb_wrap_sys_device(usb_context, sys_dev, &dev->device_handle); if (res < 0) { diff --git a/windows/hid.c b/windows/hid.c index 799c0e88..343cdb7c 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -1045,7 +1045,10 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) end_of_function: free(interface_path); - CloseHandle(device_handle); + + if (device_handle != INVALID_HANDLE_VALUE) { + CloseHandle(device_handle); + } if (pp_data) { HidD_FreePreparsedData(pp_data); @@ -1085,8 +1088,15 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * /* The user passed the right number of bytes. Use the buffer as-is. */ buf = (unsigned char *) data; } else { - if (dev->write_buf == NULL) + if (dev->write_buf == NULL) { dev->write_buf = (unsigned char *) malloc(dev->output_report_length); + + if (dev->write_buf == NULL) { + register_string_error(dev, L"hid_write/malloc"); + goto end_of_function; + } + } + buf = dev->write_buf; memcpy(buf, data, length); memset(buf + length, 0, dev->output_report_length - length); @@ -1253,8 +1263,15 @@ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const u buf = (unsigned char *) data; length_to_send = length; } else { - if (dev->feature_buf == NULL) + if (dev->feature_buf == NULL) { dev->feature_buf = (unsigned char *) malloc(dev->feature_report_length); + + if (dev->feature_buf == NULL) { + register_string_error(dev, L"hid_send_feature_report/malloc"); + return -1; + } + } + buf = dev->feature_buf; memcpy(buf, data, length); memset(buf + length, 0, dev->feature_report_length - length); @@ -1347,8 +1364,15 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device* dev, const un buf = (unsigned char *) data; length_to_send = length; } else { - if (dev->write_buf == NULL) + if (dev->write_buf == NULL) { dev->write_buf = (unsigned char *) malloc(dev->output_report_length); + + if (dev->write_buf == NULL) { + register_string_error(dev, L"hid_send_output_report/malloc"); + return -1; + } + } + buf = dev->write_buf; memcpy(buf, data, length); memset(buf + length, 0, dev->output_report_length - length);