From 904ab982e6b6df2ac469e67ce9d5266256ecbc7e Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 21 Mar 2020 00:15:35 +1000 Subject: [PATCH] GPU: Fix crash when fill rectangles are out-of-bounds --- src/core/gpu.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp index 80523ed07..8c3ab48ad 100644 --- a/src/core/gpu.cpp +++ b/src/core/gpu.cpp @@ -842,8 +842,27 @@ void GPU::ReadVRAM(u32 x, u32 y, u32 width, u32 height) {} void GPU::FillVRAM(u32 x, u32 y, u32 width, u32 height, u32 color) { const u16 color16 = RGBA8888ToRGBA5551(color); - for (u32 yoffs = 0; yoffs < height; yoffs++) - std::fill_n(&m_vram_ptr[((y + yoffs) * VRAM_WIDTH) + x], width, color16); + if ((x + width) <= VRAM_WIDTH) + { + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + const u32 row = (y + yoffs) % VRAM_HEIGHT; + std::fill_n(&m_vram_ptr[row * VRAM_WIDTH + x], width, color16); + } + } + else + { + for (u32 yoffs = 0; yoffs < height; yoffs++) + { + const u32 row = (y + yoffs) % VRAM_HEIGHT; + u16* row_ptr = &m_vram_ptr[row * VRAM_WIDTH]; + for (u32 xoffs = 0; xoffs < width; xoffs++) + { + const u32 col = (x + xoffs) % VRAM_WIDTH; + row_ptr[col] = color16; + } + } + } } void GPU::UpdateVRAM(u32 x, u32 y, u32 width, u32 height, const void* data)