diff --git a/crates/test-programs/wasi-tests/src/bin/readlink.rs b/crates/test-programs/wasi-tests/src/bin/readlink.rs index 2257a89ddc..c9c128286e 100644 --- a/crates/test-programs/wasi-tests/src/bin/readlink.rs +++ b/crates/test-programs/wasi-tests/src/bin/readlink.rs @@ -10,7 +10,7 @@ unsafe fn test_readlink(dir_fd: wasi::Fd) { // Read link into the buffer let buf = &mut [0u8; 10]; - let mut bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len()) + let bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len()) .expect("readlink should succeed"); assert_eq!(bufused, 6, "should use 6 bytes of the buffer"); assert_eq!(&buf[..6], b"target", "buffer should contain 'target'"); @@ -22,10 +22,14 @@ unsafe fn test_readlink(dir_fd: wasi::Fd) { // Read link into smaller buffer than the actual link's length let buf = &mut [0u8; 4]; - bufused = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len()) - .expect("readlink should succeed"); - assert_eq!(bufused, 4, "should use all 4 bytes of the buffer"); - assert_eq!(buf, b"targ", "buffer should contain 'targ'"); + let err = wasi::path_readlink(dir_fd, "symlink", buf.as_mut_ptr(), buf.len()) + .err() + .expect("readlink with too-small buffer should fail"); + assert_eq!( + err.raw_error(), + wasi::ERRNO_RANGE, + "readlink with too-small buffer should give ERANGE" + ); // Clean up. wasi::path_unlink_file(dir_fd, "target").expect("removing a file"); diff --git a/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs b/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs deleted file mode 100644 index b8a677b2b5..0000000000 --- a/crates/test-programs/wasi-tests/src/bin/readlink_no_buffer.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::{env, process}; -use wasi_tests::open_scratch_directory; - -unsafe fn test_readlink_no_buffer(dir_fd: wasi::Fd) { - // First create a dangling symlink. - wasi::path_symlink("target", dir_fd, "symlink").expect("creating a symlink"); - - // Readlink it into a non-existent buffer. - let bufused = wasi::path_readlink(dir_fd, "symlink", (&mut []).as_mut_ptr(), 0) - .expect("readlink with a 0-sized buffer should succeed"); - assert_eq!( - bufused, 0, - "readlink with a 0-sized buffer should return 'bufused' 0" - ); - - // Clean up. - wasi::path_unlink_file(dir_fd, "symlink").expect("removing a file"); -} - -fn main() { - let mut args = env::args(); - let prog = args.next().unwrap(); - let arg = if let Some(arg) = args.next() { - arg - } else { - eprintln!("usage: {} ", prog); - process::exit(1); - }; - - // Open scratch directory - let dir_fd = match open_scratch_directory(&arg) { - Ok(dir_fd) => dir_fd, - Err(err) => { - eprintln!("{}", err); - process::exit(1) - } - }; - - // Run the tests. - unsafe { test_readlink_no_buffer(dir_fd) } -} diff --git a/crates/wasi-c2/TEST_FAILURES b/crates/wasi-c2/TEST_FAILURES index 0cbb7d6564..e0e0c014da 100644 --- a/crates/wasi-c2/TEST_FAILURES +++ b/crates/wasi-c2/TEST_FAILURES @@ -23,10 +23,6 @@ wasi_tests::path_symlink_trailing_slashes - unclear, path_symlink is giving a ENOTDIR when it expects an EEXIST... wasi_tests::poll_oneoff - no sched yet -wasi_tests::readlink - - insists on small buffer semantics for readlink that don't seem right -wasi_tests::readlink_no_buffer - - insists on small buffer semantics for readlink that don't seem right wasi_tests::remove_directory_trailing_slashes - apparently cap-std gives EINVAL when trying to remove dir with trailing slash? diff --git a/crates/wasi-c2/src/error.rs b/crates/wasi-c2/src/error.rs index 7a592f08ce..c68c520660 100644 --- a/crates/wasi-c2/src/error.rs +++ b/crates/wasi-c2/src/error.rs @@ -110,6 +110,9 @@ pub enum Error { /// Errno::Perm: Operation not permitted #[error("Perm: Operation not permitted")] Perm, + /// Errno::Range: Result too large + #[error("Range: Result too large")] + Range, /// Errno::Spipe: Invalid seek #[error("Spipe: Invalid seek")] Spipe, diff --git a/crates/wasi-c2/src/snapshots/preview_1.rs b/crates/wasi-c2/src/snapshots/preview_1.rs index f412af21e0..1013a56377 100644 --- a/crates/wasi-c2/src/snapshots/preview_1.rs +++ b/crates/wasi-c2/src/snapshots/preview_1.rs @@ -91,6 +91,7 @@ impl From for types::Errno { Error::Overflow => Errno::Overflow, Error::Pipe => Errno::Pipe, Error::Perm => Errno::Perm, + Error::Range => Errno::Range, Error::Spipe => Errno::Spipe, Error::FileNotCapable { .. } => Errno::Notcapable, Error::DirNotCapable { .. } => Errno::Notcapable, @@ -749,7 +750,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx { let link_bytes = link.as_bytes(); let link_len = link_bytes.len(); if link_len > buf_len as usize { - return Err(Error::Nametoolong); + return Err(Error::Range); } let mut buf = buf.as_array(link_len as u32).as_slice_mut()?; buf.copy_from_slice(link_bytes);