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 875dce9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 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
4 changes: 2 additions & 2 deletions linux/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ static int get_next_hid_usage(const __u8 *report_descriptor, __u32 size, struct
/* If no top-level application collection is found and usage page/usage pair is found, pair is valid
https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/top-level-collections */
if (initial && usage_found && ctx->usage_page_found) {
*usage_page = ctx->usage_page;
return 0; /* success */
*usage_page = ctx->usage_page;
return 0; /* success */
}

return 1; /* finished processing */
Expand Down
3 changes: 1 addition & 2 deletions mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1236,7 +1236,6 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t
}

return 0;

}

int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
Expand Down Expand Up @@ -1522,7 +1521,7 @@ int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char
if (ref != NULL && CFGetTypeID(ref) == CFDataGetTypeID()) {
CFDataRef report_descriptor = (CFDataRef) ref;
const UInt8 *descriptor_buf = CFDataGetBytePtr(report_descriptor);
CFIndex descriptor_buf_len = CFDataGetLength(report_descriptor);
const CFIndex descriptor_buf_len = CFDataGetLength(report_descriptor);
size_t copy_len = (size_t) descriptor_buf_len;

if (descriptor_buf == NULL || descriptor_buf_len < 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) {
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_winapi_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_winapi_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_winapi_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 875dce9

Please sign in to comment.