@@ -139,6 +139,9 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length);
139
139
static hid_device * new_hid_device (void )
140
140
{
141
141
hid_device * dev = (hid_device * ) calloc (1 , sizeof (hid_device ));
142
+ if (!dev )
143
+ return NULL ;
144
+
142
145
dev -> blocking = 1 ;
143
146
144
147
hidapi_thread_state_init (& dev -> thread_state );
@@ -148,6 +151,9 @@ static hid_device *new_hid_device(void)
148
151
149
152
static void free_hid_device (hid_device * dev )
150
153
{
154
+ if (!dev )
155
+ return ;
156
+
151
157
/* Clean up the thread objects */
152
158
hidapi_thread_state_destroy (& dev -> thread_state );
153
159
@@ -169,6 +175,9 @@ static void register_error(hid_device *dev, const char *op)
169
175
Only call with a num_bytes of 0, 1, 2, or 4. */
170
176
static uint32_t get_bytes (uint8_t * rpt , size_t len , size_t num_bytes , size_t cur )
171
177
{
178
+ if (!rpt )
179
+ return 0 ;
180
+
172
181
/* Return if there aren't enough bytes. */
173
182
if (cur + num_bytes >= len )
174
183
return 0 ;
@@ -198,6 +207,9 @@ static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur
198
207
static int get_usage (uint8_t * report_descriptor , size_t size ,
199
208
unsigned short * usage_page , unsigned short * usage )
200
209
{
210
+ if (!report_descriptor || !usage_page || !usage )
211
+ return -1 ;
212
+
201
213
unsigned int i = 0 ;
202
214
int size_code ;
203
215
int data_len , key_size ;
@@ -546,8 +558,10 @@ static void fill_device_info_usage(struct hid_device_info *cur_dev, libusb_devic
546
558
get_usage (hid_report_descriptor , res , & page , & usage );
547
559
}
548
560
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
+ }
551
565
}
552
566
553
567
#ifdef INVASIVE_GET_USAGE
@@ -632,6 +646,9 @@ static struct hid_device_info * create_device_info_for_device(libusb_device *dev
632
646
633
647
static uint16_t get_report_descriptor_size_from_interface_descriptors (const struct libusb_interface_descriptor * intf_desc )
634
648
{
649
+ if (!intf_desc )
650
+ return 0 ;
651
+
635
652
int i = 0 ;
636
653
int found_hid_report_descriptor = 0 ;
637
654
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
685
702
686
703
static int is_xbox360 (unsigned short vendor_id , const struct libusb_interface_descriptor * intf_desc )
687
704
{
705
+ if (!intf_desc )
706
+ return 0 ;
707
+
688
708
static const int xb360_iface_subclass = 93 ;
689
709
static const int xb360_iface_protocol = 1 ; /* Wired */
690
710
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
733
753
734
754
static int is_xboxone (unsigned short vendor_id , const struct libusb_interface_descriptor * intf_desc )
735
755
{
756
+ if (!intf_desc )
757
+ return 0 ;
758
+
736
759
static const int xb1_iface_subclass = 71 ;
737
760
static const int xb1_iface_protocol = 208 ;
738
761
static const int supported_vendors [] = {
@@ -769,6 +792,8 @@ static int should_enumerate_interface(unsigned short vendor_id, const struct lib
769
792
#if 0
770
793
printf ("Checking interface 0x%x %d/%d/%d/%d\n" , vendor_id , intf_desc -> bInterfaceNumber , intf_desc -> bInterfaceClass , intf_desc -> bInterfaceSubClass , intf_desc -> bInterfaceProtocol );
771
794
#endif
795
+ if (!intf_desc )
796
+ return 0 ;
772
797
773
798
if (intf_desc -> bInterfaceClass == LIBUSB_CLASS_HID )
774
799
return 1 ;
@@ -950,6 +975,9 @@ hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const
950
975
951
976
static void LIBUSB_CALL read_callback (struct libusb_transfer * transfer )
952
977
{
978
+ if (!transfer )
979
+ return ;
980
+
953
981
hid_device * dev = transfer -> user_data ;
954
982
int res ;
955
983
@@ -1018,6 +1046,9 @@ static void LIBUSB_CALL read_callback(struct libusb_transfer *transfer)
1018
1046
1019
1047
static void * read_thread (void * param )
1020
1048
{
1049
+ if (!param )
1050
+ return NULL ;
1051
+
1021
1052
int res ;
1022
1053
hid_device * dev = param ;
1023
1054
uint8_t * buf ;
@@ -1118,6 +1149,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
1118
1149
1119
1150
(void )idProduct ;
1120
1151
1152
+ if (!conf_desc )
1153
+ return ;
1154
+
1121
1155
for (j = 0 ; j < conf_desc -> bNumInterfaces ; j ++ ) {
1122
1156
const struct libusb_interface * intf = & conf_desc -> interface [j ];
1123
1157
for (k = 0 ; k < intf -> num_altsetting ; k ++ ) {
@@ -1158,6 +1192,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
1158
1192
1159
1193
static int hidapi_initialize_device (hid_device * dev , const struct libusb_interface_descriptor * intf_desc , const struct libusb_config_descriptor * conf_desc )
1160
1194
{
1195
+ if (!conf_desc )
1196
+ return 0 ;
1197
+
1161
1198
int i = 0 ;
1162
1199
int res = 0 ;
1163
1200
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
1405
1442
1406
1443
int HID_API_EXPORT hid_write (hid_device * dev , const unsigned char * data , size_t length )
1407
1444
{
1445
+ if (!dev )
1446
+ return -1 ;
1447
+
1408
1448
int res ;
1409
1449
int report_number ;
1410
1450
int skipped_report_id = 0 ;
@@ -1447,11 +1487,14 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
1447
1487
This should be called with dev->mutex locked. */
1448
1488
static int return_data (hid_device * dev , unsigned char * data , size_t length )
1449
1489
{
1490
+ if (!dev )
1491
+ return 0 ;
1492
+
1450
1493
/* Copy the data out of the linked list item (rpt) into the
1451
1494
return buffer (data), and delete the liked list item. */
1452
1495
struct input_report * rpt = dev -> input_reports ;
1453
1496
size_t len = (length < rpt -> len )? length : rpt -> len ;
1454
- if (len > 0 )
1497
+ if (data && len > 0 )
1455
1498
memcpy (data , rpt -> data , len );
1456
1499
dev -> input_reports = rpt -> next ;
1457
1500
free (rpt -> data );
@@ -1461,13 +1504,19 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length)
1461
1504
1462
1505
static void cleanup_mutex (void * param )
1463
1506
{
1507
+ if (!param )
1508
+ return ;
1509
+
1464
1510
hid_device * dev = param ;
1465
1511
hidapi_thread_mutex_unlock (& dev -> thread_state );
1466
1512
}
1467
1513
1468
1514
1469
1515
int HID_API_EXPORT hid_read_timeout (hid_device * dev , unsigned char * data , size_t length , int milliseconds )
1470
1516
{
1517
+ if (!dev )
1518
+ return -1 ;
1519
+
1471
1520
#if 0
1472
1521
int transferred ;
1473
1522
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)
1556
1605
1557
1606
int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
1558
1607
{
1608
+ if (!dev )
1609
+ return -1 ;
1610
+
1559
1611
dev -> blocking = !nonblock ;
1560
1612
1561
1613
return 0 ;
@@ -1564,6 +1616,9 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
1564
1616
1565
1617
int HID_API_EXPORT hid_send_feature_report (hid_device * dev , const unsigned char * data , size_t length )
1566
1618
{
1619
+ if (!dev || !data )
1620
+ return -1 ;
1621
+
1567
1622
int res = -1 ;
1568
1623
int skipped_report_id = 0 ;
1569
1624
int report_number = data [0 ];
@@ -1594,6 +1649,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
1594
1649
1595
1650
int HID_API_EXPORT hid_get_feature_report (hid_device * dev , unsigned char * data , size_t length )
1596
1651
{
1652
+ if (!dev || !data )
1653
+ return -1 ;
1654
+
1597
1655
int res = -1 ;
1598
1656
int skipped_report_id = 0 ;
1599
1657
int report_number = data [0 ];
@@ -1624,6 +1682,9 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
1624
1682
1625
1683
int HID_API_EXPORT hid_send_output_report (hid_device * dev , const unsigned char * data , size_t length )
1626
1684
{
1685
+ if (!dev || !data )
1686
+ return -1 ;
1687
+
1627
1688
int res = -1 ;
1628
1689
int skipped_report_id = 0 ;
1629
1690
int report_number = data [0 ];
@@ -1654,6 +1715,9 @@ int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *
1654
1715
1655
1716
int HID_API_EXPORT HID_API_CALL hid_get_input_report (hid_device * dev , unsigned char * data , size_t length )
1656
1717
{
1718
+ if (!dev || !data )
1719
+ return -1 ;
1720
+
1657
1721
int res = -1 ;
1658
1722
int skipped_report_id = 0 ;
1659
1723
int report_number = data [0 ];
@@ -1727,20 +1791,32 @@ void HID_API_EXPORT hid_close(hid_device *dev)
1727
1791
1728
1792
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device * dev , wchar_t * string , size_t maxlen )
1729
1793
{
1794
+ if (!dev )
1795
+ return -1 ;
1796
+
1730
1797
return hid_get_indexed_string (dev , dev -> manufacturer_index , string , maxlen );
1731
1798
}
1732
1799
1733
1800
int HID_API_EXPORT_CALL hid_get_product_string (hid_device * dev , wchar_t * string , size_t maxlen )
1734
1801
{
1802
+ if (!dev )
1803
+ return -1 ;
1804
+
1735
1805
return hid_get_indexed_string (dev , dev -> product_index , string , maxlen );
1736
1806
}
1737
1807
1738
1808
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device * dev , wchar_t * string , size_t maxlen )
1739
1809
{
1810
+ if (!dev )
1811
+ return -1 ;
1812
+
1740
1813
return hid_get_indexed_string (dev , dev -> serial_index , string , maxlen );
1741
1814
}
1742
1815
1743
1816
HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info (hid_device * dev ) {
1817
+ if (!dev )
1818
+ return NULL ;
1819
+
1744
1820
if (!dev -> device_info ) {
1745
1821
struct libusb_device_descriptor desc ;
1746
1822
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
1759
1835
1760
1836
int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device * dev , int string_index , wchar_t * string , size_t maxlen )
1761
1837
{
1838
+ if (!dev || !string )
1839
+ return -1 ;
1840
+
1762
1841
wchar_t * str ;
1763
1842
1764
1843
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
1775
1854
1776
1855
int HID_API_EXPORT_CALL hid_get_report_descriptor (hid_device * dev , unsigned char * buf , size_t buf_size )
1777
1856
{
1857
+ if (!dev )
1858
+ return -1 ;
1859
+
1778
1860
return hid_get_report_descriptor_libusb (dev -> device_handle , dev -> interface , dev -> report_descriptor_size , buf , buf_size );
1779
1861
}
1780
1862
0 commit comments