Skip to content

Commit

Permalink
Add missing ptr checks for malloc/calloc/CloseHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Oct 3, 2024
1 parent c15392a commit ee571d7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions libusb/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 28 additions & 4 deletions windows/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ee571d7

Please sign in to comment.