wasi-common: change behavior of path_readlink to truncate on too-small buffers (#6225)
this is the same behavior as exists in posix readlink(2)
This commit is contained in:
@@ -22,10 +22,10 @@ unsafe fn test_readlink(dir_fd: wasi::Fd) {
|
|||||||
|
|
||||||
// Read link into smaller buffer than the actual link's length
|
// Read link into smaller buffer than the actual link's length
|
||||||
let buf = &mut [0u8; 4];
|
let buf = &mut [0u8; 4];
|
||||||
let err = 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())
|
||||||
.err()
|
.expect("readlink with too-small buffer should silently truncate");
|
||||||
.expect("readlink with too-small buffer should fail");
|
assert_eq!(bufused, 4);
|
||||||
assert_errno!(err, wasi::ERRNO_RANGE);
|
assert_eq!(buf, b"targ");
|
||||||
|
|
||||||
// Clean up.
|
// Clean up.
|
||||||
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
|
wasi::path_unlink_file(dir_fd, "target").expect("removing a file");
|
||||||
|
|||||||
@@ -801,11 +801,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
|||||||
.into_string()
|
.into_string()
|
||||||
.map_err(|_| Error::illegal_byte_sequence().context("link contents"))?;
|
.map_err(|_| Error::illegal_byte_sequence().context("link contents"))?;
|
||||||
let link_bytes = link.as_bytes();
|
let link_bytes = link.as_bytes();
|
||||||
let link_len = link_bytes.len();
|
// Like posix readlink(2), silently truncate links when they are larger than the
|
||||||
if link_len > buf_len as usize {
|
// destination buffer:
|
||||||
return Err(Error::range());
|
let link_len = std::cmp::min(link_bytes.len(), buf_len as usize);
|
||||||
}
|
buf.as_array(link_len as u32)
|
||||||
buf.as_array(link_len as u32).copy_from_slice(link_bytes)?;
|
.copy_from_slice(&link_bytes[..link_len])?;
|
||||||
Ok(link_len as types::Size)
|
Ok(link_len as types::Size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user