@@ -402,8 +402,8 @@ static int get_next_hid_usage(const __u8 *report_descriptor, __u32 size, struct
402
402
/* If no top-level application collection is found and usage page/usage pair is found, pair is valid
403
403
https://docs.microsoft.com/en-us/windows-hardware/drivers/hid/top-level-collections */
404
404
if (initial && usage_found && ctx -> usage_page_found ) {
405
- * usage_page = ctx -> usage_page ;
406
- return 0 ; /* success */
405
+ * usage_page = ctx -> usage_page ;
406
+ return 0 ; /* success */
407
407
}
408
408
409
409
return 1 ; /* finished processing */
@@ -448,6 +448,8 @@ static int get_hid_report_descriptor_from_sysfs(const char *sysfs_path, struct h
448
448
/* Construct <sysfs_path>/device/report_descriptor */
449
449
size_t rpt_path_len = strlen (sysfs_path ) + 25 + 1 ;
450
450
char * rpt_path = (char * ) calloc (1 , rpt_path_len );
451
+ if (!rpt_path )
452
+ return -1 ;
451
453
snprintf (rpt_path , rpt_path_len , "%s/device/report_descriptor" , sysfs_path );
452
454
453
455
res = get_hid_report_descriptor (rpt_path , rpt_desc );
@@ -784,7 +786,14 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
784
786
}
785
787
786
788
/* Usage Page and Usage */
787
- result = get_hid_report_descriptor_from_sysfs (sysfs_path , & report_desc );
789
+
790
+ if (sysfs_path ) {
791
+ result = get_hid_report_descriptor_from_sysfs (sysfs_path , & report_desc );
792
+ }
793
+ else {
794
+ result = -1 ;
795
+ }
796
+
788
797
if (result >= 0 ) {
789
798
unsigned short page = 0 , usage = 0 ;
790
799
struct hid_usage_iterator usage_iterator ;
@@ -809,7 +818,7 @@ static struct hid_device_info * create_device_info_for_device(struct udev_device
809
818
struct hid_device_info * prev_dev = cur_dev ;
810
819
811
820
if (!tmp )
812
- continue ;
821
+ break ;
813
822
cur_dev -> next = tmp ;
814
823
cur_dev = tmp ;
815
824
@@ -854,6 +863,7 @@ static struct hid_device_info * create_device_info_for_hid_device(hid_device *de
854
863
/* Create the udev object */
855
864
udev = udev_new ();
856
865
if (!udev ) {
866
+ errno = ENOMEM ;
857
867
register_device_error (dev , "Couldn't create udev context" );
858
868
return NULL ;
859
869
}
@@ -866,6 +876,7 @@ static struct hid_device_info * create_device_info_for_hid_device(hid_device *de
866
876
867
877
if (!root ) {
868
878
/* TODO: have a better error reporting via create_device_info_for_device */
879
+ errno = EIO ;
869
880
register_device_error (dev , "Couldn't create hid_device_info" );
870
881
}
871
882
@@ -1061,6 +1072,7 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
1061
1072
1062
1073
dev = new_hid_device ();
1063
1074
if (!dev ) {
1075
+ errno = ENOMEM ;
1064
1076
register_global_error ("Couldn't allocate memory" );
1065
1077
return NULL ;
1066
1078
}
@@ -1073,8 +1085,8 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
1073
1085
/* Make sure this is a HIDRAW device - responds to HIDIOCGRDESCSIZE */
1074
1086
res = ioctl (dev -> device_handle , HIDIOCGRDESCSIZE , & desc_size );
1075
1087
if (res < 0 ) {
1076
- hid_close (dev );
1077
1088
register_global_error_format ("ioctl(GRDESCSIZE) error for '%s', not a HIDRAW device?: %s" , path , strerror (errno ));
1089
+ hid_close (dev );
1078
1090
return NULL ;
1079
1091
}
1080
1092
@@ -1095,7 +1107,7 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
1095
1107
1096
1108
if (!data || (length == 0 )) {
1097
1109
errno = EINVAL ;
1098
- register_device_error (dev , strerror ( errno ) );
1110
+ register_device_error (dev , "Zero buffer/length" );
1099
1111
return -1 ;
1100
1112
}
1101
1113
@@ -1109,6 +1121,12 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
1109
1121
1110
1122
int HID_API_EXPORT hid_read_timeout (hid_device * dev , unsigned char * data , size_t length , int milliseconds )
1111
1123
{
1124
+ if (!data || (length == 0 )) {
1125
+ errno = EINVAL ;
1126
+ register_error_str (& dev -> last_read_error_str , "Zero buffer/length" );
1127
+ return -1 ;
1128
+ }
1129
+
1112
1130
/* Set device error to none */
1113
1131
register_error_str (& dev -> last_read_error_str , NULL );
1114
1132
@@ -1142,6 +1160,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
1142
1160
indicate a device disconnection. */
1143
1161
if (fds .revents & (POLLERR | POLLHUP | POLLNVAL )) {
1144
1162
// We cannot use strerror() here as no -1 was returned from poll().
1163
+ errno = EIO ;
1145
1164
register_error_str (& dev -> last_read_error_str , "hid_read_timeout: unexpected poll error (device disconnected)" );
1146
1165
return -1 ;
1147
1166
}
@@ -1186,6 +1205,12 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
1186
1205
{
1187
1206
int res ;
1188
1207
1208
+ if (!data || (length == 0 )) {
1209
+ errno = EINVAL ;
1210
+ register_device_error (dev , "Zero buffer/length" );
1211
+ return -1 ;
1212
+ }
1213
+
1189
1214
register_device_error (dev , NULL );
1190
1215
1191
1216
res = ioctl (dev -> device_handle , HIDIOCSFEATURE (length ), data );
@@ -1199,6 +1224,12 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
1199
1224
{
1200
1225
int res ;
1201
1226
1227
+ if (!data || (length == 0 )) {
1228
+ errno = EINVAL ;
1229
+ register_device_error (dev , "Zero buffer/length" );
1230
+ return -1 ;
1231
+ }
1232
+
1202
1233
register_device_error (dev , NULL );
1203
1234
1204
1235
res = ioctl (dev -> device_handle , HIDIOCGFEATURE (length ), data );
@@ -1212,6 +1243,12 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
1212
1243
{
1213
1244
int res ;
1214
1245
1246
+ if (!data || (length == 0 )) {
1247
+ errno = EINVAL ;
1248
+ register_device_error (dev , "Zero buffer/length" );
1249
+ return -1 ;
1250
+ }
1251
+
1215
1252
register_device_error (dev , NULL );
1216
1253
1217
1254
res = ioctl (dev -> device_handle , HIDIOCSOUTPUT (length ), data );
@@ -1225,6 +1262,12 @@ int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned c
1225
1262
{
1226
1263
int res ;
1227
1264
1265
+ if (!data || (length == 0 )) {
1266
+ errno = EINVAL ;
1267
+ register_device_error (dev , "Zero buffer/length" );
1268
+ return -1 ;
1269
+ }
1270
+
1228
1271
register_device_error (dev , NULL );
1229
1272
1230
1273
res = ioctl (dev -> device_handle , HIDIOCGINPUT (length ), data );
@@ -1253,6 +1296,7 @@ void HID_API_EXPORT hid_close(hid_device *dev)
1253
1296
int HID_API_EXPORT_CALL hid_get_manufacturer_string (hid_device * dev , wchar_t * string , size_t maxlen )
1254
1297
{
1255
1298
if (!string || !maxlen ) {
1299
+ errno = EINVAL ;
1256
1300
register_device_error (dev , "Zero buffer/length" );
1257
1301
return -1 ;
1258
1302
}
@@ -1277,6 +1321,7 @@ int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *st
1277
1321
int HID_API_EXPORT_CALL hid_get_product_string (hid_device * dev , wchar_t * string , size_t maxlen )
1278
1322
{
1279
1323
if (!string || !maxlen ) {
1324
+ errno = EINVAL ;
1280
1325
register_device_error (dev , "Zero buffer/length" );
1281
1326
return -1 ;
1282
1327
}
@@ -1301,6 +1346,7 @@ int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string,
1301
1346
int HID_API_EXPORT_CALL hid_get_serial_number_string (hid_device * dev , wchar_t * string , size_t maxlen )
1302
1347
{
1303
1348
if (!string || !maxlen ) {
1349
+ errno = EINVAL ;
1304
1350
register_device_error (dev , "Zero buffer/length" );
1305
1351
return -1 ;
1306
1352
}
@@ -1324,7 +1370,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
1324
1370
1325
1371
1326
1372
HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info (hid_device * dev ) {
1327
- if (!dev -> device_info ) {
1373
+ if (dev -> device_info ) {
1374
+ register_device_error (dev , NULL );
1375
+ }
1376
+ else {
1328
1377
// Lazy initialize device_info
1329
1378
dev -> device_info = create_device_info_for_hid_device (dev );
1330
1379
}
@@ -1339,6 +1388,7 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
1339
1388
(void )string ;
1340
1389
(void )maxlen ;
1341
1390
1391
+ errno = ENOSYS ;
1342
1392
register_device_error (dev , "hid_get_indexed_string: not supported by hidraw" );
1343
1393
1344
1394
return -1 ;
@@ -1348,6 +1398,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
1348
1398
int HID_API_EXPORT_CALL hid_get_report_descriptor (hid_device * dev , unsigned char * buf , size_t buf_size )
1349
1399
{
1350
1400
struct hidraw_report_descriptor rpt_desc ;
1401
+
1402
+ if (!buf || !buf_size ) {
1403
+ errno = EINVAL ;
1404
+ register_device_error (dev , "Zero buffer/length" );
1405
+ return -1 ;
1406
+ }
1407
+
1408
+ register_device_error (dev , NULL );
1409
+
1351
1410
int res = get_hid_report_descriptor_from_hidraw (dev , & rpt_desc );
1352
1411
if (res < 0 ) {
1353
1412
/* error already registered */
0 commit comments