Skip to content

Commit 0201cc5

Browse files
committed
Fix writing errors
1 parent 85941c7 commit 0201cc5

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/helpers.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -353,36 +353,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
353353

354354
fn write_os_str_to_c_string(&mut self, os_str: &OsStr, ptr: Pointer<Tag>, size: u64) -> InterpResult<'tcx> {
355355
let bytes = os_str_to_bytes(os_str)?;
356+
let len = bytes.len();
356357
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
357358
// terminator to memory using the `ptr` pointer would cause an overflow.
358-
if (bytes.len() as u64) < size {
359+
if (len as u64) < size {
359360
let this = self.eval_context_mut();
360361
let tcx = &{ this.tcx.tcx };
362+
let buffer = this.memory.get_mut(ptr.alloc_id)?.get_bytes_mut(tcx, ptr, Size::from_bytes(len as u64 + 1))?;
363+
buffer[..len].copy_from_slice(bytes);
361364
// This is ok because the buffer was strictly larger than `bytes`, so after adding the
362365
// null terminator, the buffer size is larger or equal to `bytes.len()`, meaning that
363366
// `bytes` actually fit inside tbe buffer.
364-
this.memory
365-
.get_mut(ptr.alloc_id)?
366-
.write_bytes(tcx, ptr, &bytes)?;
367-
// We write the `/0` terminator
368-
let tail_ptr = ptr.offset(Size::from_bytes(bytes.len() as u64 + 1), this)?;
369-
this.memory
370-
.get_mut(ptr.alloc_id)?
371-
.write_bytes(tcx, tail_ptr, b"0")
367+
buffer[len] = 0;
368+
Ok(())
372369
} else {
373370
throw_unsup_format!("OsString is larger than destination")
374371
}
375372
}
376373
}
377374

378375
#[cfg(target_os = "unix")]
379-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> {
380-
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes))
376+
fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
377+
std::os::unix::ffi::OsStringExt::into_bytes(os_str)
381378
}
382379

383380
#[cfg(target_os = "unix")]
384-
fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
385-
std::os::unix::ffi::OsStringExt::into_bytes(os_str)
381+
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> {
382+
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes))
386383
}
387384

388385
// On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the

0 commit comments

Comments
 (0)