From f93a146433a1b724065672dbad54e557c1e70a62 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 22 Mar 2022 18:58:54 -0400 Subject: interpret/memory: simplify check_and_deref_ptr --- compiler/rustc_const_eval/src/interpret/memory.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) (limited to 'compiler/rustc_const_eval') diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 6397fcaaf8d..871b7578abd 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -427,22 +427,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> { } } - // Extract from the pointer an `Option` and an offset, which is relative to the - // allocation or (if that is `None`) an absolute address. - let ptr_or_addr = if size.bytes() == 0 { - // Let's see what we can do, but don't throw errors if there's nothing there. - self.ptr_try_get_alloc(ptr) - } else { - // A "real" access, we insist on getting an `AllocId`. - Ok(self.ptr_get_alloc(ptr)?) - }; - Ok(match ptr_or_addr { + Ok(match self.ptr_try_get_alloc(ptr) { Err(addr) => { - // No memory is actually being accessed. - debug_assert!(size.bytes() == 0); - // Must be non-null. - if addr == 0 { - throw_ub!(DanglingIntPointer(0, msg)) + // We couldn't get a proper allocation. This is only okay if the access size is 0, + // and the address is not null. + if size.bytes() > 0 || addr == 0 { + throw_ub!(DanglingIntPointer(addr, msg)); } // Must be aligned. if let Some(align) = align { -- cgit 1.4.1-3-g733a5