Skip to content

Commit a6a8f09

Browse files
committed
Address review comments
1 parent 962a740 commit a6a8f09

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(rustc_private)]
22
#![feature(option_expect_none, option_unwrap_none)]
3+
#![feature(map_first_last)]
34
#![warn(rust_2018_idioms)]
45
#![allow(clippy::cast_lossless)]
56

src/shims/fs.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ impl FileHandler {
3333

3434
fn insert_fd_with_min_fd(&mut self, file_handle: FileHandle, min_fd: i32) -> i32 {
3535
let min_fd = std::cmp::max(min_fd, 3);
36+
37+
// Find the lowest unused FD, starting from min_fd. If the first such unused FD is in
38+
// between used FDs, the find_map combinator will return it. If the first such unused FD
39+
// is after all other used FDs, the find_map combinator will return None, and we will use
40+
// the FD following the greatest FD thus far.
3641
let candidate_new_fd = self
3742
.handles
3843
.range(min_fd..)
@@ -50,8 +55,9 @@ impl FileHandler {
5055
let new_fd = candidate_new_fd.unwrap_or_else(|| {
5156
// find_map ran out of BTreeMap entries before finding a free fd, use one plus the
5257
// maximum fd in the map
53-
self.handles.keys().rev().next().map(|last_fd| last_fd + 1).unwrap_or(min_fd)
58+
self.handles.last_entry().map(|entry| entry.key() + 1).unwrap_or(min_fd)
5459
});
60+
5561
self.handles.insert(new_fd, file_handle).unwrap_none();
5662
new_fd
5763
}
@@ -153,7 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
153159
&mut self,
154160
fd_op: OpTy<'tcx, Tag>,
155161
cmd_op: OpTy<'tcx, Tag>,
156-
arg_op: Option<OpTy<'tcx, Tag>>,
162+
start_op: Option<OpTy<'tcx, Tag>>,
157163
) -> InterpResult<'tcx, i32> {
158164
let this = self.eval_context_mut();
159165

@@ -179,20 +185,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
179185
// because exec() isn't supported. The F_DUPFD and F_DUPFD_CLOEXEC commands only
180186
// differ in whether the FD_CLOEXEC flag is pre-set on the new file descriptor,
181187
// thus they can share the same implementation here.
182-
let arg_op = arg_op.ok_or_else(|| {
188+
let start_op = start_op.ok_or_else(|| {
183189
err_unsup_format!(
184190
"fcntl with command F_DUPFD or F_DUPFD_CLOEXEC requires a third argument"
185191
)
186192
})?;
187-
let arg = this.read_scalar(arg_op)?.to_i32()?;
193+
let start = this.read_scalar(start_op)?.to_i32()?;
188194
let fh = &mut this.machine.file_handler;
189195
let (file_result, writable) = match fh.handles.get(&fd) {
190196
Some(FileHandle::File { file, writable }) => (file.try_clone(), *writable),
191197
Some(_) => throw_unsup_format!("Duplicating file descriptors for stdin, stdout, or stderr is not supported"),
192198
None => return this.handle_not_found(),
193199
};
194200
let fd_result = file_result.map(|duplicated| {
195-
fh.insert_fd_with_min_fd(FileHandle::File { file: duplicated, writable }, arg)
201+
fh.insert_fd_with_min_fd(FileHandle::File { file: duplicated, writable }, start)
196202
});
197203
this.try_unwrap_io_result(fd_result)
198204
} else {

0 commit comments

Comments
 (0)