Use `MAP_FAILED`.

This commit is contained in:
Sergey Pepyakin 2019-07-03 18:17:34 +02:00
parent 706e016ef8
commit 97e67cbfd7
1 changed files with 4 additions and 10 deletions

View File

@ -58,19 +58,13 @@ impl Mmap {
) )
}; };
match ptr_or_err as usize { match ptr_or_err {
// `mmap` returns -1 in case of an error.
// `mmap` shouldn't return 0 since it has a special meaning for compilers.
//
// With the current parameters, the error can only be returned in case of insufficient // With the current parameters, the error can only be returned in case of insufficient
// memory. // memory.
x if x == 0 || x as isize == -1 => Err("mmap returned error"), libc::MAP_FAILED => Err("mmap returned an error"),
_ => { _ => {
let ptr = unsafe { let ptr = NonNull::new(ptr_or_err as *mut u8)
// Safety Proof: .ok_or("mmap returned 0")?;
// the ptr cannot be null as checked within the enclosing match.
NonNull::new_unchecked(ptr_or_err as *mut u8)
};
Ok(Self { ptr, len }) Ok(Self { ptr, len })
} }
} }