@@ -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
322331int GB_save_cheats (GB_gameboy_t * gb , const char * path )
0 commit comments