From 1482fd0a52d1300dfddceb19d1818ac5a8530406 Mon Sep 17 00:00:00 2001 From: goldencm Date: Wed, 11 Aug 2021 13:57:17 -0400 Subject: [PATCH] impl checked_add to VirtAddr frame_to_pointer uses VirtAddr checked_add added panic doc fixed typo fixed formatting fixed formatting again --- src/addr.rs | 14 ++++++++++++++ src/structures/paging/mapper/offset_page_table.rs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) 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() } }