From 1c6e039d0142e224734e079165b16892ea911064 Mon Sep 17 00:00:00 2001 From: ksv1986 Date: Wed, 15 Apr 2020 20:25:59 +0300 Subject: [PATCH] hidpp10: fix handling of hidpp10_device_new() failures Signed-off-by: ksv1986 --- src/driver-hidpp10.c | 4 ++-- src/hidpp10.c | 12 ++++++++---- src/hidpp10.h | 5 +++-- src/liblur.c | 16 ++++++++-------- tools/hidpp10-dump-page.c | 4 +++- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/driver-hidpp10.c b/src/driver-hidpp10.c index eaa83d19..9690b0d2 100644 --- a/src/driver-hidpp10.c +++ b/src/driver-hidpp10.c @@ -669,9 +669,9 @@ hidpp10drv_probe(struct ratbag_device *device) * If there is a special need like for G700(s), then add a DeviceIndex * entry to the .device file. */ - dev = hidpp10_device_new(&base, device_idx, type, profile_count); + rc = hidpp10_device_new(&base, device_idx, type, profile_count, &dev); - if (!dev) { + if (rc) { log_error(device->ratbag, "Failed to get HID++1.0 device for %s\n", device->name); diff --git a/src/hidpp10.c b/src/hidpp10.c index 077c6fd7..6ae247ee 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -2759,12 +2759,14 @@ hidpp10_get_device_info(struct hidpp10_device *dev) return hidpp10_get_current_profile(dev, ¤t_profile); } -struct hidpp10_device* +int hidpp10_device_new(const struct hidpp_device *base, int idx, enum hidpp10_profile_type type, - unsigned int profile_count) + unsigned int profile_count, + struct hidpp10_device **out) { + int rc; struct hidpp10_device *dev; dev = zalloc(sizeof(*dev)); @@ -2775,12 +2777,14 @@ hidpp10_device_new(const struct hidpp_device *base, dev->profile_count = profile_count; dev->profiles = zalloc(dev->profile_count * sizeof(struct hidpp10_profile)); - if (hidpp10_get_device_info(dev) != 0) { + if ((rc = hidpp10_get_device_info(dev)) != 0) { hidpp10_device_destroy(dev); dev = NULL; } - return dev; + *out = dev; + + return rc; } int diff --git a/src/hidpp10.h b/src/hidpp10.h index 40018188..7d2e96d0 100644 --- a/src/hidpp10.h +++ b/src/hidpp10.h @@ -69,11 +69,12 @@ struct hidpp10_device { unsigned int profile_count; }; -struct hidpp10_device* +int hidpp10_device_new(const struct hidpp_device *base, int idx, enum hidpp10_profile_type type, - unsigned int profile_count); + unsigned int profile_count, + struct hidpp10_device **out); int hidpp10_device_read_profiles(struct hidpp10_device *dev); diff --git a/src/liblur.c b/src/liblur.c index 8ac04320..826caec7 100644 --- a/src/liblur.c +++ b/src/liblur.c @@ -140,20 +140,21 @@ hidraw_is_receiver(int fd) return true; } -static struct hidpp10_device * -hidpp10_init(int fd) +static int +hidpp10_init(int fd, struct hidpp10_device **out) { struct hidpp_device base; hidpp_device_init(&base, fd); return hidpp10_device_new(&base, HIDPP_RECEIVER_IDX, - HIDPP10_PROFILE_UNKNOWN, 1); + HIDPP10_PROFILE_UNKNOWN, 1, out); } _EXPORT_ int lur_receiver_new_from_hidraw(int fd, void *userdata, struct lur_receiver **out) { + int rc; struct lur_receiver *receiver; if (!hidraw_is_receiver(fd)) @@ -165,8 +166,8 @@ lur_receiver_new_from_hidraw(int fd, void *userdata, struct lur_receiver **out) receiver->userdata = userdata; list_init(&receiver->devices); - receiver->hidppdev = hidpp10_init(fd); - if (!receiver->hidppdev) + rc = hidpp10_init(fd, &receiver->hidppdev); + if (rc) goto error; *out = receiver; @@ -174,8 +175,7 @@ lur_receiver_new_from_hidraw(int fd, void *userdata, struct lur_receiver **out) error: free(receiver); - return -errno; - + return rc; } _EXPORT_ int @@ -203,7 +203,7 @@ lur_receiver_enumerate(struct lur_receiver *lur, uint32_t serial; bool is_new_device = true; - d = hidpp10_device_new(&base, i, HIDPP10_PROFILE_UNKNOWN, 1); + rc = hidpp10_device_new(&base, i, HIDPP10_PROFILE_UNKNOWN, 1, &d); if (!d) continue; diff --git a/tools/hidpp10-dump-page.c b/tools/hidpp10-dump-page.c index 8bc3dd03..5d27659a 100644 --- a/tools/hidpp10-dump-page.c +++ b/tools/hidpp10-dump-page.c @@ -98,7 +98,9 @@ main(int argc, char **argv) error(1, errno, "Failed to open path %s", path); hidpp_device_init(&base, fd); - dev = hidpp10_device_new(&base, HIDPP_WIRED_DEVICE_IDX, HIDPP10_PROFILE_UNKNOWN, 5); + rc = hidpp10_device_new(&base, HIDPP_WIRED_DEVICE_IDX, HIDPP10_PROFILE_UNKNOWN, 5, &dev); + if (rc) + return rc; if (argc == 2) rc = dump_all_pages(dev);