Skip to content

Commit 5dee646

Browse files
committed
read, write: move cast-to-usize logic up and deduplicate it
1 parent fb11930 commit 5dee646

File tree

1 file changed

+6
-4
lines changed
  • src/tools/miri/src/shims/unix

1 file changed

+6
-4
lines changed

src/tools/miri/src/shims/unix/fd.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
579579
let count = count
580580
.min(u64::try_from(this.target_isize_max()).unwrap())
581581
.min(u64::try_from(isize::MAX).unwrap());
582+
let count = usize::try_from(count).unwrap(); // now it fits in a `usize`
582583
let communicate = this.machine.communicate();
583584

584585
// We temporarily dup the FD to be able to retain mutable access to `this`.
@@ -595,15 +596,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
595596
// `usize::MAX` because it is bounded by the host's `isize`.
596597

597598
match offset {
598-
None => fd.read(&fd, communicate, buf, usize::try_from(count).unwrap(), dest, this)?,
599+
None => fd.read(&fd, communicate, buf, count, dest, this)?,
599600
Some(offset) => {
600601
let Ok(offset) = u64::try_from(offset) else {
601602
let einval = this.eval_libc("EINVAL");
602603
this.set_last_error(einval)?;
603604
this.write_int(-1, dest)?;
604605
return Ok(());
605606
};
606-
fd.pread(communicate, offset, buf, usize::try_from(count).unwrap(), dest, this)?
607+
fd.pread(communicate, offset, buf, count, dest, this)?
607608
}
608609
};
609610
Ok(())
@@ -629,6 +630,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
629630
let count = count
630631
.min(u64::try_from(this.target_isize_max()).unwrap())
631632
.min(u64::try_from(isize::MAX).unwrap());
633+
let count = usize::try_from(count).unwrap(); // now it fits in a `usize`
632634
let communicate = this.machine.communicate();
633635

634636
// We temporarily dup the FD to be able to retain mutable access to `this`.
@@ -639,15 +641,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
639641
};
640642

641643
match offset {
642-
None => fd.write(&fd, communicate, buf, usize::try_from(count).unwrap(), dest, this)?,
644+
None => fd.write(&fd, communicate, buf, count, dest, this)?,
643645
Some(offset) => {
644646
let Ok(offset) = u64::try_from(offset) else {
645647
let einval = this.eval_libc("EINVAL");
646648
this.set_last_error(einval)?;
647649
this.write_int(-1, dest)?;
648650
return Ok(());
649651
};
650-
fd.pwrite(communicate, buf, usize::try_from(count).unwrap(), offset, dest, this)?
652+
fd.pwrite(communicate, buf, count, offset, dest, this)?
651653
}
652654
};
653655
Ok(())

0 commit comments

Comments
 (0)