From 9bf090b309e525da7f432b8a60ef7031e26917cd Mon Sep 17 00:00:00 2001 From: rogerman Date: Wed, 19 Jul 2023 12:22:06 -0700 Subject: [PATCH] Cheat System: Fix a bug where the underlying cheat data would get deallocated earlier than intended. (Regression from commit 0c90e8f.) - In CheatDBGame, also differentiate between actual entry size vs. working entry size. --- desmume/src/cheatSystem.cpp | 39 +++++++++++++++++++++---------------- desmume/src/cheatSystem.h | 13 ++++++++----- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/desmume/src/cheatSystem.cpp b/desmume/src/cheatSystem.cpp index ebf2ffe15..25072ab8e 100755 --- a/desmume/src/cheatSystem.cpp +++ b/desmume/src/cheatSystem.cpp @@ -1702,7 +1702,8 @@ CheatDBGame::CheatDBGame() _firstEntryOffset = 0; _encryptOffset = 0; - _dataSize = 0; + _rawDataSize = 0; + _workingDataSize = 0; _crc = 0; _entryCount = 0; @@ -1723,9 +1724,9 @@ CheatDBGame::CheatDBGame() _cheatItemCount = 0; } -CheatDBGame::CheatDBGame(const u32 encryptOffset, const FAT_R4 &fat, const u32 dataSize) +CheatDBGame::CheatDBGame(const u32 encryptOffset, const FAT_R4 &fat, const u32 rawDataSize) { - CheatDBGame::SetInitialProperties(dataSize, encryptOffset, fat); + CheatDBGame::SetInitialProperties(rawDataSize, encryptOffset, fat); _firstEntryOffset = 0; _entryCount = 0; @@ -1745,9 +1746,9 @@ CheatDBGame::CheatDBGame(const u32 encryptOffset, const FAT_R4 &fat, const u32 d _cheatItemCount = 0; } -CheatDBGame::CheatDBGame(FILE *fp, const bool isEncrypted, const u32 encryptOffset, const FAT_R4 &fat, const u32 dataSize, u8 (&workingBuffer)[1024]) +CheatDBGame::CheatDBGame(FILE *fp, const bool isEncrypted, const u32 encryptOffset, const FAT_R4 &fat, const u32 rawDataSize, u8 (&workingBuffer)[1024]) { - CheatDBGame::SetInitialProperties(dataSize, encryptOffset, fat); + CheatDBGame::SetInitialProperties(rawDataSize, encryptOffset, fat); CheatDBGame::LoadPropertiesFromFile(fp, isEncrypted, workingBuffer); _entryDataRawPtr = NULL; @@ -1774,9 +1775,10 @@ CheatDBGame::~CheatDBGame() } } -void CheatDBGame::SetInitialProperties(const u32 dataSize, const u32 encryptOffset, const FAT_R4 &fat) +void CheatDBGame::SetInitialProperties(const u32 rawDataSize, const u32 encryptOffset, const FAT_R4 &fat) { - _dataSize = dataSize; + _rawDataSize = rawDataSize; + _workingDataSize = rawDataSize + encryptOffset; _encryptOffset = encryptOffset; _baseOffset = (u32)fat.addr; _crc = fat.CRC; @@ -1789,7 +1791,7 @@ void CheatDBGame::SetInitialProperties(const u32 dataSize, const u32 encryptOffs void CheatDBGame::LoadPropertiesFromFile(FILE *fp, const bool isEncrypted, u8 (&workingBuffer)[1024]) { - const size_t gameDataBufferSize = (_dataSize < sizeof(workingBuffer)) ? _dataSize : sizeof(workingBuffer); + const size_t gameDataBufferSize = (_workingDataSize < sizeof(workingBuffer)) ? _workingDataSize : sizeof(workingBuffer); CheatDBFile::ReadToBuffer(fp, _baseOffset, isEncrypted, _encryptOffset, gameDataBufferSize, workingBuffer); const u8 *gameDataBuffer = workingBuffer + _encryptOffset; @@ -1817,9 +1819,14 @@ u32 CheatDBGame::GetEncryptOffset() const return this->_encryptOffset; } -u32 CheatDBGame::GetDataSize() const +u32 CheatDBGame::GetRawDataSize() const { - return this->_dataSize; + return this->_rawDataSize; +} + +u32 CheatDBGame::GetWorkingDataSize() const +{ + return this->_workingDataSize; } u32 CheatDBGame::GetCRC() const @@ -1900,10 +1907,10 @@ u8* CheatDBGame::LoadEntryData(FILE *fp, const bool isEncrypted) this->_entryData = NULL; } - this->_entryDataRawPtr = (u8 *)malloc(this->_dataSize + 8); - memset(this->_entryDataRawPtr, 0, this->_dataSize + 8); + this->_entryDataRawPtr = (u8 *)malloc(this->_workingDataSize + 8); + memset(this->_entryDataRawPtr, 0, this->_workingDataSize + 8); - bool didReadSuccessfully = CheatDBFile::ReadToBuffer(fp, this->_baseOffset, isEncrypted, this->_encryptOffset, this->_dataSize, this->_entryDataRawPtr); + bool didReadSuccessfully = CheatDBFile::ReadToBuffer(fp, this->_baseOffset, isEncrypted, this->_encryptOffset, this->_workingDataSize, this->_entryDataRawPtr); if (!didReadSuccessfully) { free(this->_entryDataRawPtr); @@ -2381,7 +2388,6 @@ u32 CheatDBFile::LoadGameList(const char *gameCode, const u32 gameDatabaseCRC, C if (this->_isEncrypted) { encryptOffset = (u32)(fatEntryCurrent.addr % 512); - dataSize += encryptOffset; } if (dataSize == 0) @@ -2444,9 +2450,8 @@ bool CHEATSEXPORT::load(const char *path) return didLoadSucceed; } - CheatDBGameList gameList; - this->_dbFile.LoadGameList(gameInfo.header.gameCode, gameInfo.crcForCheatsDb, gameList); - CheatDBGame *dbGamePtr = GetCheatDBGameEntryFromList(gameList, gameInfo.header.gameCode, gameInfo.crcForCheatsDb); + this->_dbFile.LoadGameList(gameInfo.header.gameCode, gameInfo.crcForCheatsDb, this->_tempGameList); + CheatDBGame *dbGamePtr = GetCheatDBGameEntryFromList(this->_tempGameList, gameInfo.header.gameCode, gameInfo.crcForCheatsDb); if (dbGamePtr == NULL) { diff --git a/desmume/src/cheatSystem.h b/desmume/src/cheatSystem.h index d73f0edf4..79ca61533 100755 --- a/desmume/src/cheatSystem.h +++ b/desmume/src/cheatSystem.h @@ -201,7 +201,8 @@ protected: u32 _firstEntryOffset; // This offset is relative to the file head. u32 _encryptOffset; // This offset is relative to the memory address of this->_entryDataRawPtr. - u32 _dataSize; + u32 _rawDataSize; + u32 _workingDataSize; u32 _crc; u32 _entryCount; @@ -219,17 +220,18 @@ protected: public: CheatDBGame(); - CheatDBGame(const u32 encryptOffset, const FAT_R4 &fat, const u32 dataSize); - CheatDBGame(FILE *fp, const bool isEncrypted, const u32 encryptOffset, const FAT_R4 &fat, const u32 dataSize, u8 (&workingBuffer)[1024]); + CheatDBGame(const u32 encryptOffset, const FAT_R4 &fat, const u32 rawDataSize); + CheatDBGame(FILE *fp, const bool isEncrypted, const u32 encryptOffset, const FAT_R4 &fat, const u32 rawDataSize, u8 (&workingBuffer)[1024]); ~CheatDBGame(); - void SetInitialProperties(const u32 dataSize, const u32 encryptOffset, const FAT_R4 &fat); + void SetInitialProperties(const u32 rawDataSize, const u32 encryptOffset, const FAT_R4 &fat); void LoadPropertiesFromFile(FILE *fp, const bool isEncrypted, u8 (&workingBuffer)[1024]); u32 GetBaseOffset() const; u32 GetFirstEntryOffset() const; u32 GetEncryptOffset() const; - u32 GetDataSize() const; + u32 GetRawDataSize() const; + u32 GetWorkingDataSize() const; u32 GetCRC() const; u32 GetEntryCount() const; u32 GetCheatItemCount() const; @@ -281,6 +283,7 @@ class CHEATSEXPORT { private: CheatDBFile _dbFile; + CheatDBGameList _tempGameList; CheatDBGame *_selectedDbGame; CHEATS_LIST *_cheats;