From df2d4d09b83ef68c9c36f4d282e4f3b89cea5974 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Fri, 27 Sep 2024 14:45:04 +0200 Subject: [PATCH] Register errors on early outs --- linux/hid.c | 45 +++++++++++++++++++++++++++++++---------- mac/hid.c | 56 ++++++++++++++++++++++++++++++++++++++++----------- windows/hid.c | 53 +++++++++++++++++++++++++++++++++++++----------- 3 files changed, 119 insertions(+), 35 deletions(-) diff --git a/linux/hid.c b/linux/hid.c index b773a89b..c9c09d8f 100644 --- a/linux/hid.c +++ b/linux/hid.c @@ -171,9 +171,6 @@ static void register_global_error_format(const char *format, ...) * Use register_device_error(dev, NULL) to indicate "no error". */ static void register_device_error(hid_device *dev, const char *msg) { - if (!dev) - return; - register_error_str(&dev->last_error_str, msg); } @@ -1125,8 +1122,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } int bytes_written; @@ -1146,8 +1145,10 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } /* Set device error to none */ register_device_error(dev, NULL); @@ -1201,13 +1202,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length) { + if (!dev) { + register_global_error("Device is NULL"); + return -1; + } + return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0); } int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } /* Do all non-blocking in userspace using poll(), since it looks 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) int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } int res; @@ -1236,8 +1246,10 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } int res; @@ -1254,6 +1266,11 @@ int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device *dev, const un { int res; + if (!dev) { + register_global_error("Device is NULL"); + return -1; + } + register_device_error(dev, NULL); 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 int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } int res; @@ -1369,8 +1388,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return NULL; + } if (!dev->device_info) { // Lazy initialize device_info @@ -1383,8 +1404,10 @@ HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_devi int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } (void)string_index; (void)string; diff --git a/mac/hid.c b/mac/hid.c index 6e53f999..8e4c926a 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -276,9 +276,6 @@ static void register_global_error_format(const char *format, ...) * Use register_device_error(dev, NULL) to indicate "no error". */ static void register_device_error(hid_device *dev, const char *msg) { - if (!dev) - return; - register_error_str(&dev->last_error_str, msg); } @@ -1111,8 +1108,10 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path) static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } const unsigned char *data_to_send = data; CFIndex length_to_send = length; @@ -1156,8 +1155,15 @@ static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char static int get_report(hid_device *dev, IOHIDReportType type, unsigned char *data, size_t length) { - if (!dev || !data) + if (!dev) { + register_global_error("Device is NULL"); + return -1; + } + + if (!data) { + register_device_error(dev, "Data is NULL"); return -1; + } unsigned char *report = data; CFIndex report_length = length; @@ -1266,15 +1272,16 @@ static int cond_timedwait(hid_device *dev, pthread_cond_t *cond, pthread_mutex_t } return 0; - } int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) { int bytes_read = -1; - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return bytes_read; + } /* Lock the access to the report list. */ pthread_mutex_lock(&dev->mutex); @@ -1353,13 +1360,20 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length) { + if (!dev) { + register_global_error("Device is NULL"); + return -1; + } + return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0); } int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } /* All Nonblocking operation is handled by the library. */ dev->blocking = !nonblock; @@ -1501,8 +1515,10 @@ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *s } HID_API_EXPORT struct hid_device_info *HID_API_CALL hid_get_device_info(hid_device *dev) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return NULL; + } if (!dev->device_info) { 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 int HID_API_EXPORT_CALL hid_darwin_get_location_id(hid_device *dev, uint32_t *location_id) { - if (!dev || !location_id) + if (!dev) { + register_global_error("Device is NULL"); + return -1; + } + + if (!location_id) { + register_device_error(dev, "Location ID is NULL"); return -1; + } int res = get_int_property(dev->device_handle, CFSTR(kIOHIDLocationIDKey)); if (res != 0) { @@ -1552,16 +1575,25 @@ int HID_API_EXPORT_CALL hid_darwin_get_open_exclusive(void) int HID_API_EXPORT_CALL hid_darwin_is_device_open_exclusive(hid_device *dev) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } return (dev->open_options == kIOHIDOptionsTypeSeizeDevice) ? 1 : 0; } int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size) { - if (!dev) + if (!dev) { + register_global_error("Device is NULL"); return -1; + } + + if (!buf) { + register_device_error(dev, "Buffer is NULL"); + return -1; + } CFTypeRef ref = IOHIDDeviceGetProperty(dev->device_handle, CFSTR(kIOHIDReportDescriptorKey)); if (ref != NULL && CFGetTypeID(ref) == CFDataGetTypeID()) { diff --git a/windows/hid.c b/windows/hid.c index 1e096bac..41382dd9 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -1089,8 +1089,10 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * unsigned char *buf; - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return function_result; + } if (!data || !length) { register_string_error(dev, L"Zero buffer/length"); @@ -1166,8 +1168,10 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } DWORD bytes_read = 0; size_t copy_len = 0; @@ -1260,8 +1264,10 @@ int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, s int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *dev, int nonblock) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } dev->blocking = !nonblock; return 0; /* Success */ @@ -1269,8 +1275,10 @@ int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *dev, int nonbloc int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } BOOL res = FALSE; unsigned char *buf; @@ -1319,8 +1327,10 @@ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const u static int hid_get_report(hid_device *dev, DWORD report_type, unsigned char *data, size_t length) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } BOOL res; DWORD bytes_returned = 0; @@ -1376,6 +1386,11 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned int HID_API_EXPORT HID_API_CALL hid_send_output_report(hid_device* dev, const unsigned char* data, size_t length) { + if (!dev) { + register_global_error(L"Device is NULL"); + return -1; + } + BOOL res = FALSE; unsigned char *buf; size_t length_to_send; @@ -1437,8 +1452,10 @@ void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev) int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } if (!string || !maxlen) { register_string_error(dev, L"Zero buffer/length"); @@ -1460,8 +1477,10 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } if (!string || !maxlen) { register_string_error(dev, L"Zero buffer/length"); @@ -1483,8 +1502,10 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wch int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } if (!string || !maxlen) { register_string_error(dev, L"Zero buffer/length"); @@ -1505,8 +1526,10 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *de } HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info(hid_device *dev) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return NULL; + } if (!dev->device_info) { @@ -1519,8 +1542,10 @@ HID_API_EXPORT struct hid_device_info * HID_API_CALL hid_get_device_info(hid_dev int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } BOOL res; @@ -1542,8 +1567,10 @@ int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *container_id) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } wchar_t *interface_path = NULL, *device_id = NULL; CONFIGRET cr = CR_FAILURE; @@ -1597,8 +1624,10 @@ int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *conta int HID_API_EXPORT_CALL hid_get_report_descriptor(hid_device *dev, unsigned char *buf, size_t buf_size) { - if (!dev) + if (!dev) { + register_global_error(L"Device is NULL"); return -1; + } PHIDP_PREPARSED_DATA pp_data = NULL;