Skip to content

Commit fb4cb5b

Browse files
committed
Make size error distinguishable from other errors
1 parent be415db commit fb4cb5b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/helpers.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,16 +412,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
412412
}
413413

414414
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
415-
/// the Unix APIs usually handle.
416-
fn write_os_str_to_c_string(&mut self, os_str: &OsStr, scalar: Scalar<Tag>, size: u64) -> InterpResult<'tcx> {
415+
/// the Unix APIs usually handle. This function returns `Ok(false)` without trying to write if
416+
/// `size` is not large enough to fit the contents of `os_string` plus a null terminator. It
417+
/// returns `Ok(true)` if the writing process was successful. Otherwise it returns an
418+
/// `InterpError`.
419+
fn write_os_str_to_c_string(
420+
&mut self,
421+
os_str: &OsStr,
422+
scalar: Scalar<Tag>,
423+
size: u64
424+
) -> InterpResult<'tcx, bool> {
417425
let bytes = os_str_to_bytes(os_str)?;
418426
// If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null
419427
// terminator to memory using the `ptr` pointer would cause an overflow.
420428
if size <= bytes.len() as u64 {
421-
throw_unsup_format!("OsString of length {} is too large for destination buffer of size {}", bytes.len(), size)
429+
return Ok(false);
422430
}
423431
// FIXME: We should use `Iterator::chain` instead when rust-lang/rust#65704 lands.
424-
self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat())
432+
self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat())?;
433+
Ok(true)
425434
}
426435
}
427436

src/shims/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
128128
// If we cannot get the current directory, we return null
129129
match env::current_dir() {
130130
Ok(cwd) => {
131-
if this.write_os_str_to_c_string(&OsString::from(cwd), buf, size).is_ok() {
131+
if this.write_os_str_to_c_string(&OsString::from(cwd), buf, size)? {
132132
return Ok(buf);
133133
}
134134
let erange = this.eval_libc("ERANGE")?;

0 commit comments

Comments
 (0)