Skip to content

Commit 9b302c4

Browse files
committed
Register errors on early outs
1 parent 0346fc6 commit 9b302c4

File tree

3 files changed

+119
-29
lines changed

3 files changed

+119
-29
lines changed

linux/hid.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11251125

11261126
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
11271127
{
1128-
if (!dev)
1128+
if (!dev) {
1129+
register_global_error("Device is NULL");
11291130
return -1;
1131+
}
11301132

11311133
int bytes_written;
11321134

@@ -1146,8 +1148,10 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
11461148

11471149
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
11481150
{
1149-
if (!dev)
1151+
if (!dev) {
1152+
register_global_error("Device is NULL");
11501153
return -1;
1154+
}
11511155

11521156
/* Set device error to none */
11531157
register_device_error(dev, NULL);
@@ -1201,13 +1205,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12011205

12021206
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
12031207
{
1208+
if (!dev) {
1209+
register_global_error("Device is NULL");
1210+
return -1;
1211+
}
1212+
12041213
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
12051214
}
12061215

12071216
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
12081217
{
1209-
if (!dev)
1218+
if (!dev) {
1219+
register_global_error("Device is NULL");
12101220
return -1;
1221+
}
12111222

12121223
/* Do all non-blocking in userspace using poll(), since it looks
12131224
like there's a bug in the kernel in some versions where
@@ -1220,8 +1231,10 @@ int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
12201231

12211232
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
12221233
{
1223-
if (!dev)
1234+
if (!dev) {
1235+
register_global_error("Device is NULL");
12241236
return -1;
1237+
}
12251238

12261239
int res;
12271240

@@ -1236,8 +1249,10 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
12361249

12371250
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
12381251
{
1239-
if (!dev)
1252+
if (!dev) {
1253+
register_global_error("Device is NULL");
12401254
return -1;
1255+
}
12411256

12421257
int res;
12431258

@@ -1254,6 +1269,11 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12541269
{
12551270
int res;
12561271

1272+
if (!dev) {
1273+
register_global_error("Device is NULL");
1274+
return -1;
1275+
}
1276+
12571277
register_device_error(dev, NULL);
12581278

12591279
res = ioctl(dev->device_handle, HIDIOCSOUTPUT(length), data);
@@ -1265,8 +1285,10 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12651285

12661286
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
12671287
{
1268-
if (!dev)
1288+
if (!dev) {
1289+
register_global_error("Device is NULL");
12691290
return -1;
1291+
}
12701292

12711293
int res;
12721294

@@ -1369,8 +1391,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
13691391

13701392

13711393
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1372-
if (!dev)
1394+
if (!dev) {
1395+
register_global_error("Device is NULL");
13731396
return NULL;
1397+
}
13741398

13751399
if (!dev->device_info) {
13761400
// Lazy initialize device_info
@@ -1383,8 +1407,10 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
13831407

13841408
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
13851409
{
1386-
if (!dev)
1410+
if (!dev) {
1411+
register_global_error("Device is NULL");
13871412
return -1;
1413+
}
13881414

13891415
(void)string_index;
13901416
(void)string;

mac/hid.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11111111

11121112
static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
11131113
{
1114-
if (!dev)
1114+
if (!dev) {
1115+
register_global_error("Device is NULL");
11151116
return -1;
1117+
}
11161118

11171119
const unsigned char *data_to_send = data;
11181120
CFIndex length_to_send = length;
@@ -1156,8 +1158,15 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char
11561158

11571159
static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length)
11581160
{
1159-
if (!dev || !data)
1161+
if (!dev) {
1162+
register_global_error("Device is NULL");
11601163
return -1;
1164+
}
1165+
1166+
if (!data) {
1167+
register_global_error("Data is NULL");
1168+
return -1;
1169+
}
11611170

11621171
unsigned char *report = data;
11631172
CFIndex report_length = length;
@@ -1266,15 +1275,16 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t
12661275
}
12671276

12681277
return 0;
1269-
12701278
}
12711279

12721280
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
12731281
{
12741282
int bytes_read = -1;
12751283

1276-
if (!dev)
1284+
if (!dev) {
1285+
register_global_error("Device is NULL");
12771286
return bytes_read;
1287+
}
12781288

12791289
/* Lock the access to the report list. */
12801290
pthread_mutex_lock(&dev->mutex);
@@ -1353,13 +1363,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
13531363

13541364
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
13551365
{
1366+
if (!dev) {
1367+
register_global_error("Device is NULL");
1368+
return -1;
1369+
}
1370+
13561371
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
13571372
}
13581373

13591374
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
13601375
{
1361-
if (!dev)
1376+
if (!dev) {
1377+
register_global_error("Device is NULL");
13621378
return -1;
1379+
}
13631380

13641381
/* All Nonblocking operation is handled by the library. */
13651382
dev->blocking = !nonblock;
@@ -1501,8 +1518,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
15011518
}
15021519

15031520
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1504-
if (!dev)
1521+
if (!dev) {
1522+
register_global_error("Device is NULL");
15051523
return NULL;
1524+
}
15061525

15071526
if (!dev->device_info) {
15081527
dev->device_info = create_device_info(dev->device_handle);
@@ -1527,8 +1546,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
15271546

15281547
int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id)
15291548
{
1530-
if (!dev || !location_id)
1549+
if (!dev) {
1550+
register_global_error("Device is NULL");
15311551
return -1;
1552+
}
1553+
1554+
if (!location_id) {
1555+
register_global_error("Location ID is NULL");
1556+
return -1;
1557+
}
15321558

15331559
int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey));
15341560
if (res != 0) {
@@ -1552,16 +1578,25 @@ int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive(void)
15521578

15531579
int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive(hid_device *dev)
15541580
{
1555-
if (!dev)
1581+
if (!dev) {
1582+
register_global_error("Device is NULL");
15561583
return -1;
1584+
}
15571585

15581586
return (dev->open_options == kIOHIDOptionsTypeSeizeDevice) ? 1 : 0;
15591587
}
15601588

15611589
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
15621590
{
1563-
if (!dev)
1591+
if (!dev) {
1592+
register_global_error("Device is NULL");
15641593
return -1;
1594+
}
1595+
1596+
if (!buf) {
1597+
register_global_error("Buffer is NULL");
1598+
return -1;
1599+
}
15651600

15661601
CFTypeRef ref = IOHIDDeviceGetProperty(dev->device_handle, CFSTR(kIOHIDReportDescriptorKey));
15671602
if (ref != NULL && CFGetTypeID(ref) == CFDataGetTypeID()) {

0 commit comments

Comments
 (0)