Skip to content

Commit 508a308

Browse files
committed
Use a fixed array size for checking the heaps
1 parent edaf118 commit 508a308

File tree

4 files changed

+26
-103
lines changed

4 files changed

+26
-103
lines changed

ttyd-tools/rel/include/global.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,11 @@ struct WarpByIndexStruct
621621
const char *EntranceList[32];
622622
};
623623

624-
struct CheckHeaps
625-
{
626-
const char *StandardHeapArray[16];
627-
const char *SmartHeapArray[16];
628-
};
629-
630624
extern Menus Menu[27];
631625
extern Cheats Cheat[20];
632626
extern bool Displays[9];
633627
extern char DisplayBuffer[256];
628+
extern char HeapBuffer[1024];
634629
extern MemoryWatchStruct MemoryWatch[60];
635630

636631
extern AutoIncrement AdjustableValueMenu;
@@ -651,7 +646,6 @@ extern OnScreenTimerDisplay OnScreenTimer;
651646
extern DisplayActionCommandTiming DisplayActionCommands;
652647
extern MemoryCardStruct MenuSettings;
653648
extern WarpByIndexStruct WarpByIndex;
654-
extern CheckHeaps CheckHeap;
655649

656650
extern const char *VersionNumber;
657651
extern uint8_t CheatsOrder[];

ttyd-tools/rel/source/draw.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,27 +3860,12 @@ void drawHeapArrayErrors()
38603860
int32_t PosY = 120;
38613861
float Scale = 0.6;
38623862

3863-
// Draw the standard heap text
3864-
const char **tempStandardHeapArray = CheckHeap.StandardHeapArray;
3865-
uint32_t Counter = 0;
3866-
3867-
while (tempStandardHeapArray[Counter])
3868-
{
3869-
drawText(tempStandardHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3870-
PosY -= 20;
3871-
Counter++;
3872-
}
3873-
3874-
// Draw the smart heap text
3875-
const char **tempSmartHeapArray = CheckHeap.SmartHeapArray;
3876-
Counter = 0;
3863+
// Draw the text
3864+
char *tempHeapBuffer = HeapBuffer;
3865+
drawText(tempHeapBuffer, PosX, PosY, Alpha, TextColor, Scale);
38773866

3878-
while (tempSmartHeapArray[Counter])
3879-
{
3880-
drawText(tempSmartHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3881-
PosY -= 20;
3882-
Counter++;
3883-
}
3867+
// Clear the heap buffer
3868+
clearMemory(tempHeapBuffer, sizeof(HeapBuffer));
38843869
}
38853870

38863871
void drawTitleScreenInfo()

ttyd-tools/rel/source/global.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ struct Menus Menu[27];
13211321
struct Cheats Cheat[20];
13221322
bool Displays[9];
13231323
char DisplayBuffer[256];
1324+
char HeapBuffer[1024];
13241325
struct MemoryWatchStruct MemoryWatch[60];
13251326

13261327
struct AutoIncrement AdjustableValueMenu;
@@ -1341,7 +1342,6 @@ struct OnScreenTimerDisplay OnScreenTimer;
13411342
struct DisplayActionCommandTiming DisplayActionCommands;
13421343
struct MemoryCardStruct MenuSettings;
13431344
struct WarpByIndexStruct WarpByIndex;
1344-
struct CheckHeaps CheckHeap;
13451345

13461346
bool HideMenu = false;
13471347
bool MenuIsDisplayed = false;

ttyd-tools/rel/source/main.cpp

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -382,68 +382,32 @@ uint32_t Mod::pauseArtAttackTimer()
382382
return mPFN_scissor_timer_main_trampoline();
383383
}
384384

