Skip to content

Commit d8b1f10

Browse files
committed
prevent unsafe function by passing UsbControllerType
1 parent f86952c commit d8b1f10

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

src/platform/macos_iokit/enumeration.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ fn usb_service_iter(service: *const ::std::os::raw::c_char) -> Result<IoServiceI
5252
}
5353
}
5454

55+
fn usb_controller_service_iter(
56+
controller_type: UsbControllerType,
57+
) -> Result<IoServiceIterator, Error> {
58+
unsafe {
59+
let dictionary = match controller_type {
60+
UsbControllerType::XHCI => IOServiceMatching(kAppleUSBXHCI),
61+
UsbControllerType::EHCI => IOServiceMatching(kAppleUSBEHCI),
62+
UsbControllerType::OHCI | UsbControllerType::UHCI => IOServiceMatching(kAppleUSBOHCI),
63+
UsbControllerType::VHCI => IOServiceMatching(kAppleUSBVHCI),
64+
};
65+
if dictionary.is_null() {
66+
return Err(Error::new(ErrorKind::Other, "IOServiceMatching failed"));
67+
}
68+
69+
let mut iterator = 0;
70+
let r = IOServiceGetMatchingServices(kIOMasterPortDefault, dictionary, &mut iterator);
71+
if r != kIOReturnSuccess {
72+
return Err(Error::from_raw_os_error(r));
73+
}
74+
75+
Ok(IoServiceIterator::new(iterator))
76+
}
77+
}
78+
5579
pub fn list_devices() -> Result<impl Iterator<Item = DeviceInfo>, Error> {
5680
Ok(usb_service_iter(kIOUSBDeviceClassName)?.filter_map(probe_device))
5781
}
@@ -60,10 +84,22 @@ pub fn list_buses() -> Result<impl Iterator<Item = BusInfo>, Error> {
6084
// Chain all the HCI types into one iterator
6185
// A bit of a hack, could maybe probe IOPCIDevice and filter on children with IOClass.starts_with("AppleUSB")
6286
Ok([
63-
(usb_service_iter(kAppleUSBXHCI)?, UsbControllerType::XHCI),
64-
(usb_service_iter(kAppleUSBEHCI)?, UsbControllerType::EHCI),
65-
(usb_service_iter(kAppleUSBOHCI)?, UsbControllerType::OHCI),
66-
(usb_service_iter(kAppleUSBVHCI)?, UsbControllerType::VHCI),
87+
(
88+
usb_controller_service_iter(UsbControllerType::XHCI)?,
89+
UsbControllerType::XHCI,
90+
),
91+
(
92+
usb_controller_service_iter(UsbControllerType::EHCI)?,
93+
UsbControllerType::EHCI,
94+
),
95+
(
96+
usb_controller_service_iter(UsbControllerType::OHCI)?,
97+
UsbControllerType::OHCI,
98+
),
99+
(
100+
usb_controller_service_iter(UsbControllerType::VHCI)?,
101+
UsbControllerType::VHCI,
102+
),
67103
]
68104
.into_iter()
69105
.map(|(iter, hci_type)| iter.zip(std::iter::repeat(hci_type)))

0 commit comments

Comments
 (0)