Skip to content

Commit

Permalink
ioctl/mmap: do not panic if provided length is zero
Browse files Browse the repository at this point in the history
Return an error instead, as this is a public API which shouldn't panic
if provided an invalid input.
  • Loading branch information
Gnurou committed Sep 5, 2023
1 parent 6a01170 commit 0c7db64
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/src/ioctl/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,16 @@ impl Drop for PlaneMapping {

#[derive(Debug, Error)]
pub enum MmapError {
#[error("provided length was 0")]
ZeroLength,
#[error("ioctl error: {0}")]
IoctlError(#[from] Errno),
}

impl From<MmapError> for Errno {
fn from(err: MmapError) -> Self {
match err {
MmapError::ZeroLength => Errno::EINVAL,
MmapError::IoctlError(e) => e,
}
}
Expand All @@ -90,7 +93,7 @@ impl From<MmapError> for Errno {
// TODO should be unsafe because the mapping can be used after a buffer is queued?
// Or not, since this cannot cause a crash...
pub fn mmap(fd: &impl AsFd, mem_offset: u32, length: u32) -> Result<PlaneMapping, MmapError> {
let non_zero_length = NonZeroUsize::new(length as usize).unwrap();
let non_zero_length = NonZeroUsize::new(length as usize).ok_or(MmapError::ZeroLength)?;
let data = unsafe {
mman::mmap(
None,
Expand Down

0 comments on commit 0c7db64

Please sign in to comment.