From 25986be0893f8b60b39e277b740f94e1cf4a7110 Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Fri, 14 Jul 2023 13:27:50 +0100 Subject: [PATCH] Prevent corrupted heaps causing infinite loops --- test/test_runner.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/test_runner.c b/test/test_runner.c index 5543fab04e49..d91ad02d1281 100644 --- a/test/test_runner.c +++ b/test/test_runner.c @@ -223,6 +223,15 @@ void CB2_TestRunner(void) const struct MemBlock *block = head; do { + if (block->magic != MALLOC_SYSTEM_ID + || !(EWRAM_START <= (uintptr_t)block->next && (uintptr_t)block->next < EWRAM_END) + || (block->next <= block && block->next != head)) + { + MgbaPrintf_("gHeap corrupted block at 0x%p", block); + gTestRunnerState.result = TEST_RESULT_ERROR; + break; + } + if (block->allocated) { const char *location = MemBlockLocation(block); @@ -495,6 +504,7 @@ static s32 MgbaVPrintf_(const char *fmt, va_list va) { s32 i = 0; s32 c, d; + u32 p; const char *s; while (*fmt) { @@ -528,6 +538,20 @@ static s32 MgbaVPrintf_(const char *fmt, va_list va) i = MgbaPutchar_(i, buffer[--n]); } break; + case 'p': + p = va_arg(va, unsigned); + { + s32 n; + for (n = 0; n < 7; n++) + { + unsigned nybble = (p >> (24 - (4*n))) & 0xF; + if (nybble <= 9) + i = MgbaPutchar_(i, '0' + nybble); + else + i = MgbaPutchar_(i, 'a' + nybble - 10); + } + } + break; case 'q': d = va_arg(va, int); {