diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 222bef4ff136b..4b0c0c3ee6173 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -846,7 +846,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { &self, ptr: Scalar, size: Size, - allow_ptr: bool, + allow_ptr_and_undef: bool, ) -> EvalResult<'tcx> { // Empty accesses don't need to be valid pointers, but they should still be non-NULL let align = Align::from_bytes(1, 1).unwrap(); @@ -857,9 +857,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { let ptr = ptr.to_ptr()?; // Check bounds, align and relocations on the edges self.get_bytes_with_undef_and_ptr(ptr, size, align)?; - // Check undef, and maybe ptr - self.check_defined(ptr, size)?; - if !allow_ptr { + // Check undef and ptr + if !allow_ptr_and_undef { + self.check_defined(ptr, size)?; self.check_relocations(ptr, size)?; } Ok(()) diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index aadb5cc871e1a..c446980d04995 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -517,7 +517,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> // reject it. However, that's good: We don't inherently want // to reject those pointers, we just do not have the machinery to // talk about parts of a pointer. - match self.memory.check_bytes(dest.ptr, size, /*allow_ptr*/!const_mode) { + // We also accept undef, for consistency with the type-based checks. + match self.memory.check_bytes( + dest.ptr, + size, + /*allow_ptr_and_undef*/!const_mode, + ) { // In the happy case, we needn't check anything else. Ok(()) => {}, // Some error happened, try to provide a more detailed description.