Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fd_readdir to properly truncate directory entry names. #2620

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/test-programs/wasi-tests/src/bin/fd_readdir.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use more_asserts::assert_gt;
use std::{cmp::min, env, mem, process, slice, str};
use std::{env, mem, process, slice, str};
use wasi_tests::open_scratch_directory;

const BUF_LEN: usize = 256;
Expand Down Expand Up @@ -59,7 +59,9 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
let bufused =
wasi::fd_readdir(fd, buf.as_mut_ptr(), BUF_LEN, cookie).expect("failed fd_readdir");

let sl = slice::from_raw_parts(buf.as_ptr(), min(BUF_LEN, bufused));
assert!(bufused <= BUF_LEN);

let sl = slice::from_raw_parts(buf.as_ptr(), bufused);
let dirs: Vec<_> = ReadDir::from_slice(sl).collect();
let eof = bufused < BUF_LEN;
(dirs, eof)
Expand Down
5 changes: 2 additions & 3 deletions crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
let dirent_len: types::Size = dirent_raw.len().try_into()?;
let name_raw = name.as_bytes();
let name_len = name_raw.len().try_into()?;
let offset = dirent_len.checked_add(name_len).ok_or(Error::Overflow)?;

// Copy as many bytes of the dirent as we can, up to the end of the buffer.
let dirent_copy_len = min(dirent_len, buf_len - bufused);
Expand All @@ -318,6 +317,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
}

buf = buf.add(dirent_copy_len)?;
bufused += dirent_copy_len;

// Copy as many bytes of the name as we can, up to the end of the buffer.
let name_copy_len = min(name_len, buf_len - bufused);
Expand All @@ -331,8 +331,7 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
}

buf = buf.add(name_copy_len)?;

bufused += offset;
bufused += name_copy_len;
}

Ok(bufused)
Expand Down