-
Notifications
You must be signed in to change notification settings - Fork 67
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
Bad assumption on read() results #8
Comments
Hey @ayende,
Indeed i'm aware of this warning but in my experience when reading a local file, Because the docs also say:
So I believe it should be safe to assume that when reading a local file But, you also state:
I'm learning this today, Thank you very much. But I also find that very surprising! Do you have a recommendation to fix this potential issue? Use a BufReader? |
You need to keep reading until Something like this:
That work for all scenarios. |
I noticed that you have |
Thnaks! Rust's std Where can I find information about the behavior you have described above (that |
If you need to make the distinction between next & last, read to the buffer until it is full, even if you get partial read. For reference, see: |
In https://github.com/skerkour/kerkour.com/blob/main/2022/rust_file_encryption_with_password/src/main.rs#L75 (and in many other places), you are assumption that the only scenario where you'll get less than the number of requested bytes is when you are at the end of the file.
This is wrong.
See the docs here:
https://doc.rust-lang.org/std/io/trait.Read.html#tymethod.read
In particular:
There are many reasons why this may be the case.
A great example is if the read you are doing happened to start on a region that is already in memory, but continues to a page that is not there yet. The file system will return what it has right now. That can lead to really hard to figure out bugs.
The text was updated successfully, but these errors were encountered: