Skip to content

Commit

Permalink
windows: Fix leak of event handle on open_device() failure
Browse files Browse the repository at this point in the history
Create a new free_hid_device() function which takes care of all the
resources in hid_device which may need to be freed/released.

Reported-by: Johan Lindh <johan@linkdata.se>
  • Loading branch information
signal11 committed Apr 23, 2013
1 parent f75f6f7 commit 776ec62
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions windows/hid.c
Expand Up @@ -151,6 +151,14 @@ static hid_device *new_hid_device()
return dev;
}

static void free_hid_device(hid_device *dev)
{
CloseHandle(dev->ol.hEvent);
CloseHandle(dev->device_handle);
LocalFree(dev->last_error_str);
free(dev->read_buf);
free(dev);
}

static void register_error(hid_device *device, const char *op)
{
Expand Down Expand Up @@ -581,8 +589,7 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
err_pp_data:
HidD_FreePreparsedData(pp_data);
err:
CloseHandle(dev->device_handle);
free(dev);
free_hid_device(dev);
return NULL;
}

Expand Down Expand Up @@ -783,11 +790,7 @@ void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
if (!dev)
return;
CancelIo(dev->device_handle);
CloseHandle(dev->ol.hEvent);
CloseHandle(dev->device_handle);
LocalFree(dev->last_error_str);
free(dev->read_buf);
free(dev);
free_hid_device(dev);
}

int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
Expand Down

0 comments on commit 776ec62

Please sign in to comment.