`memmap` example in `README.md` contains undefined behavior
- Dominant language
- Rust
- Stars
- 2.1k
- Forks
- 48
- PR merge metrics
- No merged PRs in 30d
Description
I think that the example in the `README.md` of `memmap` contains a race condition which can lead to undefined behavior:
```
extern crate memmap;
use memmap::{Mmap, Protection};
use std::env;
use std::io;
use std::str;
fn run() -> Result<(), io::Error> {
let mut args = env::args().skip(1);
let input = args.next().expect("incorrect argument");
let map = Mmap::open_path(input, Protection::Read)?;
unsafe {
let all_bytes = map.as_slice();
if let Ok(file_str) = str::from_utf8(all_bytes) {
println!("{}", file_str);
} else {
println!("not utf8");
}
}
Ok(())
}
```
If the file changes after the UTF-8 check, the program prints a `&str` that contains non-UTF-8 bytes.
Contributor guide
Assessment
This issue has not been assessed yet.