diff --git a/Source/Core/Core/HW/Memmap.cpp b/Source/Core/Core/HW/Memmap.cpp index e4012095d9..265b4c4f42 100644 --- a/Source/Core/Core/HW/Memmap.cpp +++ b/Source/Core/Core/HW/Memmap.cpp @@ -244,11 +244,18 @@ bool AreMemoryBreakpointsActivated() #endif } -static inline bool ValidCopyRange(u32 address, size_t size) +static inline u8* GetPointerForRange(u32 address, size_t size) { - return (GetPointer(address) != nullptr && - GetPointer(address + u32(size) - 1) != nullptr && - size < EXRAM_SIZE); // Make sure we don't have a range spanning 2 separate banks + // Make sure we don't have a range spanning 2 separate banks + if (size >= EXRAM_SIZE) + return nullptr; + + // Check that the beginning and end of the range are valid + u8* pointer = GetPointer(address); + if (!pointer || !GetPointer(address + u32(size) - 1)) + return nullptr; + + return pointer; } void CopyFromEmu(void* data, u32 address, size_t size) @@ -256,12 +263,13 @@ void CopyFromEmu(void* data, u32 address, size_t size) if (size == 0) return; - if (!ValidCopyRange(address, size)) + void* pointer = GetPointerForRange(address, size); + if (!pointer) { PanicAlert("Invalid range in CopyFromEmu. %zx bytes from 0x%08x", size, address); return; } - memcpy(data, GetPointer(address), size); + memcpy(data, pointer, size); } void CopyToEmu(u32 address, const void* data, size_t size) @@ -269,25 +277,27 @@ void CopyToEmu(u32 address, const void* data, size_t size) if (size == 0) return; - if (!ValidCopyRange(address, size)) + void* pointer = GetPointerForRange(address, size); + if (!pointer) { PanicAlert("Invalid range in CopyToEmu. %zx bytes to 0x%08x", size, address); return; } - memcpy(GetPointer(address), data, size); + memcpy(pointer, data, size); } -void Memset(const u32 _Address, const u8 _iValue, const u32 _iLength) +void Memset(u32 address, u8 value, size_t size) { - if (_iLength == 0) + if (size == 0) return; - if (!ValidCopyRange(_Address, _iLength)) + void* pointer = GetPointerForRange(address, size); + if (!pointer) { - PanicAlert("Invalid range in Memset. %zx bytes at 0x%08x", _iLength, _Address); + PanicAlert("Invalid range in Memset. %zx bytes at 0x%08x", size, address); return; } - memset(GetPointer(_Address), _iValue, _iLength); + memset(pointer, value, size); } std::string GetString(u32 em_address, size_t size) diff --git a/Source/Core/Core/HW/Memmap.h b/Source/Core/Core/HW/Memmap.h index af118a9878..69701814e7 100644 --- a/Source/Core/Core/HW/Memmap.h +++ b/Source/Core/Core/HW/Memmap.h @@ -80,7 +80,7 @@ std::string GetString(u32 em_address, size_t size = 0); u8* GetPointer(const u32 address); void CopyFromEmu(void* data, u32 address, size_t size); void CopyToEmu(u32 address, const void* data, size_t size); -void Memset(const u32 address, const u8 var, const u32 length); +void Memset(u32 address, u8 value, size_t size); u8 Read_U8(const u32 address); u16 Read_U16(const u32 address); u32 Read_U32(const u32 address);