Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First 3 bytes of files read from FatFs will be truncated #65

Closed
derchr opened this issue Feb 2, 2022 · 4 comments
Closed

First 3 bytes of files read from FatFs will be truncated #65

derchr opened this issue Feb 2, 2022 · 4 comments

Comments

@derchr
Copy link

derchr commented Feb 2, 2022

Hi,
I have created a repro for this bug: https://github.com/derchr/repro-fs
Just follow the instructions in the readme and the bug should show up.

Description of the bug:
What I noticed is that somehow the first 3 bytes of all files that are being read from the filesystem get truncated.
I'm almost certain that 1-2 weeks ago this bug wasn't there as I remember loading binary files (images) from the fs and this worked flawlessly. Now always 3 bytes are missing and the image does not get parsed.
I'm also not sure if this is even caused by esp-idf-sys.

My toolchain is the esp rust compiler version 1.57.0 targeting the default ESP32.
In the example I use the master branch of esp-idf, but I also encountered the issue in the stable 4.4 version.

Thanks!

EDIT:
When using a fixed size array instead of Vec or String to read in the file, it actually works! String::with_capacity on the other hand does not work.

    use std::io::Read;
    let mut lorem = std::fs::File::open("/mnt/readme.txt").unwrap();
    let mut read: usize;
    let mut buf = [0u8; 512];
    read = lorem.read(&mut buf).unwrap();
    while read != 0 {
        println!("{}", std::str::from_utf8(&buf[..read]).unwrap());
        read = lorem.read(&mut buf).unwrap();
    }
@ivmarkov
Copy link
Collaborator

ivmarkov commented Feb 7, 2022

Likely related to this issue: esp-rs/rust#96

@derchr
Copy link
Author

derchr commented Feb 9, 2022

Yeah, it seems like this is the same bug. Closing it here in favor of the already existing bug report.

@bugadani
Copy link
Contributor

bugadani commented Mar 4, 2022

@derchr String::with_capacity only allocates memory, but does not set buffer length, i.e. you can't get a &mut [u8] for the allocated memory. Does your example work if you use vec![0; some_size] instead? In my case, I'm able to read into 0 filled vectors.

@derchr
Copy link
Author

derchr commented Mar 4, 2022

@derchr String::with_capacity only allocates memory, but does not set buffer length, i.e. you can't get a &mut [u8] for the allocated memory. Does your example work if you use vec![0; some_size] instead? In my case, I'm able to read into 0 filled vectors.

I remember also testing Vec::with_capacity and this did not work. Pre-filling the Vec and writing to it using &mut [u8] might actually work as it's similar to the fixed-array method that did work for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants