Skip to content

Commit b69d7db

Browse files
committed
Add a bunch of sanity checks
1 parent 750bf20 commit b69d7db

File tree

4 files changed

+331
-6
lines changed

4 files changed

+331
-6
lines changed

libusb/hid.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length);
139139
static hid_device *new_hid_device(void)
140140
{
141141
hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device));
142+
if (!dev)
143+
return NULL;
144+
142145
dev->blocking = 1;
143146

144147
hidapi_thread_state_init(&dev->thread_state);
@@ -148,6 +151,9 @@ static hid_device *new_hid_device(void)
148151

149152
static void free_hid_device(hid_device *dev)
150153
{
154+
if (!dev)
155+
return;
156+
151157
/* Clean up the thread objects */
152158
hidapi_thread_state_destroy(&dev->thread_state);
153159

@@ -169,6 +175,9 @@ static void register_error(hid_device *dev, const char *op)
169175
Only call with a num_bytes of 0, 1, 2, or 4. */
170176
static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur)
171177
{
178+
if (!rpt)
179+
return 0;
180+
172181
/* Return if there aren't enough bytes. */
173182
if (cur + num_bytes >= len)
174183
return 0;
@@ -198,6 +207,9 @@ static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur
198207
static int get_usage(uint8_t *report_descriptor, size_t size,
199208
unsigned short *usage_page, unsigned short *usage)
200209
{
210+
if (!report_descriptor || !usage_page || !usage)
211+
return -1;
212+
201213
unsigned int i = 0;
202214
int size_code;
203215
int data_len, key_size;
@@ -536,6 +548,9 @@ static int hid_get_report_descriptor_libusb(libusb_device_handle *handle, int in
536548
*/
537549
static void fill_device_info_usage(struct hid_device_info *cur_dev, libusb_device_handle *handle, int interface_num, uint16_t expected_report_descriptor_size)
538550
{
551+
if (!cur_dev)
552+
return;
553+
539554
unsigned char hid_report_descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];
540555
unsigned short page = 0, usage = 0;
541556

@@ -632,6 +647,9 @@ static struct hid_device_info * create_device_info_for_device(libusb_device *dev
632647

633648
static uint16_t get_report_descriptor_size_from_interface_descriptors(const struct libusb_interface_descriptor *intf_desc)
634649
{
650+
if (!intf_desc)
651+
return 0;
652+
635653
int i = 0;
636654
int found_hid_report_descriptor = 0;
637655
uint16_t result = HID_API_MAX_REPORT_DESCRIPTOR_SIZE;
@@ -685,6 +703,9 @@ static uint16_t get_report_descriptor_size_from_interface_descriptors(const stru
685703

686704
static int is_xbox360(unsigned short vendor_id, const struct libusb_interface_descriptor *intf_desc)
687705
{
706+
if (!intf_desc)
707+
return 0;
708+
688709
static const int xb360_iface_subclass = 93;
689710
static const int xb360_iface_protocol = 1; /* Wired */
690711
static const int xb360w_iface_protocol = 129; /* Wireless */
@@ -733,6 +754,9 @@ static int is_xbox360(unsigned short vendor_id, const struct libusb_interface_de
733754

734755
static int is_xboxone(unsigned short vendor_id, const struct libusb_interface_descriptor *intf_desc)
735756
{
757+
if (!intf_desc)
758+
return 0;
759+
736760
static const int xb1_iface_subclass = 71;
737761
static const int xb1_iface_protocol = 208;
738762
static const int supported_vendors[] = {
@@ -769,6 +793,8 @@ static int should_enumerate_interface(unsigned short vendor_id, const struct lib
769793
#if 0
770794
printf("Checking interface 0x%x %d/%d/%d/%d\n", vendor_id, intf_desc->bInterfaceNumber, intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol);
771795
#endif
796+
if (!intf_desc)
797+
return 0;
772798

773799
if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID)
774800
return 1;
@@ -950,6 +976,9 @@ hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const
950976

951977
static void LIBUSB_CALL read_callback(struct libusb_transfer *transfer)
952978
{
979+
if (!transfer)
980+
return;
981+
953982
hid_device *dev = transfer->user_data;
954983
int res;
955984

@@ -1018,6 +1047,9 @@ static void LIBUSB_CALL read_callback(struct libusb_transfer *transfer)
10181047

10191048
static void *read_thread(void *param)
10201049
{
1050+
if (!param)
1051+
return NULL;
1052+
10211053
int res;
10221054
hid_device *dev = param;
10231055
uint8_t *buf;
@@ -1118,6 +1150,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
11181150

11191151
(void)idProduct;
11201152

1153+
if (!conf_desc)
1154+
return;
1155+
11211156
for (j = 0; j < conf_desc->bNumInterfaces; j++) {
11221157
const struct libusb_interface *intf = &conf_desc->interface[j];
11231158
for (k = 0; k < intf->num_altsetting; k++) {
@@ -1158,6 +1193,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
11581193

11591194
static int hidapi_initialize_device(hid_device *dev, const struct libusb_interface_descriptor *intf_desc, const struct libusb_config_descriptor *conf_desc)
11601195
{
1196+
if (!conf_desc)
1197+
return 0;
1198+
11611199
int i =0;
11621200
int res = 0;
11631201
struct libusb_device_descriptor desc;
@@ -1413,6 +1451,9 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_libusb_wrap_sys_device(intptr_t sys
14131451

14141452
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
14151453
{
1454+
if (!dev)
1455+
return -1;
1456+
14161457
int res;
14171458
int report_number;
14181459
int skipped_report_id = 0;
@@ -1455,6 +1496,9 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
14551496
This should be called with dev->mutex locked. */
14561497
static int return_data(hid_device *dev, unsigned char *data, size_t length)
14571498
{
1499+
if (!dev || !data)
1500+
return 0;
1501+
14581502
/* Copy the data out of the linked list item (rpt) into the
14591503
return buffer (data), and delete the liked list item. */
14601504
struct input_report *rpt = dev->input_reports;
@@ -1469,13 +1513,19 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length)
14691513

14701514
static void cleanup_mutex(void *param)
14711515
{
1516+
if (!param)
1517+
return;
1518+
14721519
hid_device *dev = param;
14731520
hidapi_thread_mutex_unlock(&dev->thread_state);
14741521
}
14751522

14761523

14771524
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
14781525
{
1526+
if (!dev)
1527+
return -1;
1528+
14791529
#if 0
14801530
int transferred;
14811531
int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
@@ -1564,6 +1614,9 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
15641614

15651615
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
15661616
{
1617+
if (!dev)
1618+
return -1;
1619+
15671620
dev->blocking = !nonblock;
15681621

15691622
return 0;
@@ -1572,6 +1625,9 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
15721625

15731626
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
15741627
{
1628+
if (!dev || !data)
1629+
return -1;
1630+
15751631
int res = -1;
15761632
int skipped_report_id = 0;
15771633
int report_number = data[0];
@@ -1602,6 +1658,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
16021658

16031659
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
16041660
{
1661+
if (!dev || !data)
1662+
return -1;
1663+
16051664
int res = -1;
16061665
int skipped_report_id = 0;
16071666
int report_number = data[0];
@@ -1632,6 +1691,9 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
16321691

16331692
int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *data, size_t length)
16341693
{
1694+
if (!dev || !data)
1695+
return -1;
1696+
16351697
int res = -1;
16361698
int skipped_report_id = 0;
16371699
int report_number = data[0];
@@ -1662,6 +1724,9 @@ int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *
16621724

16631725
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
16641726
{
1727+
if (!dev || !data)
1728+
return -1;
1729+
16651730
int res = -1;
16661731
int skipped_report_id = 0;
16671732
int report_number = data[0];
@@ -1735,20 +1800,32 @@ void HID_API_EXPORT hid_close(hid_device *dev)
17351800

17361801
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
17371802
{
1803+
if (!dev)
1804+
return -1;
1805+
17381806
return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
17391807
}
17401808

17411809
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
17421810
{
1811+
if (!dev)
1812+
return -1;
1813+
17431814
return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
17441815
}
17451816

17461817
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
17471818
{
1819+
if (!dev)
1820+
return -1;
1821+
17481822
return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
17491823
}
17501824

17511825
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1826+
if (!dev)
1827+
return NULL;
1828+
17521829
if (!dev->device_info) {
17531830
struct libusb_device_descriptor desc;
17541831
libusb_device *usb_device = libusb_get_device(dev->device_handle);
@@ -1767,6 +1844,9 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
17671844

17681845
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
17691846
{
1847+
if (!dev || !string)
1848+
return -1;
1849+
17701850
wchar_t *str;
17711851

17721852
str = get_usb_string(dev->device_handle, string_index);
@@ -1783,6 +1863,9 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
17831863

17841864
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
17851865
{
1866+
if (!dev)
1867+
return -1;
1868+
17861869
return hid_get_report_descriptor_libusb(dev->device_handle, dev->interface, dev->report_descriptor_size, buf, buf_size);
17871870
}
17881871

0 commit comments

Comments
 (0)