385-
void clearHeapArray(const char **heap)
385+
void addTextToHeapArray(char *text)
386386
{
387-
uint32_t Counter = 0;
388-
while (heap[Counter])
389-
{
390-
delete[] heap[Counter];
391-
heap[Counter] = nullptr;
392-
Counter++;
393-
}
394-
}
395-
396-
void addTextToHeapArray(const char **heap, char *text)
397-
{
398-
// Get the next available slot in the array
399-
uint32_t TotalSlots = 16;
400-
int32_t FreeSlot = -1;
387+
char *tempHeapBuffer = HeapBuffer;
401388

402-
for (uint32_t i = 0; i < TotalSlots; i++)
403-
{
404-
if (!heap[i])
405-
{
406-
FreeSlot = i;
407-
break;
408-
}
409-
}
389+
// Make sure adding the new text will not result in an overflow
390+
uint32_t NewTextSize = ttyd::string::strlen(text);
391+
uint32_t CurrentHeapSize = ttyd::string::strlen(tempHeapBuffer);
410392

411-
// Make sure the new text can be added
412-
if (FreeSlot == -1)
393+
uint32_t NewHeapSize = CurrentHeapSize + NewTextSize + 1;
394+
uint32_t MaxHeapSize = sizeof(HeapBuffer);
395+
396+
if (NewHeapSize > MaxHeapSize)
413397
{
414-
// The new text cannot be added
398+
// Adding the new text will result in an overflow, so don't add it
415399
return;
416400
}
417401

418-
// Add the new text at the current free slot
419-
uint32_t TextSize = ttyd::string::strlen(text);
420-
char *NewText = new char[TextSize + 1];
421-
ttyd::string::strcpy(NewText, text);
422-
heap[FreeSlot] = NewText;
423-
}
424-
425-
bool checkIfTextAlreadyAdded(const char **heap, const char *text, uint32_t bytesToCompare)
426-
{
427-
uint32_t Counter = 0;
428-
while (heap[Counter])
429-
{
430-
if (compareStringsSize(heap[Counter], text, bytesToCompare))
431-
{
432-
return true;
433-
}
434-
Counter++;
435-
}
436-
return false;
402+
// Add the new text onto the heap
403+
ttyd::string::strcat(tempHeapBuffer, text);
437404
}
438405

439406
void checkHeaps()
440407
{
441-
const char **tempStandardHeapArray = CheckHeap.StandardHeapArray;
442-
const char **tempSmartHeapArray = CheckHeap.SmartHeapArray;
443408
char *tempDisplayBuffer = DisplayBuffer;
444409

445410
// Check the standard heaps
446-
bool ErrorsFound = false;
447411
for (int32_t i = 0; i < 5; i++)
448412
{
449413
const gc::os::HeapInfo &heap = gc::os::OSAlloc_HeapArray[i];
@@ -456,23 +420,20 @@ void checkHeaps()
456420
// Check pointer sanity
457421
if (!checkIfPointerIsValid(currentChunk))
458422
{
459-
ErrorsFound = true;
460423
valid = false;
461424
break;
462425
}
463426

464427
// Sanity check size
465428
if (currentChunk->size > 0x17FFFFF)
466429
{
467-
ErrorsFound = true;
468430
valid = false;
469431
break;
470432
}
471433

472434
// Check linked list integrity
473435
if (prevChunk != currentChunk->prev)
474436
{
475-
ErrorsFound = true;
476437
valid = false;
477438
break;
478439
}
@@ -483,24 +444,15 @@ void checkHeaps()
483444
if (!valid)
484445
{
485446
sprintf(tempDisplayBuffer,
486-
"Standard Heap %" PRId32 " corrupted at 0x%08" PRIX32,
447+
"Standard Heap %" PRId32 " corrupted at 0x%08" PRIX32 "\n",
487448
i,
488449
reinterpret_cast<uint32_t>(currentChunk));
489450

490-
// Only add the current heap once
491-
if (!checkIfTextAlreadyAdded(tempStandardHeapArray, tempDisplayBuffer, 16))
492-
{
493-
addTextToHeapArray(tempStandardHeapArray, tempDisplayBuffer);
494-
}
451+
// Add the text to the heap buffer
452+
addTextToHeapArray(tempDisplayBuffer);
495453
}
496454
}
497455

498-
if (!ErrorsFound)
499-
{
500-
// No errors were found, so clear the standard heap array
501-
clearHeapArray(tempStandardHeapArray);
502-
}
503-
504456
// Check the smart heap
505457
ttyd::memory::SmartWork *gpSmartWork = *reinterpret_cast<ttyd::memory::SmartWork **>(SmartWorkAddress);
506458
bool valid = true;
@@ -536,23 +488,15 @@ void checkHeaps()
536488
if (!valid)
537489
{
538490
sprintf(tempDisplayBuffer,
539-
"Smart Heap corrupted at 0x%08" PRIX32,
491+
"Smart Heap corrupted at 0x%08" PRIX32 "\n",
540492
reinterpret_cast<uint32_t>(currentChunk));
541493

542-
// Only add the current heap once
543-
if (!checkIfTextAlreadyAdded(tempSmartHeapArray, tempDisplayBuffer, 16))
544-
{
545-
addTextToHeapArray(tempSmartHeapArray, tempDisplayBuffer);
546-
}
547-
}
548-
else
549-
{
550-
// No errors were found, so clear the smart heap array
551-
clearHeapArray(tempSmartHeapArray);
494+
// Add the text to the heap buffer
495+
addTextToHeapArray(tempDisplayBuffer);
552496
}
553497

554498
// Draw any errors that occured
555-
if (tempStandardHeapArray[0] || tempSmartHeapArray[0])
499+
if (HeapBuffer[0] != '\0')
556500
{
557501
drawFunctionOnDebugLayer(drawHeapArrayErrors);
558502
}

0 commit comments

Comments
 (0)