Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 32fd0ed

Browse files
author
arkpar
committed
Fix segfault on empty device list
1 parent 34e64c8 commit 32fd0ed

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/lib.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ impl HidApi {
107107
let enumeration = ffi::hid_enumerate(0, 0);
108108
{
109109
let mut current_device = enumeration;
110-
111-
'do_while: loop {
112-
113-
device_vector.push(conv_hid_device_info(current_device));
114-
115-
if (*current_device).next.is_null() {
116-
break 'do_while;
117-
} else {
118-
current_device = (*current_device).next;
119-
}
120-
}
110+
if !current_device.is_null() {
111+
'do_while: loop {
112+
113+
device_vector.push(conv_hid_device_info(current_device));
114+
115+
if (*current_device).next.is_null() {
116+
break 'do_while;
117+
} else {
118+
current_device = (*current_device).next;
119+
}
120+
}
121+
}
121122
}
122123

123124
ffi::hid_free_enumeration(enumeration);
@@ -422,3 +423,12 @@ impl<'a> HidDevice<'a> {
422423
unsafe { wchar_to_string(buf[..res].as_ptr()) }
423424
}
424425
}
426+
427+
#[test]
428+
fn smoke() {
429+
let api = HidApi::new().unwrap();
430+
// Print out information about all connected devices
431+
for device in &api.devices() {
432+
println!("{:#?}", device);
433+
}
434+
}

0 commit comments

Comments
 (0)