Skip to content

Commit efd7f00

Browse files
committed
Fixed overflow bug in PageRangeInclusive
1 parent 33b4c2d commit efd7f00

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/structures/paging/page.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,16 @@ impl<S: PageSize> Iterator for PageRangeInclusive<S> {
380380
fn next(&mut self) -> Option<Self::Item> {
381381
if self.start <= self.end {
382382
let page = self.start;
383-
self.start += 1;
383+
384+
// If the end of the inclusive range is the maximum page possible for size S,
385+
// incrementing start until it is greater than the end will cause an integer overflow.
386+
// So instead, in that case we decrement end rather than incrementing start.
387+
let max_page_addr = VirtAddr::new(u64::MAX) - (S::SIZE - 1);
388+
if self.start.start_address() < max_page_addr {
389+
self.start += 1;
390+
} else {
391+
self.end -= 1;
392+
}
384393
Some(page)
385394
} else {
386395
None

0 commit comments

Comments
 (0)