From d2f7ed3602be867051639dcee2c660a1879d0b97 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 9 Aug 2026 19:42:59 +0200 Subject: [PATCH] Common: Fix LazyMemoryRegion::EnsureMemoryPagesWritable If (offset + size) % BLOCK_SIZE was smaller than offset % BLOCK_SIZE, the loop terminated one iteration too early, potentially leaving a block read-only. None of the existing users of LazyMemoryRegion could trigger this bug, because they never made EnsureMemoryPagesWritable calls that straddled across two blocks, but the next commit will add a new use that does. --- Source/Core/Common/MemArena.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/MemArena.h b/Source/Core/Common/MemArena.h index 1a3093cc4d..654b13b9a1 100644 --- a/Source/Core/Common/MemArena.h +++ b/Source/Core/Common/MemArena.h @@ -11,6 +11,7 @@ #include #endif +#include "Common/Align.h" #include "Common/CommonTypes.h" #include "Common/DynamicLibrary.h" @@ -207,8 +208,9 @@ public: void EnsureMemoryPagesWritable(size_t offset, size_t size) { #ifdef _WIN32 - for (const auto end_offset = offset + size; offset < end_offset; offset += BLOCK_SIZE) - EnsureMemoryPageWritable(offset); + const size_t end_offset = offset + size; + for (size_t i = Common::AlignDown(offset, BLOCK_SIZE); i < end_offset; i += BLOCK_SIZE) + EnsureMemoryPageWritable(i); #endif }