Skip to content

Commit 9112251

Browse files
authored
Merge pull request #347 from rust-osdev/fix-virtaddrnotvalid
Fix: `VirtAddrNotValid` and `PhysAddrNotValid` should contain the whole address
2 parents a143355 + b644c2c commit 9112251

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/addr.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,18 @@ pub struct PhysAddr(u64);
4040
/// a valid sign extension and are not null either. So automatic sign extension would have
4141
/// overwritten possibly meaningful bits. This likely indicates a bug, for example an invalid
4242
/// address calculation.
43-
#[derive(Debug)]
43+
///
44+
/// Contains the invalid address.
4445
pub struct VirtAddrNotValid(pub u64);
4546

47+
impl core::fmt::Debug for VirtAddrNotValid {
48+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49+
f.debug_tuple("VirtAddrNotValid")
50+
.field(&format_args!("{:#x}", self.0))
51+
.finish()
52+
}
53+
}
54+
4655
impl VirtAddr {
4756
/// Creates a new canonical virtual address.
4857
///
@@ -70,7 +79,7 @@ impl VirtAddr {
7079
match addr.get_bits(47..64) {
7180
0 | 0x1ffff => Ok(VirtAddr(addr)), // address is canonical
7281
1 => Ok(VirtAddr::new_truncate(addr)), // address needs sign extension
73-
other => Err(VirtAddrNotValid(other)),
82+
_ => Err(VirtAddrNotValid(addr)),
7483
}
7584
}
7685

@@ -325,9 +334,18 @@ impl Sub<VirtAddr> for VirtAddr {
325334
/// A passed `u64` was not a valid physical address.
326335
///
327336
/// This means that bits 52 to 64 were not all null.
328-
#[derive(Debug)]
337+
///
338+
/// Contains the invalid address.
329339
pub struct PhysAddrNotValid(pub u64);
330340

341+
impl core::fmt::Debug for PhysAddrNotValid {
342+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
343+
f.debug_tuple("PhysAddrNotValid")
344+
.field(&format_args!("{:#x}", self.0))
345+
.finish()
346+
}
347+
}
348+
331349
impl PhysAddr {
332350
/// Creates a new physical address.
333351
///
@@ -367,7 +385,7 @@ impl PhysAddr {
367385
pub fn try_new(addr: u64) -> Result<PhysAddr, PhysAddrNotValid> {
368386
match addr.get_bits(52..64) {
369387
0 => Ok(PhysAddr(addr)), // address is valid
370-
other => Err(PhysAddrNotValid(other)),
388+
_ => Err(PhysAddrNotValid(addr)),
371389
}
372390
}
373391

0 commit comments

Comments
 (0)