Skip to content

Commit

Permalink
Fix fd_renumber to handle output fds that aren't previously allocat…
Browse files Browse the repository at this point in the history
…ed. (bytecodealliance#38)

This fixes the crates/test-programs/wasi-tests/src/bin/stdio.rs test in
the Wasmtime testsuite.
  • Loading branch information
sunfishcode committed Dec 23, 2022
1 parent e20ab42 commit 36a0fa7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,18 @@ pub unsafe extern "C" fn fd_renumber(fd: Fd, to: Fd) -> Errno {
State::with_mut(|state| {
let closed = state.closed;

// Ensure the table is big enough to contain `to`. Do this before
// looking up `fd` as it can fail due to `NOMEM`.
while Fd::from(state.ndescriptors) <= to {
let old_closed = state.closed;
let new_closed = state.push_desc(Descriptor::Closed(old_closed))?;
state.closed = Some(new_closed);
}

let fd_desc = state.get_mut(fd)?;
let desc = replace(fd_desc, Descriptor::Closed(closed));

let to_desc = state.get_mut(to)?;
let to_desc = unwrap_result(state.get_mut(to));
*to_desc = desc;
state.closed = Some(fd);
Ok(())
Expand Down

0 comments on commit 36a0fa7

Please sign in to comment.