Skip to content

gdt: Check that MAX is in range #365

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

Merged
merged 1 commit into from
Mar 30, 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
5 changes: 5 additions & 0 deletions src/structures/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
/// Creates an empty GDT which can hold `MAX` number of [`Descriptor`]s.
#[inline]
pub const fn empty() -> Self {
// TODO: Replace with compiler error when feature(generic_const_exprs) is stable.
assert!(MAX > 0, "A GDT cannot have 0 entries");
assert!(MAX <= (1 << 13), "A GDT can only have at most 2^13 entries");
Self {
table: [0; MAX],
next_free: 1,
Expand Down Expand Up @@ -184,6 +187,8 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
use core::mem::size_of;
super::DescriptorTablePointer {
base: crate::VirtAddr::new(self.table.as_ptr() as u64),
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
// will not underflow or overflow.
limit: (self.next_free * size_of::<u64>() - 1) as u16,
}
}
Expand Down