From 25c0e7c94815f2c96201a0b0b4c08b772ee3369a Mon Sep 17 00:00:00 2001 From: Alcaro Date: Wed, 14 Aug 2013 10:05:15 +0200 Subject: [PATCH] Fix infinite loop when given bad ROMs (one byte files, for example); fix compile error that somehow snuck in. --- libretro/libretro.cpp | 3 +-- memmap.cpp | 14 +++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index aa7b3c6a..50ed2ed2 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -233,7 +233,7 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code) if (S9xGameGenieToRaw(code, address, val)!=NULL && S9xProActionReplayToRaw(code, address, val)!=NULL && - S9xGoldFingerToRaw(code, address, &sram, val, bytes)!=NULL) + S9xGoldFingerToRaw(code, address, sram, val, bytes)!=NULL) { // bad code, ignore return; } @@ -252,7 +252,6 @@ void retro_cheat_set(unsigned index, bool enabled, const char *code) bool retro_load_game(const struct retro_game_info *game) { - if(game->data == NULL && game->size == 0 && game->path != NULL) rom_loaded = Memory.LoadROM(game->path); else diff --git a/memmap.cpp b/memmap.cpp index 7af40629..03ceaba6 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -1519,10 +1519,12 @@ bool8 CMemory::LoadROMMem (const uint8 *source, uint32 sourceSize) strcpy(ROMFilename,"MemoryROM"); + int counter=0; do { + if (++counter>5) return FALSE;//if we keep failing, just give up after a while or we'll loop forever. memset(ROM,0, MAX_ROM_SIZE); - memset(&Multi, 0,sizeof(Multi)); + memset(&Multi, 0,sizeof(Multi)); memcpy(ROM,source,sourceSize); } while(!LoadROMInt(sourceSize)); @@ -1537,17 +1539,19 @@ bool8 CMemory::LoadROM (const char *filename) int32 totalFileSize; + int counter=0; do { + if (++counter>5) return FALSE; memset(ROM,0, MAX_ROM_SIZE); - memset(&Multi, 0,sizeof(Multi)); + memset(&Multi, 0,sizeof(Multi)); totalFileSize = FileLoader(ROM, filename, MAX_ROM_SIZE); if (!totalFileSize) - return (FALSE); + return (FALSE); if (!Settings.NoPatch) - CheckForAnyPatch(filename, HeaderCount != 0, totalFileSize); + CheckForAnyPatch(filename, HeaderCount != 0, totalFileSize); } while(!LoadROMInt(totalFileSize)); @@ -3375,7 +3379,7 @@ uint16 CMemory::checksum_calc_sum (uint8 *data, uint32 length) uint16 CMemory::checksum_mirror_sum (uint8 *start, uint32 &length, uint32 mask) { // from NSRT - while (!(length & mask)) + while (!(length & mask) && mask) mask >>= 1; uint16 part1 = checksum_calc_sum(start, mask);