Skip to content

Commit f162e51

Browse files
committed
Write -1 together with setting the global error value
1 parent c4d5754 commit f162e51

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/helpers.rs

+8
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
781781
dest: &MPlaceTy<'tcx>,
782782
) -> InterpResult<'tcx> {
783783
let err = self.eval_libc(err);
784+
self.set_last_err_and_return_neg1(err, dest)
785+
}
786+
787+
fn set_last_err_and_return_neg1(
788+
&mut self,
789+
err: Scalar,
790+
dest: &MPlaceTy<'tcx>,
791+
) -> InterpResult<'tcx> {
784792
self.set_last_error(err)?;
785793
self.eval_context_mut().write_int(-1, dest)
786794
}

src/shims/unix/fs.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
676676

677677
// `stat` always follows symlinks.
678678
let metadata = match FileMetadata::from_path(this, &path, true)? {
679-
Some(metadata) => metadata,
680-
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
679+
Ok(metadata) => metadata,
680+
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
681681
};
682682
let res = this.macos_stat_write_buf(metadata, buf_op)?;
683683
this.write_int(res, dest)
@@ -706,8 +706,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
706706
}
707707

708708
let metadata = match FileMetadata::from_path(this, &path, false)? {
709-
Some(metadata) => metadata,
710-
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
709+
Ok(metadata) => metadata,
710+
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
711711
};
712712
let res = this.macos_stat_write_buf(metadata, buf_op)?;
713713
this.write_int(res, dest)
@@ -734,8 +734,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
734734
}
735735

736736
let metadata = match FileMetadata::from_fd_num(this, fd)? {
737-
Some(metadata) => metadata,
738-
None => return this.write_int(-1, dest), // `FileMetadata` has set errno
737+
Ok(metadata) => metadata,
738+
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
739739
};
740740
let res = this.macos_stat_write_buf(metadata, buf_op)?;
741741
this.write_int(res, dest)
@@ -824,8 +824,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
824824
FileMetadata::from_path(this, &path, follow_symlink)?
825825
};
826826
let metadata = match metadata {
827-
Some(metadata) => metadata,
828-
None => return this.write_int(-1, dest),
827+
Ok(metadata) => metadata,
828+
Err(e) => return this.set_last_err_and_return_neg1(e, dest),
829829
};
830830

831831
// The `mode` field specifies the type of the file and the permissions over the file for
@@ -1699,7 +1699,7 @@ impl FileMetadata {
16991699
ecx: &mut MiriInterpCx<'tcx>,
17001700
path: &Path,
17011701
follow_symlink: bool,
1702-
) -> InterpResult<'tcx, Option<FileMetadata>> {
1702+
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
17031703
let metadata =
17041704
if follow_symlink { std::fs::metadata(path) } else { std::fs::symlink_metadata(path) };
17051705

@@ -1709,9 +1709,9 @@ impl FileMetadata {
17091709
fn from_fd_num<'tcx>(
17101710
ecx: &mut MiriInterpCx<'tcx>,
17111711
fd_num: i32,
1712-
) -> InterpResult<'tcx, Option<FileMetadata>> {
1712+
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
17131713
let Some(fd) = ecx.machine.fds.get(fd_num) else {
1714-
return ecx.fd_not_found().map(|_: i32| None);
1714+
return Ok(Err(ecx.eval_libc("EBADF")));
17151715
};
17161716

17171717
let file = &fd
@@ -1731,12 +1731,11 @@ impl FileMetadata {
17311731
fn from_meta<'tcx>(
17321732
ecx: &mut MiriInterpCx<'tcx>,
17331733
metadata: Result<std::fs::Metadata, std::io::Error>,
1734-
) -> InterpResult<'tcx, Option<FileMetadata>> {
1734+
) -> InterpResult<'tcx, Result<FileMetadata, Scalar>> {
17351735
let metadata = match metadata {
17361736
Ok(metadata) => metadata,
17371737
Err(e) => {
1738-
ecx.set_last_error_from_io_error(e)?;
1739-
return Ok(None);
1738+
return Ok(Err(ecx.io_error_to_errnum(e)?));
17401739
}
17411740
};
17421741

@@ -1759,6 +1758,6 @@ impl FileMetadata {
17591758
let modified = extract_sec_and_nsec(metadata.modified())?;
17601759

17611760
// FIXME: Provide more fields using platform specific methods.
1762-
Ok(Some(FileMetadata { mode, size, created, accessed, modified }))
1761+
Ok(Ok(FileMetadata { mode, size, created, accessed, modified }))
17631762
}
17641763
}

0 commit comments

Comments
 (0)