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 path_open trailing slash edge case #4834

Merged
merged 1 commit into from
Jun 12, 2024
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: 6 additions & 0 deletions lib/wasix/src/syscalls/wasi/path_open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ pub(crate) fn path_open_internal(
if o_flags.contains(Oflags::DIRECTORY) {
return Ok(Err(Errno::Notdir));
}

// Trailing slash matters. But the underlying opener normalizes it away later.
if path.ends_with('/') {
return Ok(Err(Errno::Isdir));
}

// strip end file name

let (parent_inode, new_entity_name) =
Expand Down
20 changes: 20 additions & 0 deletions tests/wasi-fyi/fs_open_trailing_slash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ extern "C" {
}

const ERRNO_SUCCESS: i32 = 0;
const ERRNO_ISDIR: i32 = 31;
const ERRNO_NOTDIR: i32 = 54;
const OFLAGS_CREAT: i32 = 1;
const RIGHTS_FD_READ: i64 = 2;
const RIGHTS_FD_WRITE: i64 = 64;

fn main() {
unsafe {
let fd = 5;
let path_ok = "fyi/fs_open_trailing_slash.dir/file";
let path_bad = "fyi/fs_open_trailing_slash.dir/file/";
let path_bad_new_file = "fyi/fs_open_trailing_slash.dir/new-file/";
let errno = path_open(
fd,
0,
Expand Down Expand Up @@ -53,5 +57,21 @@ fn main() {
errno, ERRNO_NOTDIR,
"opening a regular file with a trailing slash should fail"
);

let errno = path_open(
fd,
0,
path_bad_new_file.as_ptr() as i32,
path_bad_new_file.len() as i32,
OFLAGS_CREAT,
RIGHTS_FD_READ | RIGHTS_FD_WRITE,
0,
0,
1024,
);
assert_eq!(
errno, ERRNO_ISDIR,
"creating a regular file with a trailing slash should fail"
);
}
}
Loading