Win32: Fix memory leak in free_hid_device (#361)

- Fix memory leak in `free_hid_device`;
- Simpllify `hid_open_path` code;
This commit is contained in:
Dimitriy Ryazantcev
2021-12-22 23:59:56 +02:00
committed by GitHub
parent d67b5c9fec
commit b600727200
+28 -42
View File
@@ -222,7 +222,7 @@ static void free_hid_device(hid_device *dev)
free(dev->write_buf); free(dev->write_buf);
free(dev->feature_buf); free(dev->feature_buf);
free(dev->read_buf); free(dev->read_buf);
free(dev->device_info); hid_free_enumeration(dev->device_info);
free(dev); free(dev);
} }
@@ -781,72 +781,58 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsi
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
{ {
hid_device *dev; hid_device *dev = NULL;
HIDP_CAPS caps; HANDLE device_handle = INVALID_HANDLE_VALUE;
PHIDP_PREPARSED_DATA pp_data = NULL; PHIDP_PREPARSED_DATA pp_data = NULL;
BOOLEAN res; HIDP_CAPS caps;
NTSTATUS nt_res;
if (hid_init() < 0) { if (hid_init() < 0)
return NULL; goto end_of_function;
}
dev = new_hid_device();
/* Open a handle to the device */ /* Open a handle to the device */
dev->device_handle = open_device(path, TRUE); device_handle = open_device(path, TRUE);
/* Check validity of write_handle. */ /* Check validity of write_handle. */
if (dev->device_handle == INVALID_HANDLE_VALUE) { if (device_handle == INVALID_HANDLE_VALUE) {
/* System devices, such as keyboards and mice, cannot be opened in /* System devices, such as keyboards and mice, cannot be opened in
read-write mode, because the system takes exclusive control over read-write mode, because the system takes exclusive control over
them. This is to prevent keyloggers. However, feature reports them. This is to prevent keyloggers. However, feature reports
can still be sent and received. Retry opening the device, but can still be sent and received. Retry opening the device, but
without read/write access. */ without read/write access. */
dev->device_handle = open_device(path, FALSE); device_handle = open_device(path, FALSE);
/* Check the validity of the limited device_handle. */ /* Check the validity of the limited device_handle. */
if (dev->device_handle == INVALID_HANDLE_VALUE) { if (device_handle == INVALID_HANDLE_VALUE)
/* Unable to open the device, even without read-write mode. */ goto end_of_function;
register_error(dev, "CreateFile");
goto err;
}
} }
/* Set the Input Report buffer size to 64 reports. */ /* Set the Input Report buffer size to 64 reports. */
res = HidD_SetNumInputBuffers(dev->device_handle, 64); if (!HidD_SetNumInputBuffers(device_handle, 64))
if (!res) { goto end_of_function;
register_error(dev, "HidD_SetNumInputBuffers");
goto err;
}
/* Get the Input Report length for the device. */ /* Get the Input Report length for the device. */
res = HidD_GetPreparsedData(dev->device_handle, &pp_data); if (!HidD_GetPreparsedData(device_handle, &pp_data))
if (!res) { goto end_of_function;
register_error(dev, "HidD_GetPreparsedData");
goto err; if (HidP_GetCaps(pp_data, &caps) != HIDP_STATUS_SUCCESS)
} goto end_of_function;
nt_res = HidP_GetCaps(pp_data, &caps);
if (nt_res != HIDP_STATUS_SUCCESS) { dev = new_hid_device();
register_error(dev, "HidP_GetCaps");
goto err_pp_data; dev->device_handle = device_handle;
} device_handle = INVALID_HANDLE_VALUE;
dev->output_report_length = caps.OutputReportByteLength; dev->output_report_length = caps.OutputReportByteLength;
dev->input_report_length = caps.InputReportByteLength; dev->input_report_length = caps.InputReportByteLength;
dev->feature_report_length = caps.FeatureReportByteLength; dev->feature_report_length = caps.FeatureReportByteLength;
HidD_FreePreparsedData(pp_data);
dev->read_buf = (char*) malloc(dev->input_report_length); dev->read_buf = (char*) malloc(dev->input_report_length);
dev->device_info = hid_get_device_info(path, dev->device_handle); dev->device_info = hid_get_device_info(path, dev->device_handle);
return dev; end_of_function:
CloseHandle(device_handle);
HidD_FreePreparsedData(pp_data);
err_pp_data: return dev;
HidD_FreePreparsedData(pp_data);
err:
free_hid_device(dev);
return NULL;
} }
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length) int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)