diff --git a/CHANGELOG.md b/CHANGELOG.md index 69f628d8a0..af7b1c002c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). relaxed lifetime requirements relative to 0.27.1. ([#2136](https://github.com/nix-rust/nix/pull/2136)) +- `sys::mman::mmap` to return `NonNull`. + ([#2000](https://github.com/nix-rust/nix/pull/2000)) + ## [0.27.1] - 2023-08-28 ### Fixed @@ -114,6 +117,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). `sys::kevent::Kqueue::kevent`, and `sys::event::kqueue` is deprecated in favor of `sys::kevent::Kqueue::new`. ([#1943](https://github.com/nix-rust/nix/pull/1943)) + +### Removed + - Removed deprecated IoVec API. ([#1855](https://github.com/nix-rust/nix/pull/1855)) - Removed deprecated net APIs. diff --git a/src/sys/mman.rs b/src/sys/mman.rs index 8cfd6d6d54..8731ccc891 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -9,6 +9,7 @@ use crate::Result; use crate::{fcntl::OFlag, sys::stat::Mode}; use libc::{self, c_int, c_void, off_t, size_t}; use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}}; +use std::ptr::NonNull; libc_bitflags! { /// Desired memory protection of a memory mapping. @@ -425,19 +426,15 @@ pub unsafe fn mmap( flags: MapFlags, f: Option, offset: off_t, -) -> Result<*mut c_void> { +) -> Result> { let ptr = addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void); let fd = f.map(|f| f.as_fd().as_raw_fd()).unwrap_or(-1); let ret = libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset); - - if ret == libc::MAP_FAILED { - Err(Errno::last()) - } else { - Ok(ret) - } + + NonNull::new(ret).ok_or_else(Errno::last) } /// Expands (or shrinks) an existing memory mapping, potentially moving it at @@ -527,8 +524,8 @@ pub unsafe fn madvise( /// let mut slice: &mut [u8] = unsafe { /// let mem = mmap::(None, one_k_non_zero, ProtFlags::PROT_NONE, /// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, None, 0).unwrap(); -/// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap(); -/// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K) +/// mprotect(mem.as_ptr(), ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap(); +/// std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K) /// }; /// assert_eq!(slice[0], 0x00); /// slice[0] = 0xFF; diff --git a/test/sys/test_mman.rs b/test/sys/test_mman.rs index 704e14f9c5..62d9c1e227 100644 --- a/test/sys/test_mman.rs +++ b/test/sys/test_mman.rs @@ -4,7 +4,7 @@ use std::{num::NonZeroUsize, os::unix::io::BorrowedFd}; #[test] fn test_mmap_anonymous() { unsafe { - let ptr = mmap::( + let mut ptr = mmap::( None, NonZeroUsize::new(1).unwrap(), ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, @@ -12,10 +12,11 @@ fn test_mmap_anonymous() { None, 0, ) - .unwrap() as *mut u8; - assert_eq!(*ptr, 0x00u8); - *ptr = 0xffu8; - assert_eq!(*ptr, 0xffu8); + .unwrap() + .cast::(); + assert_eq!(*ptr.as_ref(), 0x00u8); + *ptr.as_mut() = 0xffu8; + assert_eq!(*ptr.as_ref(), 0xffu8); } } @@ -38,7 +39,7 @@ fn test_mremap_grow() { 0, ) .unwrap(); - std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K) + std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K) }; assert_eq!(slice[ONE_K - 1], 0x00); slice[ONE_K - 1] = 0xFF; @@ -96,7 +97,7 @@ fn test_mremap_shrink() { 0, ) .unwrap(); - std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K) + std::slice::from_raw_parts_mut(mem.as_ptr().cast(), ONE_K) }; assert_eq!(slice[ONE_K - 1], 0x00); slice[ONE_K - 1] = 0xFF;