Avoid writing more bytes than necessary

This commit is contained in:
Christian Poveda 2019-09-20 02:13:48 -05:00
parent 133c2b39db
commit 49275d4269

View File

@ -120,25 +120,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
) -> InterpResult<'tcx, Scalar<Tag>> {
let this = self.eval_context_mut();
if this.machine.communicate {
let tcx = &{this.tcx.tcx};
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
// If we cannot get the current directory, we return null
if let Ok(cwd) = env::current_dir() {
// It is not clear what happens with non-utf8 paths here
let mut bytes = cwd.display().to_string().into_bytes();
// If the buffer is smaller than the path, we return null
if bytes.len() as u64 <= size {
// We need `size` bytes exactly
bytes.resize(size as usize, 0);
this.memory_mut().get_mut(buf.alloc_id)?.write_bytes(tcx, buf, &bytes)?;
return Ok(Scalar::Ptr(buf))
}
}
return Ok(Scalar::ptr_null(&*this.tcx));
if !this.machine.communicate {
throw_unsup_format!("Function not available when isolation is enabled")
}
throw_unsup_format!("Function not available when isolation is enabled")
let tcx = &{this.tcx.tcx};
let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
// If we cannot get the current directory, we return null
// FIXME: Technically we have to set the `errno` global too
if let Ok(cwd) = env::current_dir() {
// It is not clear what happens with non-utf8 paths here
let mut bytes = cwd.display().to_string().into_bytes();
// If the buffer is smaller or equal than the path, we return null.
// FIXME: Technically we have to set the `errno` global too
if (bytes.len() as u64) < size {
// We add a `/0` terminator
bytes.push(0);
// This is ok because the buffer is larger than the path with the null terminator.
this.memory_mut().get_mut(buf.alloc_id)?.write_bytes(tcx, buf, &bytes)?;
return Ok(Scalar::Ptr(buf))
}
}
Ok(Scalar::ptr_null(&*this.tcx))
}
}