diff --git a/src/helpers.rs b/src/helpers.rs index c975ad144e26..ddaf8f48a5aa 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -356,19 +356,18 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let len = bytes.len(); // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null // terminator to memory using the `ptr` pointer would cause an overflow. - if (len as u64) < size { - let this = self.eval_context_mut(); - let tcx = &{ this.tcx.tcx }; - let buffer = this.memory.get_mut(ptr.alloc_id)?.get_bytes_mut(tcx, ptr, Size::from_bytes(len as u64 + 1))?; - buffer[..len].copy_from_slice(bytes); - // This is ok because the buffer was strictly larger than `bytes`, so after adding the - // null terminator, the buffer size is larger or equal to `bytes.len()`, meaning that - // `bytes` actually fit inside tbe buffer. - buffer[len] = 0; - Ok(()) - } else { - throw_unsup_format!("OsString is larger than destination") + if size <= bytes.len() as u64 { + throw_unsup_format!("OsString of length {} is too large for destination buffer of size {}", len, size) } + + let this = self.eval_context_mut(); + let buffer = this.memory.get_mut(ptr.alloc_id)?.get_bytes_mut(&*this.tcx, ptr, Size::from_bytes(len as u64 + 1))?; + buffer[..len].copy_from_slice(bytes); + // This is ok because the buffer was strictly larger than `bytes`, so after adding the + // null terminator, the buffer size is larger or equal to `bytes.len()`, meaning that + // `bytes` actually fit inside tbe buffer. + buffer[len] = 0; + Ok(()) } }