Skip to content

Commit 019c10d

Browse files
committed
Move the actual writing to a free function
1 parent 5bd5eec commit 019c10d

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/shims/unix/fd.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
582582
fd.pread(communicate, &mut bytes, offset, this)
583583
}
584584
};
585-
let res = write_byte_helper(buf, bytes, result, this)?;
585+
let res = read_byte_helper(buf, bytes, result, this)?;
586586
this.write_scalar(res, dest)?;
587587
Ok(())
588588
}
@@ -616,27 +616,12 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
616616
this.write_scalar(Scalar::from_target_isize(res, this), dest)?;
617617
return Ok(());
618618
};
619-
620-
let result = match offset {
621-
None => fd.write(&fd, communicate, &bytes, this),
622-
Some(offset) => {
623-
let Ok(offset) = u64::try_from(offset) else {
624-
let einval = this.eval_libc("EINVAL");
625-
this.set_last_error(einval)?;
626-
this.write_scalar(Scalar::from_target_isize(-1, this), dest)?;
627-
return Ok(());
628-
};
629-
fd.pwrite(communicate, &bytes, offset, this)
630-
}
631-
};
632-
633-
let result = result?.map(|c| i64::try_from(c).unwrap());
634-
let res = this.try_unwrap_io_result(result)?;
635-
this.write_scalar(Scalar::from_target_isize(res, this), dest)?;
619+
let res = write_byte_helper(offset, fd, bytes, communicate, this)?;
620+
this.write_scalar(res, dest)?;
636621
Ok(())
637622
}
638623
}
639-
fn write_byte_helper<'tcx>(
624+
fn read_byte_helper<'tcx>(
640625
buf: Pointer,
641626
bytes: Vec<u8>,
642627
result: InterpResult<'tcx, io::Result<usize>>,
@@ -664,3 +649,26 @@ fn write_byte_helper<'tcx>(
664649
}
665650
}
666651
}
652+
653+
fn write_byte_helper<'tcx>(
654+
offset: Option<i128>,
655+
fd: FileDescriptionRef,
656+
bytes: Vec<u8>,
657+
communicate: bool,
658+
ecx: &mut MiriInterpCx<'tcx>,
659+
) -> InterpResult<'tcx, Scalar> {
660+
let result = match offset {
661+
None => fd.write(&fd, communicate, &bytes, ecx),
662+
Some(offset) => {
663+
let Ok(offset) = u64::try_from(offset) else {
664+
let einval = ecx.eval_libc("EINVAL");
665+
ecx.set_last_error(einval)?;
666+
return Ok(Scalar::from_target_isize(-1, ecx));
667+
};
668+
fd.pwrite(communicate, &bytes, offset, ecx)
669+
}
670+
};
671+
let result = result?.map(|c| i64::try_from(c).unwrap());
672+
let res = ecx.try_unwrap_io_result(result)?;
673+
Ok(Scalar::from_target_isize(res, ecx))
674+
}

0 commit comments

Comments
 (0)