Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix memory leaks in linux/hidraw implementation.

Zbigniew Czapiga noticed that some of the udev objects in linux/hid.c weren't
being freed properly.
  • Loading branch information...
commit abd82e60373b9864f6b23335ab46186a1a48b0bd 1 parent 75bfadb
@signal11 signal11 authored
Showing with 12 additions and 9 deletions.
  1. +12 −9 linux/hid.c
21 linux/hid.c
@@ -195,9 +195,9 @@ static int get_device_string(hid_device *dev, const char *key, wchar_t *string,
- udev_device_unref(parent);
- // udev_dev doesn't need unref'd. Not sure why, but
- // it'll throw "double free" errors.
+ udev_device_unref(udev_dev);
+ // parent doesn't need to be (and can't be) unref'd.
+ // I'm not sure why, but it'll throw double-free() errors.
return ret;
@@ -209,7 +209,6 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
- struct udev_device *dev;
struct hid_device_info *root = NULL; // return object
struct hid_device_info *cur_dev = NULL;
@@ -234,23 +233,25 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
const char *sysfs_path;
const char *dev_path;
const char *str;
+ struct udev_device *hid_dev; // The device's HID interface.
+ struct udev_device *dev; // The actual hardware device.
unsigned short dev_vid;
unsigned short dev_pid;
/* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
sysfs_path = udev_list_entry_get_name(dev_list_entry);
- dev = udev_device_new_from_syspath(udev, sysfs_path);
- dev_path = udev_device_get_devnode(dev);
+ hid_dev = udev_device_new_from_syspath(udev, sysfs_path);
+ dev_path = udev_device_get_devnode(hid_dev);
- /* The device pointed to by dev contains information about
+ /* The device pointed to by hid_dev contains information about
the hidraw device. In order to get information about the
USB device, get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". This will
be several levels up the tree, but the function will find
dev = udev_device_get_parent_with_subsystem_devtype(
- dev,
+ hid_dev,
if (!dev) {
@@ -318,7 +319,9 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
goto next;
- udev_device_unref(dev);
+ udev_device_unref(hid_dev);
+ /* dev doesn't need to be (and can't be) unref()d. It will
+ cause a double-free() error. I'm not sure why. */
/* Free the enumerator and udev objects. */
Please sign in to comment.
Something went wrong with that request. Please try again.