Skip to content

Commit a2a19ae

Browse files
authored
Merge pull request #730 from AZero13/xcode
Free expression if getline fails
2 parents 00f08bf + ce89184 commit a2a19ae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Core/cheats.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,16 +289,20 @@ int GB_load_cheats(GB_gameboy_t *gb, const char *path, bool replace_existing)
289289

290290
uint32_t magic = 0;
291291
uint32_t struct_size = 0;
292-
fread(&magic, sizeof(magic), 1, f);
293-
fread(&struct_size, sizeof(struct_size), 1, f);
292+
if (fread(&magic, sizeof(magic), 1, f) != 1) {
293+
goto error;
294+
}
295+
if (fread(&struct_size, sizeof(struct_size), 1, f) != 1) {
296+
goto error;
297+
}
294298
if (magic != LE32(CHEAT_MAGIC) && magic != BE32(CHEAT_MAGIC)) {
295299
GB_log(gb, "The file is not a SameBoy cheat database");
296-
return -1;
300+
goto error;
297301
}
298302

299303
if (struct_size != sizeof(GB_cheat_t)) {
300304
GB_log(gb, "This cheat database is not compatible with this version of SameBoy");
301-
return -1;
305+
goto error;
302306
}
303307

304308
// Remove all cheats first
@@ -307,7 +311,7 @@ int GB_load_cheats(GB_gameboy_t *gb, const char *path, bool replace_existing)
307311
}
308312

309313
GB_cheat_t cheat;
310-
while (fread(&cheat, sizeof(cheat), 1, f)) {
314+
while (fread(&cheat, sizeof(cheat), 1, f) == 1) {
311315
if (magic != CHEAT_MAGIC) {
312316
cheat.address = __builtin_bswap16(cheat.address);
313317
cheat.bank = __builtin_bswap16(cheat.bank);
@@ -316,7 +320,12 @@ int GB_load_cheats(GB_gameboy_t *gb, const char *path, bool replace_existing)
316320
GB_add_cheat(gb, cheat.description, cheat.address, cheat.bank, cheat.value, cheat.old_value, cheat.use_old_value, cheat.enabled);
317321
}
318322

323+
fclose(f);
319324
return 0;
325+
326+
error:
327+
fclose(f);
328+
return -1;
320329
}
321330

322331
int GB_save_cheats(GB_gameboy_t *gb, const char *path)

Core/gb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static char *default_input_callback(GB_gameboy_t *gb)
5858

5959
if (getline(&expression, &size, stdin) == -1) {
6060
/* The user doesn't have STDIN or used ^D. We make sure the program keeps running. */
61+
free(expression);
6162
GB_set_async_input_callback(gb, NULL); /* Disable async input */
6263
return strdup("c");
6364
}

0 commit comments

Comments
 (0)