Skip to content

Commit

Permalink
improve load_rom error handling
Browse files Browse the repository at this point in the history
This commit fixes the singular, long-standing compiler warning of ignoring the return value of `fread`. It also catches more file errors and exits the program on failure, to match with `new_disk`.
  • Loading branch information
OliveIsAWord committed Oct 8, 2024
1 parent 1c98337 commit bfa01c9
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,29 @@ void main_loop(void) {
}

void load_rom(const char *filename) {
FILE *rom;
rom = fopen(filename, "r");
FILE *rom = fopen(filename, "r");

if (!rom) {
fprintf(stderr, "couldn't open ROM file %s\n", filename);
return;
fprintf(stderr, "couldn't find ROM file %s\n", filename);
exit(1);
}

printf("using %s as boot ROM\n", filename);
fread(&vm.memory_rom, sizeof(fox32rom), 1, rom);

if (fread(&vm.memory_rom, sizeof(fox32rom), 1, rom) != 1) {
fprintf(stderr, "error reading ROM file %s\n", filename);
if (feof(rom)) {
fprintf(stderr, "ROM file too small, must be %lu bytes\n", sizeof(fox32rom));
}
fclose(rom);
exit(1);
}

if (fgetc(rom) != EOF) {
fprintf(stderr, "error reading ROM file %s\n", filename);
fprintf(stderr, "ROM file too large, must be %lu bytes\n", sizeof(fox32rom));
fclose(rom);
exit(1);
}

fclose(rom);
}

0 comments on commit bfa01c9

Please sign in to comment.