Skip to content

Commit 3d3da2e

Browse files
committed
Add a bunch of sanity checks
1 parent ba28d38 commit 3d3da2e

File tree

4 files changed

+275
-11
lines changed

4 files changed

+275
-11
lines changed

libusb/hid.c

Lines changed: 85 additions & 3 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;
@@ -546,8 +558,10 @@ static void fill_device_info_usage(struct hid_device_info *cur_dev, libusb_devic
546558
get_usage(hid_report_descriptor, res, &page, &usage);
547559
}
548560

549-
cur_dev->usage_page = page;
550-
cur_dev->usage = usage;
561+
if (cur_dev) {
562+
cur_dev->usage_page = page;
563+
cur_dev->usage = usage;
564+
}
551565
}
552566

553567
#ifdef INVASIVE_GET_USAGE
@@ -632,6 +646,9 @@ static struct hid_device_info * create_device_info_for_device(libusb_device *dev
632646

633647
static uint16_t get_report_descriptor_size_from_interface_descriptors(const struct libusb_interface_descriptor *intf_desc)
634648
{
649+
if (!intf_desc)
650+
return 0;
651+
635652
int i = 0;
636653
int found_hid_report_descriptor = 0;
637654
uint16_t result = HID_API_MAX_REPORT_DESCRIPTOR_SIZE;
@@ -685,6 +702,9 @@ static uint16_t get_report_descriptor_size_from_interface_descriptors(const stru
685702

686703
static int is_xbox360(unsigned short vendor_id, const struct libusb_interface_descriptor *intf_desc)
687704
{
705+
if (!intf_desc)
706+
return 0;
707+
688708
static const int xb360_iface_subclass = 93;
689709
static const int xb360_iface_protocol = 1; /* Wired */
690710
static const int xb360w_iface_protocol = 129; /* Wireless */
@@ -733,6 +753,9 @@ static int is_xbox360(unsigned short vendor_id, const struct libusb_interface_de
733753

734754
static int is_xboxone(unsigned short vendor_id, const struct libusb_interface_descriptor *intf_desc)
735755
{
756+
if (!intf_desc)
757+
return 0;
758+
736759
static const int xb1_iface_subclass = 71;
737760
static const int xb1_iface_protocol = 208;
738761
static const int supported_vendors[] = {
@@ -769,6 +792,8 @@ static int should_enumerate_interface(unsigned short vendor_id, const struct lib
769792
#if 0
770793
printf("Checking interface 0x%x %d/%d/%d/%d\n", vendor_id, intf_desc->bInterfaceNumber, intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol);
771794
#endif
795+
if (!intf_desc)
796+
return 0;
772797

773798
if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID)
774799
return 1;
@@ -950,6 +975,9 @@ hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const
950975

951976
static void LIBUSB_CALL read_callback(struct libusb_transfer *transfer)
952977
{
978+
if (!transfer)
979+
return;
980+
953981
hid_device *dev = transfer->user_data;
954982
int res;
955983

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

10191047
static void *read_thread(void *param)
10201048
{
1049+
if (!param)
1050+
return NULL;
1051+
10211052
int res;
10221053
hid_device *dev = param;
10231054
uint8_t *buf;
@@ -1118,6 +1149,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
11181149

11191150
(void)idProduct;
11201151

1152+
if (!conf_desc)
1153+
return;
1154+
11211155
for (j = 0; j < conf_desc->bNumInterfaces; j++) {
11221156
const struct libusb_interface *intf = &conf_desc->interface[j];
11231157
for (k = 0; k < intf->num_altsetting; k++) {
@@ -1158,6 +1192,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
11581192

11591193
static int hidapi_initialize_device(hid_device *dev, const struct libusb_interface_descriptor *intf_desc, const struct libusb_config_descriptor *conf_desc)
11601194
{
1195+
if (!conf_desc)
1196+
return 0;
1197+
11611198
int i =0;
11621199
int res = 0;
11631200
struct libusb_device_descriptor desc;
@@ -1405,6 +1442,9 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_libusb_wrap_sys_device(intptr_t sys
14051442

14061443
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
14071444
{
1445+
if (!dev)
1446+
return -1;
1447+
14081448
int res;
14091449
int report_number;
14101450
int skipped_report_id = 0;
@@ -1447,11 +1487,14 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
14471487
This should be called with dev->mutex locked. */
14481488
static int return_data(hid_device *dev, unsigned char *data, size_t length)
14491489
{
1490+
if (!dev)
1491+
return 0;
1492+
14501493
/* Copy the data out of the linked list item (rpt) into the
14511494
return buffer (data), and delete the liked list item. */
14521495
struct input_report *rpt = dev->input_reports;
14531496
size_t len = (length < rpt->len)? length: rpt->len;
1454-
if (len > 0)
1497+
if (data && len > 0)
14551498
memcpy(data, rpt->data, len);
14561499
dev->input_reports = rpt->next;
14571500
free(rpt->data);
@@ -1461,13 +1504,19 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length)
14611504

14621505
static void cleanup_mutex(void *param)
14631506
{
1507+
if (!param)
1508+
return;
1509+
14641510
hid_device *dev = param;
14651511
hidapi_thread_mutex_unlock(&dev->thread_state);
14661512
}
14671513

14681514

14691515
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
14701516
{
1517+
if (!dev)
1518+
return -1;
1519+
14711520
#if 0
14721521
int transferred;
14731522
int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
@@ -1556,6 +1605,9 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
15561605

15571606
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
15581607
{
1608+
if (!dev)
1609+
return -1;
1610+
15591611
dev->blocking = !nonblock;
15601612

15611613
return 0;
@@ -1564,6 +1616,9 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
15641616

15651617
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
15661618
{
1619+
if (!dev || !data)
1620+
return -1;
1621+
15671622
int res = -1;
15681623
int skipped_report_id = 0;
15691624
int report_number = data[0];
@@ -1594,6 +1649,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
15941649

15951650
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
15961651
{
1652+
if (!dev || !data)
1653+
return -1;
1654+
15971655
int res = -1;
15981656
int skipped_report_id = 0;
15991657
int report_number = data[0];
@@ -1624,6 +1682,9 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
16241682

16251683
int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *data, size_t length)
16261684
{
1685+
if (!dev || !data)
1686+
return -1;
1687+
16271688
int res = -1;
16281689
int skipped_report_id = 0;
16291690
int report_number = data[0];
@@ -1654,6 +1715,9 @@ int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *
16541715

16551716
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
16561717
{
1718+
if (!dev || !data)
1719+
return -1;
1720+
16571721
int res = -1;
16581722
int skipped_report_id = 0;
16591723
int report_number = data[0];
@@ -1727,20 +1791,32 @@ void HID_API_EXPORT hid_close(hid_device *dev)
17271791

17281792
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
17291793
{
1794+
if (!dev)
1795+
return -1;
1796+
17301797
return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
17311798
}
17321799

17331800
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
17341801
{
1802+
if (!dev)
1803+
return -1;
1804+
17351805
return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
17361806
}
17371807

17381808
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
17391809
{
1810+
if (!dev)
1811+
return -1;
1812+
17401813
return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
17411814
}
17421815

17431816
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1817+
if (!dev)
1818+
return NULL;
1819+
17441820
if (!dev->device_info) {
17451821
struct libusb_device_descriptor desc;
17461822
libusb_device *usb_device = libusb_get_device(dev->device_handle);
@@ -1759,6 +1835,9 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
17591835

17601836
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
17611837
{
1838+
if (!dev || !string)
1839+
return -1;
1840+
17621841
wchar_t *str;
17631842

17641843
str = get_usb_string(dev->device_handle, string_index);
@@ -1775,6 +1854,9 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
17751854

17761855
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
17771856
{
1857+
if (!dev)
1858+
return -1;
1859+
17781860
return hid_get_report_descriptor_libusb(dev->device_handle, dev->interface, dev->report_descriptor_size, buf, buf_size);
17791861
}
17801862

0 commit comments

Comments
 (0)