dcuddeback / libusb-rs

A safe Rust wrapper for libusb.
MIT License
199 stars 64 forks source link

Panic when call read_control() with 256 bytes buf #36

Closed zoumi closed 4 years ago

zoumi commented 4 years ago

I have modify the read_control() in device_handle.rs to print out some useful informations:

    pub fn read_control(&self, request_type: u8, request: u8, value: u16, index: u16, buf: &mut [u8], timeout: Duration) -> ::Result<usize> {
        if request_type & LIBUSB_ENDPOINT_DIR_MASK != LIBUSB_ENDPOINT_IN {
            return Err(Error::InvalidParam);
        }

        let ptr = buf.as_mut_ptr() as *mut c_uchar;
        let len = buf.len() as u16;
        let timeout_ms = (timeout.as_secs() * 1000 + timeout.subsec_nanos() as u64 / 1_000_000) as c_uint;
        let res = unsafe {
            println!("libusb_control_transfer: len {:?}",len);
            libusb_control_transfer(self.handle, request_type, request, value, index, ptr, len, timeout_ms)
        };
        println!("libusb_control_transfer: res {:?}",res);
        if res < 0 {
            Err(error::from_libusb(res))
        } else {
            Ok(res as usize)
        }
}

When I try to read my bluetooth HID mouse, I got the error. Here is the out put:

libusb_control_transfer: len 256
libusb_control_transfer: res -8
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Overflow', src\libcore\result.rs:999:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

After modify

    pub fn read_languages(&self, timeout: Duration) -> ::Result<Vec<Language>> {
        let mut buf = Vec::<u8>::with_capacity(256)
        ....

to

    pub fn read_languages(&self, timeout: Duration) -> ::Result<Vec<Language>> {
        let mut buf = Vec::<u8>::with_capacity(65)
        ....

it just works. Here is the output:

libusb_control_transfer: len 65
libusb_control_transfer: res 4

My enviroment: Default host: x86_64-pc-windows-gnu installed toolchains nightly-2019-04-21-i686-pc-windows-msvc (default) installed targets for active toolchain i686-pc-windows-msvc nightly-2019-04-21-i686-pc-windows-msvc (default) rustc 1.36.0-nightly (33fe1131c 2019-04-20)

libusb-1.0.dll version: 1.0.20.11004

zoumi commented 4 years ago

relative issue: https://github.com/dcuddeback/libusb-rs/issues/21

zoumi commented 4 years ago

close by https://github.com/dcuddeback/libusb-rs/pull/38