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

Fixed overflow bug in PageRangeInclusive #351

Merged
merged 3 commits into from
Mar 18, 2022
Merged
Changes from all 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
30 changes: 29 additions & 1 deletion src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,16 @@ impl<S: PageSize> Iterator for PageRangeInclusive<S> {
fn next(&mut self) -> Option<Self::Item> {
if self.start <= self.end {
let page = self.start;
self.start += 1;

// If the end of the inclusive range is the maximum page possible for size S,
// incrementing start until it is greater than the end will cause an integer overflow.
// So instead, in that case we decrement end rather than incrementing start.
let max_page_addr = VirtAddr::new(u64::MAX) - (S::SIZE - 1);
if self.start.start_address() < max_page_addr {
self.start += 1;
} else {
self.end -= 1;
}
Some(page)
} else {
None
Expand Down Expand Up @@ -438,4 +447,23 @@ mod tests {
}
assert_eq!(range_inclusive.next(), None);
}

#[test]
pub fn test_page_range_inclusive_overflow() {
let page_size = Size4KiB::SIZE;
let number = 1000;

let start_addr = VirtAddr::new(u64::MAX).align_down(page_size) - number * page_size;
let start: Page = Page::containing_address(start_addr);
let end = start + number;

let mut range_inclusive = Page::range_inclusive(start, end);
for i in 0..=number {
assert_eq!(
range_inclusive.next(),
Some(Page::containing_address(start_addr + page_size * i))
);
}
assert_eq!(range_inclusive.next(), None);
drzewiec marked this conversation as resolved.
Show resolved Hide resolved
}
}