fix(framebuffer): add proper bounds checking for accessing a pixel

This commit is contained in:
2025-10-24 19:54:28 -07:00
parent 6cd502ef18
commit 0c2303d789

View File

@@ -31,6 +31,11 @@ impl Framebuffer {
/// Mutably acquires a pixel of the framebuffer at the specified `x` and `y` coordinate. /// Mutably acquires a pixel of the framebuffer at the specified `x` and `y` coordinate.
pub fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> { pub fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
// Verify that the coordinates are within the bounds of the framebuffer.
if x >= self.width || y >= self.height {
return None;
}
// Calculate the index of the pixel safely, returning None if it overflows. // Calculate the index of the pixel safely, returning None if it overflows.
let index = y.checked_mul(self.width)?.checked_add(x)?; let index = y.checked_mul(self.width)?.checked_add(x)?;
// Return the pixel at the index. If the index is out of bounds, this will return None. // Return the pixel at the index. If the index is out of bounds, this will return None.