cocool97 / adb_client

Rust ADB (Android Debug Bridge) client library
MIT License
144 stars 23 forks source link

Fix underflow in recv #53

Closed MahieDev closed 6 days ago

MahieDev commented 6 days ago

Fairly new to rust, so might miss something obvious.

Nevertheless, I am experiencing an underflow when performing adb pull, as effective_read may be greater than length.

Fixed by using the length as a reference for the number of bytes to read. Again, not sure if this is the most optimal solution, but works for me.

cocool97 commented 6 days ago

Thanks for the PR, do you have something that might help me to reproduce this issue ?

I haven't had this error before, so this would be nice to reproduce it !

MahieDev commented 6 days ago

Sure, no problem. Just pushed a minimal .txt with contents "abc" to /data/local/tmp

fn main() {
    let mut server = ADBServer::default();
    let mut device = server.get_device().expect("cannot get device");
    let stdout = io::stdout();
    let mut handle = stdout.lock();
    device.pull("data/local/tmp/abc.txt", &mut handle).unwrap();
}

Amazing repo btw! :)

cocool97 commented 6 days ago

I can indeed reproduce it. It's maybe related to the size of the file pulled, but I have to check.

Thanks for pinpointing it !

cocool97 commented 6 days ago

Looks like the error is related to the fact we pass buf to read, that may have a size bigger then what we have to read.

Can you try with something like this:

let effective_read = self.inner.read(&mut buf[0..length])?;
MahieDev commented 6 days ago

Yeah, works perfectly, cool!

cocool97 commented 6 days ago

Merged, thanks for the PR ;)