From df469ddcb377abe07cbf06167b5326ec85c17fae Mon Sep 17 00:00:00 2001 From: auronandace Date: Sat, 21 Feb 2026 08:39:26 +0000 Subject: [PATCH] apply is_multiple_of lint --- src/context/memory.rs | 6 ++---- src/memory/mod.rs | 2 +- src/scheme/memory.rs | 5 +++-- src/scheme/mod.rs | 8 ++++---- src/scheme/proc.rs | 6 +++--- src/scheme/user.rs | 12 +++++------- src/syscall/fs.rs | 8 ++++---- src/syscall/futex.rs | 6 +++--- src/syscall/process.rs | 2 +- src/syscall/usercopy.rs | 2 +- 10 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/context/memory.rs b/src/context/memory.rs index d60a24a2fb..d4b2f553f0 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -752,7 +752,7 @@ impl PageSpan { Self::validate(address, size).filter(|this| !this.is_empty()) } pub fn validate(address: VirtualAddress, size: usize) -> Option { - if address.data() % PAGE_SIZE != 0 || size % PAGE_SIZE != 0 { + if !address.data().is_multiple_of(PAGE_SIZE) || !size.is_multiple_of(PAGE_SIZE) { return None; } if address.data().saturating_add(size) > crate::USER_END_OFFSET { @@ -1025,9 +1025,7 @@ impl UserGrants { pub fn remove_containing(&mut self, page: Page) -> Option { // Points to the gap *after* the greatest grant smaller than or equal to `page`. let mut cursor = self.inner.upper_bound_mut(Bound::Included(&page)); - let Some((&base, info)) = cursor.peek_prev() else { - return None; - }; + let (&base, info) = cursor.peek_prev()?; if (base..base.next_by(info.page_count())).contains(&page) { let (base, info) = cursor.remove_prev().unwrap(); diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 17c09d1ebc..aaf10d1a32 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -346,7 +346,7 @@ impl Frame { / PAGE_SIZE } pub fn is_aligned_to_order(self, order: u32) -> bool { - self.base().data() % (PAGE_SIZE << order) == 0 + self.base().data().is_multiple_of(PAGE_SIZE << order) } } diff --git a/src/scheme/memory.rs b/src/scheme/memory.rs index 8c16fa1d2b..b8d13a7b97 100644 --- a/src/scheme/memory.rs +++ b/src/scheme/memory.rs @@ -127,12 +127,13 @@ impl MemoryScheme { ) -> Result { // TODO: Check physical_address against the real MAXPHYADDR. let end = 1 << 52; - if (physical_address.saturating_add(size) as u64) > end || physical_address % PAGE_SIZE != 0 + if (physical_address.saturating_add(size) as u64) > end + || !physical_address.is_multiple_of(PAGE_SIZE) { return Err(Error::new(EINVAL)); } - if size % PAGE_SIZE != 0 { + if !size.is_multiple_of(PAGE_SIZE) { warn!( "physmap size {} is not multiple of PAGE_SIZE {}", size, PAGE_SIZE diff --git a/src/scheme/mod.rs b/src/scheme/mod.rs index 6ea3864d7c..843a36d100 100644 --- a/src/scheme/mod.rs +++ b/src/scheme/mod.rs @@ -306,14 +306,14 @@ impl KernelScheme for SchemeList { ) -> Result { match self.get_user_inner(id, token) { Some(inner) => inner.fevent(flags), - _ => return Err(Error::new(EBADF)), + _ => Err(Error::new(EBADF)), } } fn fsync(&self, id: usize, token: &mut CleanLockToken) -> Result<()> { match self.get_user_inner(id, token) { Some(inner) => inner.fsync(), - None => return Err(Error::new(EBADF)), + None => Err(Error::new(EBADF)), } } @@ -333,7 +333,7 @@ impl KernelScheme for SchemeList { ) -> Result { match self.get_user_inner(id, token) { Some(inner) => inner.read(buf, flags, token), - None => return Err(Error::new(EBADF)), + None => Err(Error::new(EBADF)), } } @@ -347,7 +347,7 @@ impl KernelScheme for SchemeList { ) -> Result { match self.get_user_inner(id, token) { Some(inner) => inner.write(buf, token), - None => return Err(Error::new(EBADF)), + None => Err(Error::new(EBADF)), } } diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs index 1b8cc06da6..71b78d8366 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -980,8 +980,8 @@ impl ContextHandle { let state = if data.thread_control_addr != 0 && data.proc_control_addr != 0 { let validate_off = |addr, sz| { - let off = addr % PAGE_SIZE; - if off % mem::align_of::() == 0 && off + sz <= PAGE_SIZE { + let off: usize = addr % PAGE_SIZE; + if off.is_multiple_of(mem::align_of::()) && off + sz <= PAGE_SIZE { Ok(off as u16) } else { Err(Error::new(EINVAL)) @@ -1077,7 +1077,7 @@ impl ContextHandle { Ok(mem::size_of::()) } - ContextHandle::CurrentAddrSpace { .. } => { + ContextHandle::CurrentAddrSpace => { let mut iter = buf.usizes(); let addrspace_fd = iter.next().ok_or(Error::new(EINVAL))??; let sp = iter.next().ok_or(Error::new(EINVAL))??; diff --git a/src/scheme/user.rs b/src/scheme/user.rs index 49f2e0a82c..1410a86082 100644 --- a/src/scheme/user.rs +++ b/src/scheme/user.rs @@ -567,12 +567,10 @@ impl UserInner { }, )?; - let head = CopyInfo { + CopyInfo { src: Some(array), dst: WRITE.then_some(head_part_of_buf.reinterpret_unchecked()), - }; - - head + } } else { CopyInfo { src: None, @@ -1002,7 +1000,7 @@ impl UserInner { let page_count = unaligned_size.div_ceil(PAGE_SIZE); - if map.address % PAGE_SIZE != 0 { + if !map.address.is_multiple_of(PAGE_SIZE) { return Err(Error::new(EINVAL)); }; @@ -1011,7 +1009,7 @@ impl UserInner { let dst_base = (map.address != 0 || fixed) .then_some(Page::containing_address(VirtualAddress::new(map.address))); - if map.offset % PAGE_SIZE != 0 { + if !map.offset.is_multiple_of(PAGE_SIZE) { return Err(Error::new(EINVAL)); } @@ -1933,7 +1931,7 @@ impl KernelScheme for UserScheme { token: &mut CleanLockToken, ) -> Result { let inner = self.inner.clone(); - if payload.len() % mem::size_of::() != 0 { + if !payload.len().is_multiple_of(mem::size_of::()) { return Err(Error::new(EINVAL)); } diff --git a/src/syscall/fs.rs b/src/syscall/fs.rs index a04d58576b..6c8aa6664d 100644 --- a/src/syscall/fs.rs +++ b/src/syscall/fs.rs @@ -590,10 +590,10 @@ pub fn mremap( flags: usize, token: &mut CleanLockToken, ) -> Result { - if old_address % PAGE_SIZE != 0 - || old_size % PAGE_SIZE != 0 - || new_address % PAGE_SIZE != 0 - || new_size % PAGE_SIZE != 0 + if !old_address.is_multiple_of(PAGE_SIZE) + || !old_size.is_multiple_of(PAGE_SIZE) + || !new_address.is_multiple_of(PAGE_SIZE) + || !new_size.is_multiple_of(PAGE_SIZE) { return Err(Error::new(EINVAL)); } diff --git a/src/syscall/futex.rs b/src/syscall/futex.rs index 2659fc8cb7..673d089432 100644 --- a/src/syscall/futex.rs +++ b/src/syscall/futex.rs @@ -102,7 +102,7 @@ pub fn futex( let (fetched, expected) = if op == FUTEX_WAIT { // Must be aligned, otherwise it could cross a page boundary and mess up the // (simpler) validation we did in the first place. - if addr % 4 != 0 { + if !addr.is_multiple_of(4) { return Err(Error::new(EINVAL)); } @@ -123,7 +123,7 @@ pub fn futex( use core::sync::atomic::AtomicU64; // op == FUTEX_WAIT64 - if addr % 8 != 0 { + if !addr.is_multiple_of(8) { return Err(Error::new(EINVAL)); } ( @@ -157,7 +157,7 @@ pub fn futex( futexes .entry(target_physaddr) - .or_insert_with(|| Vec::new()) + .or_insert_with(Vec::new) .push(FutexEntry { target_virtaddr, context_lock: context_lock.clone(), diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 24476049a8..3e43989e18 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -80,7 +80,7 @@ pub fn mprotect(address: usize, size: usize, flags: MapFlags) -> Result<()> { const KERNEL_METADATA_BASE: usize = crate::USER_END_OFFSET - syscall::KERNEL_METADATA_SIZE; const KERNEL_METADATA_PAGE_COUNT: usize = syscall::KERNEL_METADATA_SIZE / PAGE_SIZE + { - if syscall::KERNEL_METADATA_SIZE % PAGE_SIZE == 0 { + if syscall::KERNEL_METADATA_SIZE.is_multiple_of(PAGE_SIZE) { 0 } else { 1 diff --git a/src/syscall/usercopy.rs b/src/syscall/usercopy.rs index 735471b438..d4b3016fe8 100644 --- a/src/syscall/usercopy.rs +++ b/src/syscall/usercopy.rs @@ -228,7 +228,7 @@ fn is_kernel_mem(slice: &[u8]) -> bool { /// - the region is empty (EINVAL), or /// - any byte in the region exceeds USER_END_OFFSET (EFAULT). pub fn validate_region(address: usize, size: usize) -> Result { - if address % PAGE_SIZE != 0 || size % PAGE_SIZE != 0 || size == 0 { + if !address.is_multiple_of(PAGE_SIZE) || !size.is_multiple_of(PAGE_SIZE) || size == 0 { return Err(Error::new(EINVAL)); } if address.saturating_add(size) > crate::USER_END_OFFSET {