Skip to content

Commit df2d4d0

Browse files
committed
Register errors on early outs
1 parent 9fab199 commit df2d4d0

File tree

3 files changed

+119
-35
lines changed

3 files changed

+119
-35
lines changed

linux/hid.c

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,6 @@ 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)
175-
return;
176-
177174
register_error_str(&dev->last_error_str, msg);
178175
}
179176

@@ -1125,8 +1122,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11251122

11261123
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
11271124
{
1128-
if (!dev)
1125+
if (!dev) {
1126+
register_global_error("Device is NULL");
11291127
return -1;
1128+
}
11301129

11311130
int bytes_written;
11321131

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

11471146
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
11481147
{
1149-
if (!dev)
1148+
if (!dev) {
1149+
register_global_error("Device is NULL");
11501150
return -1;
1151+
}
11511152

11521153
/* Set device error to none */
11531154
register_device_error(dev, NULL);
@@ -1201,13 +1202,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12011202

12021203
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
12031204
{
1205+
if (!dev) {
1206+
register_global_error("Device is NULL");
1207+
return -1;
1208+
}
1209+
12041210
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
12051211
}
12061212

12071213
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
12081214
{
1209-
if (!dev)
1215+
if (!dev) {
1216+
register_global_error("Device is NULL");
12101217
return -1;
1218+
}
12111219

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

12211229
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
12221230
{
1223-
if (!dev)
1231+
if (!dev) {
1232+
register_global_error("Device is NULL");
12241233
return -1;
1234+
}
12251235

12261236
int res;
12271237

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

12371247
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
12381248
{
1239-
if (!dev)
1249+
if (!dev) {
1250+
register_global_error("Device is NULL");
12401251
return -1;
1252+
}
12411253

12421254
int res;
12431255

@@ -1254,6 +1266,11 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un
12541266
{
12551267
int res;
12561268

1269+
if (!dev) {
1270+
register_global_error("Device is NULL");
1271+
return -1;
1272+
}
1273+
12571274
register_device_error(dev, NULL);
12581275

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

12661283
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
12671284
{
1268-
if (!dev)
1285+
if (!dev) {
1286+
register_global_error("Device is NULL");
12691287
return -1;
1288+
}
12701289

12711290
int res;
12721291

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

13701389

13711390
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1372-
if (!dev)
1391+
if (!dev) {
1392+
register_global_error("Device is NULL");
13731393
return NULL;
1394+
}
13741395

13751396
if (!dev->device_info) {
13761397
// Lazy initialize device_info
@@ -1383,8 +1404,10 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi
13831404

13841405
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
13851406
{
1386-
if (!dev)
1407+
if (!dev) {
1408+
register_global_error("Device is NULL");
13871409
return -1;
1410+
}
13881411

13891412
(void)string_index;
13901413
(void)string;

mac/hid.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ 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)
280-
return;
281-
282279
register_error_str(&dev->last_error_str, msg);
283280
}
284281

@@ -1111,8 +1108,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
11111108

11121109
static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
11131110
{
1114-
if (!dev)
1111+
if (!dev) {
1112+
register_global_error("Device is NULL");
11151113
return -1;
1114+
}
11161115

11171116
const unsigned char *data_to_send = data;
11181117
CFIndex length_to_send = length;
@@ -1156,8 +1155,15 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char
11561155

11571156
static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length)
11581157
{
1159-
if (!dev || !data)
1158+
if (!dev) {
1159+
register_global_error("Device is NULL");
1160+
return -1;
1161+
}
1162+
1163+
if (!data) {
1164+
register_device_error(dev, "Data is NULL");
11601165
return -1;
1166+
}
11611167

11621168
unsigned char *report = data;
11631169
CFIndex report_length = length;
@@ -1266,15 +1272,16 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t
12661272
}
12671273

12681274
return 0;
1269-
12701275
}
12711276

12721277
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
12731278
{
12741279
int bytes_read = -1;
12751280

1276-
if (!dev)
1281+
if (!dev) {
1282+
register_global_error("Device is NULL");
12771283
return bytes_read;
1284+
}
12781285

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

13541361
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
13551362
{
1363+
if (!dev) {
1364+
register_global_error("Device is NULL");
1365+
return -1;
1366+
}
1367+
13561368
return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
13571369
}
13581370

13591371
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
13601372
{
1361-
if (!dev)
1373+
if (!dev) {
1374+
register_global_error("Device is NULL");
13621375
return -1;
1376+
}
13631377

13641378
/* All Nonblocking operation is handled by the library. */
13651379
dev->blocking = !nonblock;
@@ -1501,8 +1515,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s
15011515
}
15021516

15031517
HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) {
1504-
if (!dev)
1518+
if (!dev) {
1519+
register_global_error("Device is NULL");
15051520
return NULL;
1521+
}
15061522

15071523
if (!dev->device_info) {
15081524
dev->device_info = create_device_info(dev->device_handle);
@@ -1527,8 +1543,15 @@ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index
15271543

15281544
int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id)
15291545
{
1530-
if (!dev || !location_id)
1546+
if (!dev) {
1547+
register_global_error("Device is NULL");
1548+
return -1;
1549+
}
1550+
1551+
if (!location_id) {
1552+
register_device_error(dev, "Location ID is NULL");
15311553
return -1;
1554+
}
15321555

15331556
int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey));
15341557
if (res != 0) {
@@ -1552,16 +1575,25 @@ int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive(void)
15521575

15531576
int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive(hid_device *dev)
15541577
{
1555-
if (!dev)
1578+
if (!dev) {
1579+
register_global_error("Device is NULL");
15561580
return -1;
1581+
}
15571582

15581583
return (dev->open_options == kIOHIDOptionsTypeSeizeDevice) ? 1 : 0;
15591584
}
15601585

15611586
int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size)
15621587
{
1563-
if (!dev)
1588+
if (!dev) {
1589+
register_global_error("Device is NULL");
15641590
return -1;
1591+
}
1592+
1593+
if (!buf) {
1594+
register_device_error(dev, "Buffer is NULL");
1595+
return -1;
1596+
}
15651597

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

0 commit comments

Comments
 (0)