brson / brson/stdx

`memmap` example in `README.md` contains undefined behavior

Open
#37 3 comments 0 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.