Skip to content

Commit 6767087

Browse files
author
Mark McCaskey
committed
Always flush when writing, even if we get invalid pointers
1 parent 7d05d3c commit 6767087

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/wasi/src/syscalls/mod.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
3434
unsafe { state::get_wasi_state(&mut *(ctx as *const Ctx as *mut Ctx)) }
3535
}
3636

37-
fn write_bytes<T: Write>(
37+
fn write_bytes_inner<T: Write>(
3838
mut write_loc: T,
3939
memory: &Memory,
4040
iovs_arr_cell: &[Cell<__wasi_ciovec_t>],
@@ -44,19 +44,25 @@ fn write_bytes<T: Write>(
4444
let iov_inner = iov.get();
4545
let bytes = iov_inner.buf.deref(memory, 0, iov_inner.buf_len)?;
4646
write_loc
47-
.write(&bytes.iter().map(|b_cell| b_cell.get()).collect::<Vec<u8>>())
48-
.map_err(|_| {
49-
write_loc.flush();
50-
__WASI_EIO
51-
})?;
47+
.write_all(&bytes.iter().map(|b_cell| b_cell.get()).collect::<Vec<u8>>())
48+
.map_err(|_| __WASI_EIO)?;
5249

5350
// TODO: handle failure more accurately
5451
bytes_written += iov_inner.buf_len;
5552
}
56-
write_loc.flush();
5753
Ok(bytes_written)
5854
}
5955

56+
fn write_bytes<T: Write>(
57+
mut write_loc: T,
58+
memory: &Memory,
59+
iovs_arr_cell: &[Cell<__wasi_ciovec_t>],
60+
) -> Result<u32, __wasi_errno_t> {
61+
let result = write_bytes_inner(&mut write_loc, memory, iovs_arr_cell);
62+
write_loc.flush();
63+
result
64+
}
65+
6066
fn read_bytes<T: Read>(
6167
mut reader: T,
6268
memory: &Memory,

0 commit comments

Comments
 (0)