Merge pull request #3291 from JosJuice/memset-error-check

Memmap: More error checking for Memset
This commit is contained in:
Scott Mansell 2015-11-28 22:38:24 +13:00
commit 861bd98e3a
2 changed files with 27 additions and 14 deletions

View File

@ -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,22 +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;
u8* ptr = GetPointer(_Address);
if (ptr != nullptr)
memset(ptr, _iValue, _iLength);
void* pointer = GetPointerForRange(address, size);
if (!pointer)
{
PanicAlert("Invalid range in Memset. %zx bytes at 0x%08x", size, address);
return;
}
memset(pointer, value, size);
}
std::string GetString(u32 em_address, size_t size)

View File

@ -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);