Skip to content

Commit 0eef651

Browse files
committed
Auto merge of #559 - kevinmehall:mmap, r=kamalmarhubi
Actually mark mmap and related functions as `unsafe` The nix::sys::mman::mmap documentation says > Calls to mmap are inherently unsafe, so they must be made in an unsafe block. however, the function was not actually marked unsafe. * `munmap` should also be `unsafe` for obvious reasons. * `madvise` should be `unsafe` because of the `MADV_DONTNEED` flag. * `mlock` was already marked `unsafe` * `munlock` and `msync` don't strictly need to be `unsafe` despite taking pointers AFAICT, but are marked `unsafe` for consistency and in case they add additional flags in the future. [breaking-change]
2 parents 5c90289 + 902f281 commit 0eef651

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
<!--### Added-->
9+
10+
### Changed
11+
- Marked `sys::mman::{ mmap, munmap, madvise, munlock, msync }` as unsafe.
12+
([#559](https://github.com/nix-rust/nix/pull/559))
13+
14+
<!--### Fixed-->
15+
816
## [0.8.0] 2017-03-02
917

1018
### Added

src/sys/mman.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
195195
Errno::result(ffi::mlock(addr, length)).map(drop)
196196
}
197197

198-
pub fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
199-
Errno::result(unsafe { ffi::munlock(addr, length) }).map(drop)
198+
pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
199+
Errno::result(ffi::munlock(addr, length)).map(drop)
200200
}
201201

202202
/// Calls to mmap are inherently unsafe, so they must be made in an unsafe block. Typically
203203
/// a higher-level abstraction will hide the unsafe interactions with the mmap'd region.
204-
pub fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> {
205-
let ret = unsafe { ffi::mmap(addr, length, prot.bits(), flags.bits(), fd, offset) };
204+
pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> {
205+
let ret = ffi::mmap(addr, length, prot.bits(), flags.bits(), fd, offset);
206206

207207
if ret as isize == MAP_FAILED {
208208
Err(Error::Sys(Errno::last()))
@@ -211,16 +211,16 @@ pub fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags,
211211
}
212212
}
213213

214-
pub fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
215-
Errno::result(unsafe { ffi::munmap(addr, len) }).map(drop)
214+
pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
215+
Errno::result(ffi::munmap(addr, len)).map(drop)
216216
}
217217

218-
pub fn madvise(addr: *const c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
219-
Errno::result(unsafe { ffi::madvise(addr, length, advise) }).map(drop)
218+
pub unsafe fn madvise(addr: *const c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
219+
Errno::result(ffi::madvise(addr, length, advise)).map(drop)
220220
}
221221

222-
pub fn msync(addr: *const c_void, length: size_t, flags: MsFlags) -> Result<()> {
223-
Errno::result(unsafe { ffi::msync(addr, length, flags.bits()) }).map(drop)
222+
pub unsafe fn msync(addr: *const c_void, length: size_t, flags: MsFlags) -> Result<()> {
223+
Errno::result(ffi::msync(addr, length, flags.bits())).map(drop)
224224
}
225225

226226
pub fn shm_open<P: ?Sized + NixPath>(name: &P, flag: OFlag, mode: Mode) -> Result<RawFd> {

0 commit comments

Comments
 (0)