Skip to content

Commit e06cdc7

Browse files
committed
follow-up to reviews from RalfJung
1 parent 3ffbeae commit e06cdc7

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

src/helpers.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -448,31 +448,30 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
448448
}
449449

450450
fn read_os_str_from_target_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
451-
if cfg!(target_os = "unix") {
452-
self.read_os_str_from_c_str(scalar)
453-
} else if cfg!(target_os = "windows") {
454-
self.read_os_str_from_wide_str(scalar)
455-
} else {
456-
throw_unsup_format!("support for target OS not yet available")
451+
let target_os = self.eval_context_ref().tcx.sess.target.target.target_os.as_str();
452+
match target_os {
453+
"linux" => self.read_os_str_from_c_str(scalar).map(|x| x.to_os_string()),
454+
"windows" => self.read_os_str_from_wide_str(scalar),
455+
_ => throw_unsup_format!("support for target OS not yet available"),
457456
}
458457
}
459458

460459
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
461460
/// the Unix APIs usually handle.
462-
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString>
461+
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
463462
where
464463
'tcx: 'a,
465464
'mir: 'a,
466465
{
467466
#[cfg(target_os = "unix")]
468-
fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, OsString> {
469-
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes).to_os_string())
467+
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
468+
Ok(std::os::unix::ffi::OsStringExt::from_bytes(bytes))
470469
}
471470
#[cfg(not(target_os = "unix"))]
472-
fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, OsString> {
471+
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
473472
let s = std::str::from_utf8(bytes)
474473
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
475-
Ok(OsStr::new(s).to_os_string())
474+
Ok(OsStr::new(s))
476475
}
477476

478477
let this = self.eval_context_ref();
@@ -509,12 +508,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
509508
mplace: MPlaceTy<'tcx, Tag>,
510509
size: u64,
511510
) -> InterpResult<'tcx, (bool, u64)> {
512-
if cfg!(target_os = "unix") {
513-
self.write_os_str_to_c_str(os_str, mplace.ptr, size)
514-
} else if cfg!(target_os = "windows") {
515-
self.write_os_str_to_wide_str(os_str, mplace, size)
516-
} else {
517-
panic!("support for target OS not yet available")
511+
let target_os = self.eval_context_ref().tcx.sess.target.target.target_os.as_str();
512+
match target_os {
513+
"linux" => self.write_os_str_to_c_str(os_str, mplace.ptr, size),
514+
"windows" => self.write_os_str_to_wide_str(os_str, mplace, size),
515+
_ => panic!("support for target OS not yet available"),
518516
}
519517
}
520518

@@ -574,7 +572,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
574572
}
575573
#[cfg(not(target_os = "windows"))]
576574
fn os_str_to_u16vec(os_str: &OsStr) -> Vec<u16> {
577-
os_str.to_str().encode_utf16().collect()
575+
os_str.to_str().unwrap().encode_utf16().collect()
578576
}
579577

580578
let u16_vec = os_str_to_u16vec(os_str);
@@ -589,7 +587,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
589587

590588
// Store the UTF-16 string.
591589
let char_size = Size::from_bytes(2);
592-
for (idx, &c) in u16_vec.iter().enumerate() {
590+
for (idx, c) in u16_vec.into_iter().chain(iter::once(0x0000)).enumerate() {
593591
let place = this.mplace_field(mplace, idx as u64)?;
594592
this.write_scalar(Scalar::from_uint(c, char_size), place.into())?;
595593
}
@@ -601,12 +599,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
601599
os_str: &OsStr,
602600
memkind: MemoryKind<MiriMemoryKind>,
603601
) -> MPlaceTy<'tcx, Tag> {
604-
if cfg!(target_os = "unix") {
605-
self.alloc_os_str_as_c_str(os_str, memkind)
606-
} else if cfg!(target_os = "windows") {
607-
self.alloc_os_str_as_wide_str(os_str, memkind)
608-
} else {
609-
panic!("support for target OS not yet available")
602+
let target_os = self.eval_context_ref().tcx.sess.target.target.target_os.as_str();
603+
match target_os {
604+
"linux" => self.alloc_os_str_as_c_str(os_str, memkind),
605+
"windows" => self.alloc_os_str_as_wide_str(os_str, memkind),
610606
}
611607
}
612608

0 commit comments

Comments
 (0)