diff --git a/src/addr.rs b/src/addr.rs index 1c93f0219..16f568f9e 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -107,6 +107,20 @@ impl VirtAddr { self.0 } + /// Preforms a checked add between two virtual addresses + /// + /// Panics if an integer overflow occurs + #[inline] + pub fn checked_add(self, addr: U) -> VirtAddr + where + U: Into, + { + VirtAddr::new( + self.as_u64() + .checked_add(addr.into()) + .expect("Integer overflow"), + ) + } /// Creates a virtual address from the given pointer // cfg(target_pointer_width = "32") is only here for backwards // compatibility: Earlier versions of this crate did not have any `cfg()` diff --git a/src/structures/paging/mapper/offset_page_table.rs b/src/structures/paging/mapper/offset_page_table.rs index 6cfaf42f6..a5876252f 100644 --- a/src/structures/paging/mapper/offset_page_table.rs +++ b/src/structures/paging/mapper/offset_page_table.rs @@ -50,7 +50,7 @@ struct PhysOffset { unsafe impl PageTableFrameMapping for PhysOffset { fn frame_to_pointer(&self, frame: PhysFrame) -> *mut PageTable { - let virt = self.offset + frame.start_address().as_u64(); + let virt = self.offset.checked_add(frame.start_address().as_u64()); virt.as_mut_ptr() } }