diff options
| author | Ralf Jung <post@ralfj.de> | 2024-07-27 18:09:50 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-07-27 21:12:54 +0200 |
| commit | f8ebe8d783e20c44508fab32b708f1b9d9a4bf13 (patch) | |
| tree | 27b486aa8fa8400866095e053fa478fe9a7a7d94 /compiler/rustc_middle | |
| parent | 5b38b149dc5f8a9aaeb06eac6c6f819e0a17caee (diff) | |
| download | rust-f8ebe8d783e20c44508fab32b708f1b9d9a4bf13.tar.gz rust-f8ebe8d783e20c44508fab32b708f1b9d9a4bf13.zip | |
improve dangling/oob errors and make them more uniform
Diffstat (limited to 'compiler/rustc_middle')
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/error.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/pointer.rs | 9 |
2 files changed, 14 insertions, 6 deletions
diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 9df19565ab3..68676e862ef 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -329,16 +329,21 @@ pub enum UndefinedBehaviorInfo<'tcx> { /// Using a pointer after it got freed. PointerUseAfterFree(AllocId, CheckInAllocMsg), /// Used a pointer outside the bounds it is valid for. - /// (If `ptr_size > 0`, determines the size of the memory range that was expected to be in-bounds.) PointerOutOfBounds { alloc_id: AllocId, alloc_size: Size, ptr_offset: i64, - ptr_size: Size, + /// The size of the memory range that was expected to be in-bounds. + inbounds_size: Size, msg: CheckInAllocMsg, }, /// Using an integer as a pointer in the wrong way. - DanglingIntPointer(u64, CheckInAllocMsg), + DanglingIntPointer { + addr: u64, + /// The size of the memory range that was expected to be in-bounds (or 0 if we don't know). + inbounds_size: Size, + msg: CheckInAllocMsg, + }, /// Used a pointer with bad alignment. AlignmentCheckFailed(Misalignment, CheckAlignMsg), /// Writing to read-only memory. diff --git a/compiler/rustc_middle/src/mir/interpret/pointer.rs b/compiler/rustc_middle/src/mir/interpret/pointer.rs index a0acacc844f..074245fcd32 100644 --- a/compiler/rustc_middle/src/mir/interpret/pointer.rs +++ b/compiler/rustc_middle/src/mir/interpret/pointer.rs @@ -180,9 +180,12 @@ impl Provenance for CtfeProvenance { fn fmt(ptr: &Pointer<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Print AllocId. fmt::Debug::fmt(&ptr.provenance.alloc_id(), f)?; // propagates `alternate` flag - // Print offset only if it is non-zero. - if ptr.offset.bytes() > 0 { - write!(f, "+{:#x}", ptr.offset.bytes())?; + // Print offset only if it is non-zero. Print it signed. + let signed_offset = ptr.offset.bytes() as i64; + if signed_offset > 0 { + write!(f, "+{:#x}", signed_offset)?; + } else if signed_offset < 0 { + write!(f, "-{:#x}", signed_offset.unsigned_abs())?; } // Print immutable status. if ptr.provenance.immutable() { |
