Skip to content

Commit 941c50d

Browse files
committed
gdt: Check that MAX is in range
The GDT can have a maxium length of 2^16 bytes, and must contain at least one null descriptor. As `MAX` counts the number of `u64` entries, we must have `0 < MAX <= 2^13`. Unfortunely, we cannot do this check with a `where` clause, as `feature(generic_const_expers)` is not yet stable. However, we can do this check with an `assert!` in `GlobalDescriptorTable::empty()`, which is a `const fn`. Pointed out by @Freax13 in #360 (comment) Signed-off-by: Joe Richey <[email protected]>
1 parent 2e65c45 commit 941c50d

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/structures/gdt.rs

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
6464
/// Creates an empty GDT which can hold `MAX` number of [`Descriptor`]s.
6565
#[inline]
6666
pub const fn empty() -> Self {
67+
// TODO: Replace with compiler error when feature(generic_const_exprs) is stable.
68+
assert!(MAX > 0, "A GDT cannot have 0 entries");
69+
assert!(MAX <= (1 << 13), "A GDT can only have at most 2^13 entries");
6770
Self {
6871
table: [0; MAX],
6972
next_free: 1,
@@ -184,6 +187,8 @@ impl<const MAX: usize> GlobalDescriptorTable<MAX> {
184187
use core::mem::size_of;
185188
super::DescriptorTablePointer {
186189
base: crate::VirtAddr::new(self.table.as_ptr() as u64),
190+
// 0 < self.next_free <= MAX <= 2^13, so the limit calculation
191+
// will not underflow or overflow.
187192
limit: (self.next_free * size_of::<u64>() - 1) as u16,
188193
}
189194
}

0 commit comments

Comments
 (0)