apply is_multiple_of lint

This commit is contained in:
auronandace
2026-02-21 08:39:26 +00:00
parent 02a68d3c87
commit df469ddcb3
10 changed files with 27 additions and 30 deletions
+2 -4
View File
@@ -752,7 +752,7 @@ impl PageSpan {
Self::validate(address, size).filter(|this| !this.is_empty())
}
pub fn validate(address: VirtualAddress, size: usize) -> Option<Self> {
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<Grant> {
// 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();
+1 -1
View File
@@ -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)
}
}
+3 -2
View File
@@ -127,12 +127,13 @@ impl MemoryScheme {
) -> Result<usize> {
// 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
+4 -4
View File
@@ -306,14 +306,14 @@ impl KernelScheme for SchemeList {
) -> Result<EventFlags> {
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<usize> {
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<usize> {
match self.get_user_inner(id, token) {
Some(inner) => inner.write(buf, token),
None => return Err(Error::new(EBADF)),
None => Err(Error::new(EBADF)),
}
}
+3 -3
View File
@@ -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::<usize>() == 0 && off + sz <= PAGE_SIZE {
let off: usize = addr % PAGE_SIZE;
if off.is_multiple_of(mem::align_of::<usize>()) && off + sz <= PAGE_SIZE {
Ok(off as u16)
} else {
Err(Error::new(EINVAL))
@@ -1077,7 +1077,7 @@ impl ContextHandle {
Ok(mem::size_of::<usize>())
}
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))??;
+5 -7
View File
@@ -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<usize> {
let inner = self.inner.clone();
if payload.len() % mem::size_of::<usize>() != 0 {
if !payload.len().is_multiple_of(mem::size_of::<usize>()) {
return Err(Error::new(EINVAL));
}
+4 -4
View File
@@ -590,10 +590,10 @@ pub fn mremap(
flags: usize,
token: &mut CleanLockToken,
) -> Result<usize> {
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));
}
+3 -3
View File
@@ -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(),
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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<PageSpan> {
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 {