1
Fork 0

Rollup merge of #98860 - RalfJung:dangling-int-ptr, r=davidtwco

adjust dangling-int-ptr error message

based on suggestions by `@saethlin` in https://github.com/rust-lang/miri/issues/2163

Fixes https://github.com/rust-lang/miri/issues/2163

I also did a bit of refactoring on this, so we have a helper method to create a `Pointer` with `None` provenance.
This commit is contained in:
Matthias Krüger 2022-07-05 17:08:10 +02:00 committed by GitHub
commit 69195c026e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 60 additions and 47 deletions

View file

@ -186,7 +186,7 @@ pub enum CheckInAllocMsg {
impl fmt::Display for CheckInAllocMsg {
/// When this is printed as an error the context looks like this:
/// "{msg}0x01 is not a valid pointer".
/// "{msg}{pointer} is a dangling pointer".
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@ -194,9 +194,9 @@ impl fmt::Display for CheckInAllocMsg {
match *self {
CheckInAllocMsg::DerefTest => "dereferencing pointer failed: ",
CheckInAllocMsg::MemoryAccessTest => "memory access failed: ",
CheckInAllocMsg::PointerArithmeticTest => "pointer arithmetic failed: ",
CheckInAllocMsg::PointerArithmeticTest => "out-of-bounds pointer arithmetic: ",
CheckInAllocMsg::OffsetFromTest => "out-of-bounds offset_from: ",
CheckInAllocMsg::InboundsTest => "",
CheckInAllocMsg::InboundsTest => "out-of-bounds pointer use: ",
}
)
}
@ -350,14 +350,12 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
ptr_size = ptr_size.bytes(),
ptr_size_p = pluralize!(ptr_size.bytes()),
),
DanglingIntPointer(0, CheckInAllocMsg::InboundsTest) => {
write!(f, "null pointer is not a valid pointer for this operation")
}
DanglingIntPointer(0, msg) => {
write!(f, "{msg}null pointer is not a valid pointer")
}
DanglingIntPointer(i, msg) => {
write!(f, "{msg}{i:#x} is not a valid pointer")
write!(
f,
"{msg}{pointer} is a dangling pointer (it has no provenance)",
pointer = Pointer::<Option<AllocId>>::from_addr(*i),
)
}
AlignmentCheckFailed { required, has } => write!(
f,

View file

@ -181,7 +181,17 @@ impl<Tag: Provenance> fmt::Debug for Pointer<Option<Tag>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.provenance {
Some(tag) => Provenance::fmt(&Pointer::new(tag, self.offset), f),
None => write!(f, "{:#x}", self.offset.bytes()),
None => write!(f, "{:#x}[noalloc]", self.offset.bytes()),
}
}
}
impl<Tag: Provenance> fmt::Display for Pointer<Option<Tag>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.provenance.is_none() && self.offset.bytes() == 0 {
write!(f, "null pointer")
} else {
fmt::Debug::fmt(self, f)
}
}
}
@ -226,9 +236,14 @@ impl<Tag> Pointer<Option<Tag>> {
}
impl<Tag> Pointer<Option<Tag>> {
#[inline(always)]
pub fn from_addr(addr: u64) -> Self {
Pointer { provenance: None, offset: Size::from_bytes(addr) }
}
#[inline(always)]
pub fn null() -> Self {
Pointer { provenance: None, offset: Size::ZERO }
Pointer::from_addr(0)
}
}