Skip to content

Commit 11031f3

Browse files
committed
Register errors on early outs
1 parent 0346fc6 commit 11031f3

File tree

3 files changed

+125
-31
lines changed

3 files changed

+125
-31
lines changed

linux/hid.c

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ static void register_global_error_format(const char *format, ...)
171171
* Use register_device_error(dev, NULL) to indicate "no error". */
172172
static void register_device_error(hid_device *dev, const char *msg)
173173
{
174-
if (!dev)
174+
if (!dev) {
175+
register_global_error(msg);
175176
return;
177+
}
176178

177179
register_error_str(&dev->last_error_str, msg);
178180
}
@@ -1125,8 +1127,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11251127

11261128
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
11271129
{
1128-
if (!dev)
1130+
if (!dev) {
1131+
register_global_error("Device is NULL");
11291132
return -1;
1133+
}
11301134

11311135
int bytes_written;
11321136

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

11471151
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
11481152
{
1149-
if (!dev)
1153+
if (!dev) {
1154+
register_global_error("Device is NULL");
11501155
return -1;
1156+
}
11511157

11521158
/* Set device error to none */
11531159
register_device_error(dev, NULL);
@@ -1201,13 +1207,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12011207

12021208
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
12031209
{
1210+
if (!dev) {
1211+
register_global_error("Device is NULL");
1212+
return -1;
1213+
}
1214+
12041215
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
12051216
}
12061217

12071218
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
12081219
{
1209-
if (!dev)
1220+
if (!dev) {
1221+
register_global_error("Device is NULL");
12101222
return -1;
1223+
}
12111224

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

12211234
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
12221235
{
1223-
if (!dev)
1236+
if (!dev) {
1237+
register_global_error("Device is NULL");
12241238
return -1;
1239+
}
12251240

12261241
int res;
12271242

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

12371252
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
12381253
{
1239-
if (!dev)
1254+
if (!dev) {
1255+
register_global_error("Device is NULL");
12401256
return -1;
1257+
}
12411258

12421259
int res;
12431260

@@ -1254,6 +1271,11 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12541271
{
12551272
int res;
12561273

1274+
if (!dev) {
1275+
register_global_error("Device is NULL");
1276+
return -1;
1277+
}
1278+
12571279
register_device_error(dev, NULL);
12581280

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

12661288
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
12671289
{
1268-
if (!dev)
1290+
if (!dev) {
1291+
register_global_error("Device is NULL");
12691292
return -1;
1293+
}
12701294

12711295
int res;
12721296

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

13701394

13711395
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1372-
if (!dev)
1396+
if (!dev) {
1397+
register_global_error("Device is NULL");
13731398
return NULL;
1399+
}
13741400

13751401
if (!dev->device_info) {
13761402
// Lazy initialize device_info
@@ -1383,8 +1409,10 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
13831409

13841410
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
13851411
{
1386-
if (!dev)
1412+
if (!dev) {
1413+
register_global_error("Device is NULL");
13871414
return -1;
1415+
}
13881416

13891417
(void)string_index;
13901418
(void)string;

mac/hid.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,10 @@ static void register_global_error_format(const char *format, ...)
276276
* Use register_device_error(dev, NULL) to indicate "no error". */
277277
static void register_device_error(hid_device *dev, const char *msg)
278278
{
279-
if (!dev)
279+
if (!dev) {
280+
register_global_error(msg);
280281
return;
282+
}
281283

282284
register_error_str(&dev->last_error_str, msg);
283285
}
@@ -1111,8 +1113,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11111113

11121114
static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
11131115
{
1114-
if (!dev)
1116+
if (!dev) {
1117+
register_global_error("Device is NULL");
11151118
return -1;
1119+
}
11161120

11171121
const unsigned char *data_to_send = data;
11181122
CFIndex length_to_send = length;
@@ -1156,8 +1160,15 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char
11561160

11571161
static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length)
11581162
{
1159-
if (!dev || !data)
1163+
if (!dev) {
1164+
register_global_error("Device is NULL");
1165+
return -1;
1166+
}
1167+
1168+
if (!data) {
1169+
register_device_error(dev, "Data is NULL");
11601170
return -1;
1171+
}
11611172

11621173
unsigned char *report = data;
11631174
CFIndex report_length = length;
@@ -1266,15 +1277,16 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t
12661277
}
12671278

12681279
return 0;
1269-
12701280
}
12711281

12721282
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
12731283
{
12741284
int bytes_read = -1;
12751285

1276-
if (!dev)
1286+
if (!dev) {
1287+
register_global_error("Device is NULL");
12771288
return bytes_read;
1289+
}
12781290

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

13541366
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
13551367
{
1368+
if (!dev) {
1369+
register_global_error("Device is NULL");
1370+
return -1;
1371+
}
1372+
13561373
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
13571374
}
13581375

13591376
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
13601377
{
1361-
if (!dev)
1378+
if (!dev) {
1379+
register_global_error("Device is NULL");
13621380
return -1;
1381+
}
13631382

13641383
/* All Nonblocking operation is handled by the library. */
13651384
dev->blocking = !nonblock;
@@ -1501,8 +1520,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
15011520
}
15021521

15031522
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1504-
if (!dev)
1523+
if (!dev) {
1524+
register_global_error("Device is NULL");
15051525
return NULL;
1526+
}
15061527

15071528
if (!dev->device_info) {
15081529
dev->device_info = create_device_info(dev->device_handle);
@@ -1527,8 +1548,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
15271548

15281549
int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id)
15291550
{
1530-
if (!dev || !location_id)
1551+
if (!dev) {
1552+
register_global_error("Device is NULL");
1553+
return -1;
1554+
}
1555+
1556+
if (!location_id) {
1557+
register_device_error(dev, "Location ID is NULL");
15311558
return -1;
1559+
}
15321560

15331561
int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey));
15341562
if (res != 0) {
@@ -1552,16 +1580,25 @@ int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive(void)
15521580

15531581
int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive(hid_device *dev)
15541582
{
1555-
if (!dev)
1583+
if (!dev) {
1584+
register_global_error("Device is NULL");
15561585
return -1;
1586+
}
15571587

15581588
return (dev->open_options == kIOHIDOptionsTypeSeizeDevice) ? 1 : 0;
15591589
}
15601590

15611591
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
15621592
{
1563-
if (!dev)
1593+
if (!dev) {
1594+
register_global_error("Device is NULL");
15641595
return -1;
1596+
}
1597+
1598+
if (!buf) {
1599+
register_device_error(dev, "Buffer is NULL");
1600+
return -1;
1601+
}
15651602

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

0 commit comments

Comments
 (0)