@@ -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 ;
@@ -536,6 +548,9 @@ static int hid_get_report_descriptor_libusb(libusb_device_handle *handle, int in
536
548
*/
537
549
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 )
538
550
{
551
+ if (!cur_dev )
552
+ return ;
553
+
539
554
unsigned char hid_report_descriptor [HID_API_MAX_REPORT_DESCRIPTOR_SIZE ];
540
555
unsigned short page = 0 , usage = 0 ;
541
556
@@ -632,6 +647,9 @@ static struct hid_device_info * create_device_info_for_device(libusb_device *dev
632
647
633
648
static uint16_t get_report_descriptor_size_from_interface_descriptors (const struct libusb_interface_descriptor * intf_desc )
634
649
{
650
+ if (!intf_desc )
651
+ return 0 ;
652
+
635
653
int i = 0 ;
636
654
int found_hid_report_descriptor = 0 ;
637
655
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
685
703
686
704
static int is_xbox360 (unsigned short vendor_id , const struct libusb_interface_descriptor * intf_desc )
687
705
{
706
+ if (!intf_desc )
707
+ return 0 ;
708
+
688
709
static const int xb360_iface_subclass = 93 ;
689
710
static const int xb360_iface_protocol = 1 ; /* Wired */
690
711
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
733
754
734
755
static int is_xboxone (unsigned short vendor_id , const struct libusb_interface_descriptor * intf_desc )
735
756
{
757
+ if (!intf_desc )
758
+ return 0 ;
759
+
736
760
static const int xb1_iface_subclass = 71 ;
737
761
static const int xb1_iface_protocol = 208 ;
738
762
static const int supported_vendors [] = {
@@ -769,6 +793,8 @@ static int should_enumerate_interface(unsigned short vendor_id, const struct lib
769
793
#if 0
770
794
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
795
#endif
796
+ if (!intf_desc )
797
+ return 0 ;
772
798
773
799
if (intf_desc -> bInterfaceClass == LIBUSB_CLASS_HID )
774
800
return 1 ;
@@ -950,6 +976,9 @@ hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const
950
976
951
977
static void LIBUSB_CALL read_callback (struct libusb_transfer * transfer )
952
978
{
979
+ if (!transfer )
980
+ return ;
981
+
953
982
hid_device * dev = transfer -> user_data ;
954
983
int res ;
955
984
@@ -1018,6 +1047,9 @@ static void LIBUSB_CALL read_callback(struct libusb_transfer *transfer)
1018
1047
1019
1048
static void * read_thread (void * param )
1020
1049
{
1050
+ if (!param )
1051
+ return NULL ;
1052
+
1021
1053
int res ;
1022
1054
hid_device * dev = param ;
1023
1055
uint8_t * buf ;
@@ -1118,6 +1150,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
1118
1150
1119
1151
(void )idProduct ;
1120
1152
1153
+ if (!conf_desc )
1154
+ return ;
1155
+
1121
1156
for (j = 0 ; j < conf_desc -> bNumInterfaces ; j ++ ) {
1122
1157
const struct libusb_interface * intf = & conf_desc -> interface [j ];
1123
1158
for (k = 0 ; k < intf -> num_altsetting ; k ++ ) {
@@ -1158,6 +1193,9 @@ static void init_xboxone(libusb_device_handle *device_handle, unsigned short idV
1158
1193
1159
1194
static int hidapi_initialize_device (hid_device * dev , const struct libusb_interface_descriptor * intf_desc , const struct libusb_config_descriptor * conf_desc )
1160
1195
{
1196
+ if (!conf_desc )
1197
+ return 0 ;
1198
+
1161
1199
int i = 0 ;
1162
1200
int res = 0 ;
1163
1201
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
1413
1451
1414
1452
int HID_API_EXPORT hid_write (hid_device * dev , const unsigned char * data , size_t length )
1415
1453
{
1454
+ if (!dev )
1455
+ return -1 ;
1456
+
1416
1457
int res ;
1417
1458
int report_number ;
1418
1459
int skipped_report_id = 0 ;
@@ -1455,6 +1496,9 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
1455
1496
This should be called with dev->mutex locked. */
1456
1497
static int return_data (hid_device * dev , unsigned char * data , size_t length )
1457
1498
{
1499
+ if (!dev || !data )
1500
+ return 0 ;
1501
+
1458
1502
/* Copy the data out of the linked list item (rpt) into the
1459
1503
return buffer (data), and delete the liked list item. */
1460
1504
struct input_report * rpt = dev -> input_reports ;
@@ -1469,13 +1513,19 @@ static int return_data(hid_device *dev, unsigned char *data, size_t length)
1469
1513
1470
1514
static void cleanup_mutex (void * param )
1471
1515
{
1516
+ if (!param )
1517
+ return ;
1518
+
1472
1519
hid_device * dev = param ;
1473
1520
hidapi_thread_mutex_unlock (& dev -> thread_state );
1474
1521
}
1475
1522
1476
1523
1477
1524
int HID_API_EXPORT hid_read_timeout (hid_device * dev , unsigned char * data , size_t length , int milliseconds )
1478
1525
{
1526
+ if (!dev )
1527
+ return -1 ;
1528
+
1479
1529
#if 0
1480
1530
int transferred ;
1481
1531
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)
1564
1614
1565
1615
int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
1566
1616
{
1617
+ if (!dev )
1618
+ return -1 ;
1619
+
1567
1620
dev -> blocking = !nonblock ;
1568
1621
1569
1622
return 0 ;
@@ -1572,6 +1625,9 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
1572
1625
1573
1626
int HID_API_EXPORT hid_send_feature_report (hid_device * dev , const unsigned char * data , size_t length )
1574
1627
{
1628
+ if (!dev || !data )
1629
+ return -1 ;
1630
+
1575
1631
int res = -1 ;
1576
1632
int skipped_report_id = 0 ;
1577
1633
int report_number = data [0 ];
@@ -1602,6 +1658,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
1602
1658
1603
1659
int HID_API_EXPORT hid_get_feature_report (hid_device * dev , unsigned char * data , size_t length )
1604
1660
{
1661
+ if (!dev || !data )
1662
+ return -1 ;
1663
+
1605
1664
int res = -1 ;
1606
1665
int skipped_report_id = 0 ;
1607
1666
int report_number = data [0 ];
@@ -1632,6 +1691,9 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
1632
1691
1633
1692
int HID_API_EXPORT hid_send_output_report (hid_device * dev , const unsigned char * data , size_t length )
1634
1693
{
1694
+ if (!dev || !data )
1695
+ return -1 ;
1696
+
1635
1697
int res = -1 ;
1636
1698
int skipped_report_id = 0 ;
1637
1699
int report_number = data [0 ];
@@ -1662,6 +1724,9 @@ int HID_API_EXPORT hid_send_output_report(hid_device *dev, const unsigned char *
1662
1724
1663
1725
int HID_API_EXPORT HID_API_CALL hid_get_input_report (hid_device * dev , unsigned char * data , size_t length )
1664
1726
{
1727
+ if (!dev || !data )
1728
+ return -1 ;
1729
+
1665
1730
int res = -1 ;
1666
1731
int skipped_report_id = 0 ;
1667
1732
int report_number = data [0 ];
@@ -1735,20 +1800,32 @@ void HID_API_EXPORT hid_close(hid_device *dev)
1735
1800
1736
1801
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device * dev , wchar_t * string , size_t maxlen )
1737
1802
{
1803
+ if (!dev )
1804
+ return -1 ;
1805
+
1738
1806
return hid_get_indexed_string (dev , dev -> manufacturer_index , string , maxlen );
1739
1807
}
1740
1808
1741
1809
int HID_API_EXPORT_CALL hid_get_product_string (hid_device * dev , wchar_t * string , size_t maxlen )
1742
1810
{
1811
+ if (!dev )
1812
+ return -1 ;
1813
+
1743
1814
return hid_get_indexed_string (dev , dev -> product_index , string , maxlen );
1744
1815
}
1745
1816
1746
1817
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device * dev , wchar_t * string , size_t maxlen )
1747
1818
{
1819
+ if (!dev )
1820
+ return -1 ;
1821
+
1748
1822
return hid_get_indexed_string (dev , dev -> serial_index , string , maxlen );
1749
1823
}
1750
1824
1751
1825
HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info (hid_device * dev ) {
1826
+ if (!dev )
1827
+ return NULL ;
1828
+
1752
1829
if (!dev -> device_info ) {
1753
1830
struct libusb_device_descriptor desc ;
1754
1831
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
1767
1844
1768
1845
int HID_API_EXPORT_CALL hid_get_indexed_string (hid_device * dev , int string_index , wchar_t * string , size_t maxlen )
1769
1846
{
1847
+ if (!dev || !string )
1848
+ return -1 ;
1849
+
1770
1850
wchar_t * str ;
1771
1851
1772
1852
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
1783
1863
1784
1864
int HID_API_EXPORT_CALL hid_get_report_descriptor (hid_device * dev , unsigned char * buf , size_t buf_size )
1785
1865
{
1866
+ if (!dev )
1867
+ return -1 ;
1868
+
1786
1869
return hid_get_report_descriptor_libusb (dev -> device_handle , dev -> interface , dev -> report_descriptor_size , buf , buf_size );
1787
1870
}
1788
1871
0 commit comments