esp-rs / embedded-svc

Rust APIs and abstractions for various embedded services (WiFi, Network, Httpd, Logging, etc.)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

io::read_max always returns an empty array

Squirrelcoding opened this issue · comments

I was investigating another issue in the esp32 std demo and this seems to be the problem. Judging by the code, this is what happens:

  1. Initiate offset and size variables
  2. Read some data into the buffer
  3. break the loop since size is 0 and not manipulated yet
  4. Return the buffer up until the index size, which is 0. This makes the function always return an empty array
pub fn read_max<R: Read>(mut read: R, buf: &mut [u8]) -> Result<(&[u8], usize), R::Error> {
    let mut offset = 0;
    let mut size = 0;

    loop {
        let r = read.read(&mut buf[offset..])?;

        if size == 0 {
            break;
        }

        offset += r;
        size += r;
    }

    Ok((&buf[..size], size))
}

I couldn't find the code in this repo but its right here on docs.rs

As mentioned in the other thread, the function is buggy, but the bug is fixed in master. And the function is renamed.