tbu- · GitHub

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.

Read the original on github.com ↗