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:
Pat Hickey
2023-04-18 11:12:30 -07:00
committed by GitHub
parent 2494867c5f
commit 24b607cf75
2 changed files with 9 additions and 9 deletions

View File

@@ -801,11 +801,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
.into_string()
.map_err(|_| Error::illegal_byte_sequence().context("link contents"))?;
let link_bytes = link.as_bytes();
let link_len = link_bytes.len();
if link_len > buf_len as usize {
return Err(Error::range());
}
buf.as_array(link_len as u32).copy_from_slice(link_bytes)?;
// Like posix readlink(2), silently truncate links when they are larger than the
// destination buffer:
let link_len = std::cmp::min(link_bytes.len(), buf_len as usize);
buf.as_array(link_len as u32)
.copy_from_slice(&link_bytes[..link_len])?;
Ok(link_len as types::Size)
}