From 94975d7dbefde07e7012b99dfad05590de450169 Mon Sep 17 00:00:00 2001 From: harry Date: Fri, 12 Apr 2024 08:14:20 -0400 Subject: [PATCH] Fix code so that FCEUGI fully constructs properly and is not clobbered after the construction by a memset. Also, some minor code cleanup in related areas. --- src/fceu.cpp | 41 ++++++++++++++++++++++------------------- src/git.h | 33 +++++++++++++++++---------------- src/unif.cpp | 6 +++--- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/fceu.cpp b/src/fceu.cpp index 06bc5d604..cca8fd26f 100644 --- a/src/fceu.cpp +++ b/src/fceu.cpp @@ -121,23 +121,27 @@ static unsigned int pauseTimer = 0; FCEUGI::FCEUGI() - : filename(0), - archiveFilename(0) { - //printf("%08x",opsize); // WTF?! } FCEUGI::~FCEUGI() { + if (name) + { + free(name); + name = nullptr; + } + if (filename) { free(filename); - filename = NULL; + filename = nullptr; } + if (archiveFilename) { free(archiveFilename); - archiveFilename = NULL; + archiveFilename = nullptr; } } @@ -194,7 +198,7 @@ static void FCEU_CloseGame(void) if (GameInfo->name) { free(GameInfo->name); - GameInfo->name = NULL; + GameInfo->name = nullptr; } if (GameInfo->type != GIT_NSF) { @@ -224,7 +228,7 @@ static void FCEU_CloseGame(void) FCEU_CloseGenie(); delete GameInfo; - GameInfo = NULL; + GameInfo = nullptr; currFrameCounter = 0; @@ -243,7 +247,7 @@ static void FCEU_CloseGame(void) uint64 timestampbase; -FCEUGI *GameInfo = NULL; +FCEUGI *GameInfo = nullptr; void (*GameInterface)(GI h); void (*GameStateRestore)(int version); @@ -308,8 +312,8 @@ void FlushGenieRW(void) { } free(AReadG); free(BWriteG); - AReadG = NULL; - BWriteG = NULL; + AReadG = nullptr; + BWriteG = nullptr; RWWrap = 0; } } @@ -375,7 +379,7 @@ static void AllocBuffers() { static void FreeBuffers() { FCEU_free(RAM); - RAM = NULL; + RAM = nullptr; } //------ @@ -402,14 +406,14 @@ void ResetGameLoaded(void) { if (GameInfo) FCEU_CloseGame(); EmulationPaused = 0; //mbg 5/8/08 - loading games while paused was bad news. maybe this fixes it GameStateRestore = 0; - PPU_hook = NULL; - GameHBIRQHook = NULL; - FFCEUX_PPURead = NULL; - FFCEUX_PPUWrite = NULL; + PPU_hook = nullptr; + GameHBIRQHook = nullptr; + FFCEUX_PPURead = nullptr; + FFCEUX_PPUWrite = nullptr; if (GameExpSound.Kill) GameExpSound.Kill(); memset(&GameExpSound, 0, sizeof(GameExpSound)); - MapIRQHook = NULL; + MapIRQHook = nullptr; MMC5Hack = 0; PEC586Hack = 0; QTAIHack = 0; @@ -474,7 +478,6 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen FCEU_CloseGame(); GameInfo = new FCEUGI(); - memset( (void*)GameInfo, 0, sizeof(FCEUGI)); GameInfo->filename = strdup(fp->filename.c_str()); if (fp->archiveFilename != "") @@ -1344,7 +1347,7 @@ void UpdateAutosave(void) { FCEUSS_Save(f, false); AutoSS = true; //Flag that an auto-savestate was made free(f); - f = NULL; + f = nullptr; AutosaveStatus[AutosaveIndex] = 1; } } @@ -1358,7 +1361,7 @@ void FCEUI_RewindToLastAutosave(void) { f = strdup(FCEU_MakeFName(FCEUMKF_AUTOSTATE, AutosaveIndex, 0).c_str()); FCEUSS_Load(f); free(f); - f = NULL; + f = nullptr; //Set pointer to previous available slot if (AutosaveStatus[(AutosaveIndex + AutosaveQty - 1) % AutosaveQty] == 1) { diff --git a/src/git.h b/src/git.h index b324fc3f1..b02878cb0 100644 --- a/src/git.h +++ b/src/git.h @@ -153,27 +153,28 @@ struct FCEUGI FCEUGI(); ~FCEUGI(); - uint8 *name; //Game name, UTF8 encoding - int mappernum; - - EGIT type; - EGIV vidsys; //Current emulated video system; - ESI input[2]; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input. - ESIFC inputfc; //Desired Famicom expansion port device. -1 for unknown desired input. - ESIS cspecial; //Special cart expansion: DIP switches, barcode reader, etc. - EGIPPU vs_ppu; //PPU type for Vs. System - EGIVS vs_type; //Vs. System type - uint8 vs_cswitch; // Switch first and second controllers for Vs. System + uint8 *name = nullptr; //Game name, UTF8 encoding + int mappernum = 0; + + EGIT type = GIT_CART; + EGIV vidsys = GIV_USER; //Current emulated video system; + ESI input[2] = { SI_UNSET, SI_UNSET }; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input. + ESIFC inputfc = SIFC_UNSET; //Desired Famicom expansion port device. -1 for unknown desired input. + ESIS cspecial = SIS_NONE; //Special cart expansion: DIP switches, barcode reader, etc. + EGIPPU vs_ppu = GIPPU_USER; //PPU type for Vs. System + EGIVS vs_type = EGIVS_NORMAL; //Vs. System type + uint8 vs_cswitch = SIS_NONE; // Switch first and second controllers for Vs. System MD5DATA MD5; //mbg 6/8/08 - ??? - int soundrate; //For Ogg Vorbis expansion sound wacky support. 0 for default. - int soundchan; //Number of sound channels. + int soundrate = 0; //For Ogg Vorbis expansion sound wacky support. 0 for default. + int soundchan = 0; //Number of sound channels. - char* filename; - char* archiveFilename; - int archiveCount; + char* filename = nullptr; + char* archiveFilename = nullptr; + int archiveCount = 0; + bool loadedFromTmpFile = false; // Was loaded from temporary file, file most likely no longer exists }; #endif diff --git a/src/unif.cpp b/src/unif.cpp index e47a8c549..36702e3d0 100644 --- a/src/unif.cpp +++ b/src/unif.cpp @@ -161,9 +161,9 @@ static int NAME(FCEUFILE *fp) { namebuf[index] = 0; FCEU_printf("%s\n", namebuf); - if (!GameInfo->name) { - GameInfo->name = (uint8*)malloc(strlen(namebuf) + 1); //mbg merge 7/17/06 added cast - strcpy((char*)GameInfo->name, namebuf); //mbg merge 7/17/06 added cast + if (GameInfo->name == nullptr) + { + GameInfo->name = (uint8*)strdup(namebuf); } return(1); }