Skip to content

Commit 94975d7

Browse files
committed
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.
1 parent c97e2c9 commit 94975d7

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

src/fceu.cpp

+22-19
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,27 @@ static unsigned int pauseTimer = 0;
121121

122122

123123
FCEUGI::FCEUGI()
124-
: filename(0),
125-
archiveFilename(0)
126124
{
127-
//printf("%08x",opsize); // WTF?!
128125
}
129126

130127
FCEUGI::~FCEUGI()
131128
{
129+
if (name)
130+
{
131+
free(name);
132+
name = nullptr;
133+
}
134+
132135
if (filename)
133136
{
134137
free(filename);
135-
filename = NULL;
138+
filename = nullptr;
136139
}
140+
137141
if (archiveFilename)
138142
{
139143
free(archiveFilename);
140-
archiveFilename = NULL;
144+
archiveFilename = nullptr;
141145
}
142146
}
143147

@@ -194,7 +198,7 @@ static void FCEU_CloseGame(void)
194198

195199
if (GameInfo->name) {
196200
free(GameInfo->name);
197-
GameInfo->name = NULL;
201+
GameInfo->name = nullptr;
198202
}
199203

200204
if (GameInfo->type != GIT_NSF) {
@@ -224,7 +228,7 @@ static void FCEU_CloseGame(void)
224228
FCEU_CloseGenie();
225229

226230
delete GameInfo;
227-
GameInfo = NULL;
231+
GameInfo = nullptr;
228232

229233
currFrameCounter = 0;
230234

@@ -243,7 +247,7 @@ static void FCEU_CloseGame(void)
243247
uint64 timestampbase;
244248

245249

246-
FCEUGI *GameInfo = NULL;
250+
FCEUGI *GameInfo = nullptr;
247251

248252
void (*GameInterface)(GI h);
249253
void (*GameStateRestore)(int version);
@@ -308,8 +312,8 @@ void FlushGenieRW(void) {
308312
}
309313
free(AReadG);
310314
free(BWriteG);
311-
AReadG = NULL;
312-
BWriteG = NULL;
315+
AReadG = nullptr;
316+
BWriteG = nullptr;
313317
RWWrap = 0;
314318
}
315319
}
@@ -375,7 +379,7 @@ static void AllocBuffers() {
375379

376380
static void FreeBuffers() {
377381
FCEU_free(RAM);
378-
RAM = NULL;
382+
RAM = nullptr;
379383
}
380384
//------
381385

@@ -402,14 +406,14 @@ void ResetGameLoaded(void) {
402406
if (GameInfo) FCEU_CloseGame();
403407
EmulationPaused = 0; //mbg 5/8/08 - loading games while paused was bad news. maybe this fixes it
404408
GameStateRestore = 0;
405-
PPU_hook = NULL;
406-
GameHBIRQHook = NULL;
407-
FFCEUX_PPURead = NULL;
408-
FFCEUX_PPUWrite = NULL;
409+
PPU_hook = nullptr;
410+
GameHBIRQHook = nullptr;
411+
FFCEUX_PPURead = nullptr;
412+
FFCEUX_PPUWrite = nullptr;
409413
if (GameExpSound.Kill)
410414
GameExpSound.Kill();
411415
memset(&GameExpSound, 0, sizeof(GameExpSound));
412-
MapIRQHook = NULL;
416+
MapIRQHook = nullptr;
413417
MMC5Hack = 0;
414418
PEC586Hack = 0;
415419
QTAIHack = 0;
@@ -474,7 +478,6 @@ FCEUGI *FCEUI_LoadGameVirtual(const char *name, int OverwriteVidMode, bool silen
474478

475479
FCEU_CloseGame();
476480
GameInfo = new FCEUGI();
477-
memset( (void*)GameInfo, 0, sizeof(FCEUGI));
478481

479482
GameInfo->filename = strdup(fp->filename.c_str());
480483
if (fp->archiveFilename != "")
@@ -1344,7 +1347,7 @@ void UpdateAutosave(void) {
13441347
FCEUSS_Save(f, false);
13451348
AutoSS = true; //Flag that an auto-savestate was made
13461349
free(f);
1347-
f = NULL;
1350+
f = nullptr;
13481351
AutosaveStatus[AutosaveIndex] = 1;
13491352
}
13501353
}
@@ -1358,7 +1361,7 @@ void FCEUI_RewindToLastAutosave(void) {
13581361
f = strdup(FCEU_MakeFName(FCEUMKF_AUTOSTATE, AutosaveIndex, 0).c_str());
13591362
FCEUSS_Load(f);
13601363
free(f);
1361-
f = NULL;
1364+
f = nullptr;
13621365

13631366
//Set pointer to previous available slot
13641367
if (AutosaveStatus[(AutosaveIndex + AutosaveQty - 1) % AutosaveQty] == 1) {

src/git.h

+17-16
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,28 @@ struct FCEUGI
153153
FCEUGI();
154154
~FCEUGI();
155155

156-
uint8 *name; //Game name, UTF8 encoding
157-
int mappernum;
158-
159-
EGIT type;
160-
EGIV vidsys; //Current emulated video system;
161-
ESI input[2]; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input.
162-
ESIFC inputfc; //Desired Famicom expansion port device. -1 for unknown desired input.
163-
ESIS cspecial; //Special cart expansion: DIP switches, barcode reader, etc.
164-
EGIPPU vs_ppu; //PPU type for Vs. System
165-
EGIVS vs_type; //Vs. System type
166-
uint8 vs_cswitch; // Switch first and second controllers for Vs. System
156+
uint8 *name = nullptr; //Game name, UTF8 encoding
157+
int mappernum = 0;
158+
159+
EGIT type = GIT_CART;
160+
EGIV vidsys = GIV_USER; //Current emulated video system;
161+
ESI input[2] = { SI_UNSET, SI_UNSET }; //Desired input for emulated input ports 1 and 2; -1 for unknown desired input.
162+
ESIFC inputfc = SIFC_UNSET; //Desired Famicom expansion port device. -1 for unknown desired input.
163+
ESIS cspecial = SIS_NONE; //Special cart expansion: DIP switches, barcode reader, etc.
164+
EGIPPU vs_ppu = GIPPU_USER; //PPU type for Vs. System
165+
EGIVS vs_type = EGIVS_NORMAL; //Vs. System type
166+
uint8 vs_cswitch = SIS_NONE; // Switch first and second controllers for Vs. System
167167

168168
MD5DATA MD5;
169169

170170
//mbg 6/8/08 - ???
171-
int soundrate; //For Ogg Vorbis expansion sound wacky support. 0 for default.
172-
int soundchan; //Number of sound channels.
171+
int soundrate = 0; //For Ogg Vorbis expansion sound wacky support. 0 for default.
172+
int soundchan = 0; //Number of sound channels.
173173

174-
char* filename;
175-
char* archiveFilename;
176-
int archiveCount;
174+
char* filename = nullptr;
175+
char* archiveFilename = nullptr;
176+
int archiveCount = 0;
177+
bool loadedFromTmpFile = false; // Was loaded from temporary file, file most likely no longer exists
177178
};
178179

179180
#endif

src/unif.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ static int NAME(FCEUFILE *fp) {
161161
namebuf[index] = 0;
162162
FCEU_printf("%s\n", namebuf);
163163

164-
if (!GameInfo->name) {
165-
GameInfo->name = (uint8*)malloc(strlen(namebuf) + 1); //mbg merge 7/17/06 added cast
166-
strcpy((char*)GameInfo->name, namebuf); //mbg merge 7/17/06 added cast
164+
if (GameInfo->name == nullptr)
165+
{
166+
GameInfo->name = (uint8*)strdup(namebuf);
167167
}
168168
return(1);
169169
}

0 commit comments

Comments
 (0)