Skip to content

Commit b600727

Browse files
authored
Win32: Fix memory leak in free_hid_device (signal11#361)
- Fix memory leak in `free_hid_device`; - Simpllify `hid_open_path` code;
1 parent d67b5c9 commit b600727

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

windows/hid.c

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static void free_hid_device(hid_device *dev)
222222
free(dev->write_buf);
223223
free(dev->feature_buf);
224224
free(dev->read_buf);
225-
free(dev->device_info);
225+
hid_free_enumeration(dev->device_info);
226226
free(dev);
227227
}
228228

@@ -781,72 +781,58 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsi
781781

782782
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
783783
{
784-
hid_device *dev;
785-
HIDP_CAPS caps;
784+
hid_device *dev = NULL;
785+
HANDLE device_handle = INVALID_HANDLE_VALUE;
786786
PHIDP_PREPARSED_DATA pp_data = NULL;
787-
BOOLEAN res;
788-
NTSTATUS nt_res;
789-
790-
if (hid_init() < 0) {
791-
return NULL;
792-
}
787+
HIDP_CAPS caps;
793788

794-
dev = new_hid_device();
789+
if (hid_init() < 0)
790+
goto end_of_function;
795791

796792
/* Open a handle to the device */
797-
dev->device_handle = open_device(path, TRUE);
793+
device_handle = open_device(path, TRUE);
798794

799795
/* Check validity of write_handle. */
800-
if (dev->device_handle == INVALID_HANDLE_VALUE) {
796+
if (device_handle == INVALID_HANDLE_VALUE) {
801797
/* System devices, such as keyboards and mice, cannot be opened in
802798
read-write mode, because the system takes exclusive control over
803799
them. This is to prevent keyloggers. However, feature reports
804800
can still be sent and received. Retry opening the device, but
805801
without read/write access. */
806-
dev->device_handle = open_device(path, FALSE);
802+
device_handle = open_device(path, FALSE);
807803

808804
/* Check the validity of the limited device_handle. */
809-
if (dev->device_handle == INVALID_HANDLE_VALUE) {
810-
/* Unable to open the device, even without read-write mode. */
811-
register_error(dev, "CreateFile");
812-
goto err;
813-
}
805+
if (device_handle == INVALID_HANDLE_VALUE)
806+
goto end_of_function;
814807
}
815808

816809
/* Set the Input Report buffer size to 64 reports. */
817-
res = HidD_SetNumInputBuffers(dev->device_handle, 64);
818-
if (!res) {
819-
register_error(dev, "HidD_SetNumInputBuffers");
820-
goto err;
821-
}
810+
if (!HidD_SetNumInputBuffers(device_handle, 64))
811+
goto end_of_function;
822812

823813
/* Get the Input Report length for the device. */
824-
res = HidD_GetPreparsedData(dev->device_handle, &pp_data);
825-
if (!res) {
826-
register_error(dev, "HidD_GetPreparsedData");
827-
goto err;
828-
}
829-
nt_res = HidP_GetCaps(pp_data, &caps);
830-
if (nt_res != HIDP_STATUS_SUCCESS) {
831-
register_error(dev, "HidP_GetCaps");
832-
goto err_pp_data;
833-
}
814+
if (!HidD_GetPreparsedData(device_handle, &pp_data))
815+
goto end_of_function;
816+
817+
if (HidP_GetCaps(pp_data, &caps) != HIDP_STATUS_SUCCESS)
818+
goto end_of_function;
819+
820+
dev = new_hid_device();
821+
822+
dev->device_handle = device_handle;
823+
device_handle = INVALID_HANDLE_VALUE;
824+
834825
dev->output_report_length = caps.OutputReportByteLength;
835826
dev->input_report_length = caps.InputReportByteLength;
836827
dev->feature_report_length = caps.FeatureReportByteLength;
837-
HidD_FreePreparsedData(pp_data);
838-
839828
dev->read_buf = (char*) malloc(dev->input_report_length);
840-
841829
dev->device_info = hid_get_device_info(path, dev->device_handle);
842830

843-
return dev;
831+
end_of_function:
832+
CloseHandle(device_handle);
833+
HidD_FreePreparsedData(pp_data);
844834

845-
err_pp_data:
846-
HidD_FreePreparsedData(pp_data);
847-
err:
848-
free_hid_device(dev);
849-
return NULL;
835+
return dev;
850836
}
851837

852838
int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)

0 commit comments

Comments
 (0)