diff --git a/core/embed/rust/src/ui/shape/bitmap.rs b/core/embed/rust/src/ui/shape/bitmap.rs index c9a7765812..031830d981 100644 --- a/core/embed/rust/src/ui/shape/bitmap.rs +++ b/core/embed/rust/src/ui/shape/bitmap.rs @@ -173,22 +173,27 @@ impl<'a> Bitmap<'a> { /// /// Returns None if row is out of range. pub fn row(&self, row: i16) -> Option<&[T]> { - if row >= 0 && row < self.size.y { - let offset = row as usize * (self.stride / core::mem::size_of::()); - if offset % core::mem::align_of::() == 0 { - self.wait_for_dma(); - // SAFETY: - // The resulting slice is inside the bitmap and properly aligned. - // Potential DMA operation is finished. - return Some(unsafe { - core::slice::from_raw_parts( - (self.ptr as *const T).add(offset), - self.stride / core::mem::size_of::(), - ) - }); - } + if row < 0 || row >= self.size.y { + return None; } - None + + let offset = row as usize * (self.stride / core::mem::size_of::()); + + if offset % core::mem::align_of::() != 0 { + return None; + } + + self.wait_for_dma(); + + // SAFETY: + // The resulting slice is inside the bitmap and properly aligned. + // Potential DMA operation is finished. + Some(unsafe { + core::slice::from_raw_parts( + (self.ptr as *const T).add(offset), + self.stride / core::mem::size_of::(), + ) + }) } /// Returns the specified row as a mutable slice. @@ -196,23 +201,32 @@ impl<'a> Bitmap<'a> { /// Returns None if row is out of range or /// the bitmap is not set as mutable. pub fn row_mut(&mut self, row: i16) -> Option<&mut [T]> { - if self.mutable && row >= 0 && row < self.size.y { - let offset = row as usize * (self.stride / core::mem::size_of::()); - if offset % core::mem::align_of::() == 0 { - self.wait_for_dma(); - // SAFETY: - // The bitmap is mutable. - // The resulting slice is inside the bitmap and properly aligned. - // Potential DMA operation is finished. - return Some(unsafe { - core::slice::from_raw_parts_mut( - (self.ptr as *mut T).add(offset), - self.stride / core::mem::size_of::(), - ) - }); - } + if !self.mutable { + return None; } - None + + if row < 0 || row >= self.size.y { + return None; + } + + let offset = row as usize * (self.stride / core::mem::size_of::()); + + if offset % core::mem::align_of::() != 0 { + return None; + } + + self.wait_for_dma(); + + // SAFETY: + // The bitmap is mutable. + // The resulting slice is inside the bitmap and properly aligned. + // Potential DMA operation is finished. + Some(unsafe { + core::slice::from_raw_parts_mut( + (self.ptr as *mut T).add(offset), + self.stride / core::mem::size_of::(), + ) + }) } /// Returns specified consecutive rows as a mutable slice @@ -220,31 +234,36 @@ impl<'a> Bitmap<'a> { /// Returns None if any of requested row is out of range or /// the bitmap is not set as mutable. pub fn rows_mut(&mut self, row: i16, height: i16) -> Option<&mut [T]> { - if self.mutable - && row >= 0 - && height > 0 - && row < self.size.y - && row + height <= self.size.y - { - let offset = self.stride * row as usize; - if offset % core::mem::align_of::() == 0 { - let len = self.stride * height as usize; - self.wait_for_dma(); - // SAFETY: - // The bitmap is mutable. - // The resulting slice is inside the bitmap and properly aligned. - // Potential DMA operation is finished. - let array = unsafe { - core::slice::from_raw_parts_mut( - self.ptr as *mut T, - self.size.y as usize * self.stride / core::mem::size_of::(), - ) - }; - - return Some(&mut array[offset..offset + len]); - } + if !self.mutable { + return None; } - None + + if row < 0 || height <= 0 || row + height > self.size.y { + return None; + } + + let offset = self.stride * row as usize; + + if offset % core::mem::align_of::() != 0 { + return None; + } + + self.wait_for_dma(); + + // SAFETY: + // The bitmap is mutable. + // The resulting slice is inside the bitmap and properly aligned. + // Potential DMA operation is finished. + let array = unsafe { + core::slice::from_raw_parts_mut( + self.ptr as *mut T, + self.size.y as usize * self.stride / core::mem::size_of::(), + ) + }; + + let len = self.stride * height as usize; + + Some(&mut array[offset..offset + len]) } /// Return raw mut pointer to the specified bitmap row.