Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clippy lints #125

Merged
merged 24 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "memmap2"
version = "0.9.5"
authors = ["Dan Burkert <dan@danburkert.com>", "Yevhenii Reizner <razrfalcon@gmail.com>"]
authors = [
"Dan Burkert <dan@danburkert.com>",
RazrFalcon marked this conversation as resolved.
Show resolved Hide resolved
"Yevhenii Reizner <razrfalcon@gmail.com>",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/RazrFalcon/memmap2-rs"
documentation = "https://docs.rs/memmap2"
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
msrv = "1.36.0"
RazrFalcon marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub enum Advice {
/// Indicates that the application would like the wired pages in this address range to be
/// zeroed out if the address range is deallocated without first unwiring the pages (i.e.
/// a munmap(2) without a preceding munlock(2) or the application quits). This is used
/// with madvise() system call.
/// with `madvise()` system call.
#[cfg(any(target_os = "macos", target_os = "ios"))]
ZeroWiredPages = libc::MADV_ZERO_WIRED_PAGES,
}
Expand Down
119 changes: 78 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#![deny(clippy::all, clippy::pedantic)]
#![allow(
RazrFalcon marked this conversation as resolved.
Show resolved Hide resolved
// pedantic exceptions
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::doc_markdown,
clippy::explicit_deref_methods,
clippy::missing_errors_doc,
clippy::module_name_repetitions,
clippy::must_use_candidate,
clippy::needless_pass_by_value,
clippy::return_self_not_must_use,
clippy::unreadable_literal,
clippy::upper_case_acronyms,
// remove later
clippy::legacy_numeric_constants
)]

