abi: proper handling for empty slice arguments

This commit is contained in:
Mark Poliakov 2024-12-20 13:41:31 +02:00
parent dfba752ddf
commit 55e6dae194

View File

@ -17,6 +17,11 @@ pub fn ref_mut<'a, T: Sized>(addr: usize) -> Result<&'a mut T, Error> {
/// Validates a &str passed as base address and length
pub fn str_ref<'a>(base: usize, len: usize) -> Result<&'a str, Error> {
if len == 0 {
// Ignore the base
return Ok("");
}
let slice = slice_ref(base, len)?;
if slice.contains(&0) {
log::warn!("User-supplied string contains NUL characters");
@ -27,12 +32,22 @@ pub fn str_ref<'a>(base: usize, len: usize) -> Result<&'a str, Error> {
/// Validates a &[T] passed as base address and element count
pub fn slice_ref<'a, T: Sized>(base: usize, count: usize) -> Result<&'a [T], Error> {
if count == 0 {
// Ignore the base
return Ok(&[]);
}
let proc = Thread::current();
let ptr = base as *const T;
unsafe { ptr.validate_user_slice(count, proc.address_space()) }
}
/// Validates a &mut [T] passed as base address and element count
pub fn slice_mut<'a, T: Sized>(base: usize, count: usize) -> Result<&'a mut [T], Error> {
if count == 0 {
// Ignore the base
return Ok(&mut []);
}
let proc = Thread::current();
let ptr = base as *mut T;
unsafe { ptr.validate_user_slice_mut(count, proc.address_space()) }