From 36a0fa76a3f45969a7b8b6b66b8e36c03b2f9bd1 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 23 Dec 2022 10:51:37 -0800 Subject: [PATCH] Fix `fd_renumber` to handle output fds that aren't previously allocated. (#38) This fixes the crates/test-programs/wasi-tests/src/bin/stdio.rs test in the Wasmtime testsuite. --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index f105f667f916..28b399fbe96b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(())