Check return value of fread()

Thu, 11 Apr 2013 09:37:11 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 11 Apr 2013 09:37:11 +0100
changeset 138
d744db15cdf7
parent 137
994d03cdcba2
child 139
d91346487fe9

Check return value of fread()

src/state.c file | annotate | diff | revisions
     1.1 --- a/src/state.c	Thu Apr 11 09:36:53 2013 +0100
     1.2 +++ b/src/state.c	Thu Apr 11 09:37:11 2013 +0100
     1.3 @@ -76,8 +76,14 @@
     1.4  	uint8_t *romdat1, *romdat2;
     1.5  	romdat1 = malloc(romlen);
     1.6  	romdat2 = malloc(romlen2);
     1.7 -	fread(romdat1, 1, romlen, r15c);
     1.8 -	fread(romdat2, 1, romlen2, r14c);
     1.9 +	if (fread(romdat1, 1, romlen, r15c) != romlen) {
    1.10 +		fprintf(stderr, "[state] Error reading ROM 15C.\n");
    1.11 +		return -3;
    1.12 +	}
    1.13 +	if (fread(romdat2, 1, romlen2, r14c) != romlen) {
    1.14 +		fprintf(stderr, "[state] Error reading ROM 14C.\n");
    1.15 +		return -3;
    1.16 +	}
    1.17  
    1.18  	// convert the ROM data
    1.19  	for (size_t i=0; i<(romlen + romlen2); i+=2) {