//! A cross-platform Rust API for memory mapped buffers.
//!
//! The core functionality is provided by either [`Mmap`] or [`MmapMut`],
Expand Down Expand Up @@ -229,36 +248,39 @@ impl MmapOptions {

/// Returns the configured length, or the length of the provided file.
fn get_len<T: MmapAsRawDesc>(&self, file: &T) -> Result<usize> {
self.len.map(Ok).unwrap_or_else(|| {
let desc = file.as_raw_desc();
let file_len = file_len(desc.0)?;

if file_len < self.offset {
return Err(Error::new(
ErrorKind::InvalidData,
"memory map offset is larger than length",
));
}
let len = file_len - self.offset;

// Rust's slice cannot be larger than isize::MAX.
// See https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
//
// This is not a problem on 64-bit targets, but on 32-bit one
// having a file or an anonymous mapping larger than 2GB is quite normal
// and we have to prevent it.
//
// The code below is essentially the same as in Rust's std:
// https://github.com/rust-lang/rust/blob/db78ab70a88a0a5e89031d7ee4eccec835dcdbde/library/alloc/src/raw_vec.rs#L495
if mem::size_of::<usize>() < 8 && len > isize::MAX as u64 {
return Err(Error::new(
ErrorKind::InvalidData,
"memory map length overflows isize",
));
}

Ok(len as usize)
})
self.len.map_or_else(
|| {
let desc = file.as_raw_desc();
let file_len = file_len(desc.0)?;

if file_len < self.offset {
return Err(Error::new(
ErrorKind::InvalidData,
"memory map offset is larger than length",
));
}
let len = file_len - self.offset;

// Rust's slice cannot be larger than isize::MAX.
// See https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
//
// This is not a problem on 64-bit targets, but on 32-bit one
// having a file or an anonymous mapping larger than 2GB is quite normal
// and we have to prevent it.
//
// The code below is essentially the same as in Rust's std:
// https://github.com/rust-lang/rust/blob/db78ab70a88a0a5e89031d7ee4eccec835dcdbde/library/alloc/src/raw_vec.rs#L495
if mem::size_of::<usize>() < 8 && len > isize::MAX as u64 {
return Err(Error::new(
ErrorKind::InvalidData,
"memory map length overflows isize",
));
}

Ok(len as usize)
},
Ok,
)
}

/// Configures the anonymous memory map to be suitable for a process or thread stack.
Expand Down Expand Up @@ -418,7 +440,7 @@ impl MmapOptions {
/// # let tempdir = tempfile::tempdir()?;
/// let path: PathBuf = /* path to file */
/// # tempdir.path().join("map_mut");
/// let file = OpenOptions::new().read(true).write(true).create(true).open(&path)?;
/// let file = OpenOptions::new().read(true).write(true).create(true).truncate(true).open(&path)?;
/// file.set_len(13)?;
///
/// let mut mmap = unsafe {
Expand Down Expand Up @@ -676,6 +698,7 @@ impl Mmap {
/// # .read(true)
/// # .write(true)
/// # .create(true)
/// # .truncate(true)
/// # .open(tempdir.path()
/// # .join("make_mut"))?;
/// # file.set_len(128)?;
Expand Down Expand Up @@ -886,7 +909,7 @@ impl MmapRaw {
/// let tempdir = tempfile::tempdir()?;
/// let path: PathBuf = /* path to file */
/// # tempdir.path().join("flush");
/// let file = OpenOptions::new().read(true).write(true).create(true).open(&path)?;
/// let file = OpenOptions::new().read(true).write(true).create(true).truncate(true).open(&path)?;
/// file.set_len(128)?;
///
/// let mut mmap = unsafe { MmapRaw::map_raw(&file)? };
Expand Down Expand Up @@ -1113,6 +1136,7 @@ impl MmapMut {
/// .read(true)
/// .write(true)
/// .create(true)
/// .truncate(true)
/// .open(&path)?;
/// file.set_len(13)?;
///
Expand Down Expand Up @@ -1160,7 +1184,7 @@ impl MmapMut {
/// # let tempdir = tempfile::tempdir()?;
/// let path: PathBuf = /* path to file */
/// # tempdir.path().join("flush");
/// let file = OpenOptions::new().read(true).write(true).create(true).open(&path)?;
/// let file = OpenOptions::new().read(true).write(true).create(true).truncate(true).open(&path)?;
/// file.set_len(128)?;
///
/// let mut mmap = unsafe { MmapMut::map_mut(&file)? };
Expand Down Expand Up @@ -1469,6 +1493,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();

Expand Down Expand Up @@ -1502,6 +1527,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();

Expand Down Expand Up @@ -1534,6 +1560,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
let mmap = unsafe { Mmap::map(&file).unwrap() };
Expand Down Expand Up @@ -1566,7 +1593,7 @@ mod test {

#[test]
fn map_anon_zero_len() {
assert!(MmapOptions::new().map_anon().unwrap().is_empty())
assert!(MmapOptions::new().map_anon().unwrap().is_empty());
}

#[test]
Expand All @@ -1589,6 +1616,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.set_len(128).unwrap();
Expand All @@ -1613,6 +1641,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.set_len(128).unwrap();
Expand All @@ -1639,6 +1668,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.set_len(128).unwrap();
Expand Down Expand Up @@ -1675,6 +1705,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.set_len(128).unwrap();
Expand All @@ -1700,10 +1731,11 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();

let offset = u32::MAX as u64 + 2;
let offset = u64::from(u32::MAX) + 2;
let len = 5432;
file.set_len(offset + len as u64).unwrap();

Expand Down Expand Up @@ -1743,14 +1775,13 @@ mod test {

#[test]
fn sync_send() {
let mmap = MmapMut::map_anon(129).unwrap();

fn is_sync_send<T>(_val: T)
where
T: Sync + Send,
{
}

let mmap = MmapMut::map_anon(129).unwrap();
is_sync_send(mmap);
}

Expand Down Expand Up @@ -1787,6 +1818,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(tempdir.path().join("jit_x86"))
.expect("open");

Expand All @@ -1807,6 +1839,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.expect("open");
file.set_len(256_u64).expect("set_len");
Expand Down Expand Up @@ -1853,6 +1886,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.expect("open");
file.set_len(256_u64).expect("set_len");
Expand Down Expand Up @@ -1907,6 +1941,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.expect("open");
file.write_all(b"abc123").unwrap();
Expand Down Expand Up @@ -1961,6 +1996,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();

Expand Down Expand Up @@ -2067,6 +2103,7 @@ mod test {
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.unwrap();
file.set_len(128).unwrap();
Expand Down Expand Up @@ -2115,8 +2152,8 @@ mod test {

unsafe {
mmap.remap(final_len, RemapOptions::new().may_move(true))
.unwrap()
};
.unwrap();
}

// The size should have been updated
assert_eq!(mmap.len(), final_len);
Expand Down Expand Up @@ -2197,8 +2234,8 @@ mod test {

unsafe {
mmap.remap(final_len, RemapOptions::new().may_move(true))
.unwrap()
};
.unwrap();
}

// The size should have been updated
assert_eq!(mmap.len(), final_len);
Expand Down
6 changes: 3 additions & 3 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ impl MmapInner {
let stack = if stack { MAP_STACK } else { 0 };
let populate = if populate { MAP_POPULATE } else { 0 };
let hugetlb = if huge.is_some() { MAP_HUGETLB } else { 0 };
let offset = huge
.map(|mask| ((mask as u64) & (MAP_HUGE_MASK as u64)) << MAP_HUGE_SHIFT)
.unwrap_or(0);
let offset = huge.map_or(0, |mask| {
(u64::from(mask) & (MAP_HUGE_MASK as u64)) << MAP_HUGE_SHIFT
});
MmapInner::new(
len,
libc::PROT_READ | libc::PROT_WRITE,
Expand Down
10 changes: 5 additions & 5 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ struct SYSTEM_INFO {
wProcessorRevision: WORD,
}

#[allow(dead_code)]
#[repr(C)]
#[derive(Copy, Clone)]
pub struct FILETIME {
Expand Down Expand Up @@ -136,8 +137,7 @@ extern "system" {
///
/// This aligns the pointer to `allocation_granularity()` or 1 if unknown.
fn empty_slice_ptr() -> *mut c_void {
let align = allocation_granularity().max(1);
unsafe { mem::transmute(align) }
allocation_granularity().max(1) as *mut c_void
}

pub struct MmapInner {
Expand Down Expand Up @@ -216,7 +216,7 @@ impl MmapInner {
Ok(MmapInner {
handle: Some(new_handle),
ptr: ptr.offset(alignment as isize),
len: len as usize,
len,
copy,
})
}
Expand Down Expand Up @@ -382,7 +382,7 @@ impl MmapInner {
Ok(MmapInner {
handle: None,
ptr,
len: len as usize,
len,
copy: false,
})
} else {
Expand Down Expand Up @@ -463,7 +463,7 @@ impl MmapInner {

#[inline]
pub fn mut_ptr(&mut self) -> *mut u8 {
self.ptr as *mut u8
self.ptr.cast()
}

#[inline]
Expand Down
Loading