From 9b92e2e68ec8f0f66f16f61d8076a7444f97707b Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 20 Oct 2023 18:41:47 -0500 Subject: [PATCH] Assert that `sf::Image::{set|get}Pixel` coordinates are within bounds Yay catching more UB :) --- src/SFML/Graphics/Image.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index a5c777934..a6167235f 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -35,6 +35,7 @@ #include #include +#include #include @@ -263,7 +264,9 @@ void Image::createMaskFromColor(const Color& color, std::uint8_t alpha) //////////////////////////////////////////////////////////// void Image::setPixel(const Vector2u& coords, const Color& color) { - std::uint8_t* pixel = &m_pixels[(coords.x + coords.y * m_size.x) * 4]; + const auto index = (coords.x + coords.y * m_size.x) * 4; + assert(index < m_pixels.size() && "Image::setPixel() cannot access out of bounds pixel"); + std::uint8_t* pixel = &m_pixels[index]; *pixel++ = color.r; *pixel++ = color.g; *pixel++ = color.b; @@ -274,7 +277,9 @@ void Image::setPixel(const Vector2u& coords, const Color& color) //////////////////////////////////////////////////////////// Color Image::getPixel(const Vector2u& coords) const { - const std::uint8_t* pixel = &m_pixels[(coords.x + coords.y * m_size.x) * 4]; + const auto index = (coords.x + coords.y * m_size.x) * 4; + assert(index < m_pixels.size() && "Image::getPixel() cannot access out of bounds pixel"); + const std::uint8_t* pixel = &m_pixels[index]; return {pixel[0], pixel[1], pixel[2], pixel[3]}; }