diff options
author | yenatch <yenatch@gmail.com> | 2017-12-28 01:31:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-28 01:31:00 -0500 |
commit | e2b378f5e32ea1416fbc9ac5e96d23be244e4a6b (patch) | |
tree | 70fd1d4709150af457258763be04960931acd95b | |
parent | da28d1a84b0499bead314e17ae2ff0d13eb03196 (diff) | |
parent | 9af2aee640b555f9f52503233bc06d299b99974d (diff) |
Merge pull request #442 from yenatch/fix-fread
fix fread warnings
-rw-r--r-- | tools/Makefile | 1 | ||||
-rw-r--r-- | tools/common.h | 5 | ||||
-rw-r--r-- | tools/gfx.c | 7 | ||||
-rw-r--r-- | tools/palette.c | 15 | ||||
-rw-r--r-- | tools/png_dimensions.c | 6 | ||||
-rw-r--r-- | tools/pokemon_animation.c | 20 | ||||
-rw-r--r-- | tools/pokemon_animation_graphics.c | 10 |
7 files changed, 48 insertions, 16 deletions
diff --git a/tools/Makefile b/tools/Makefile index 36c643d41..4a5d51507 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -18,5 +18,6 @@ all: $(tools) clean: rm -f $(tools) +gfx md5: common.h %: %.c $(CC) $(CFLAGS) -o $@ $< diff --git a/tools/common.h b/tools/common.h index bc877ccb9..4da0b2ef1 100644 --- a/tools/common.h +++ b/tools/common.h @@ -21,7 +21,10 @@ uint8_t *read_u8(char *filename, int *size) { *size = ftell(f); rewind(f); uint8_t *data = malloc(*size); - fread(data, 1, *size, f); + if (*size != (int)fread(data, 1, *size, f)) { + fprintf(stderr, "Could not read file: \"%s\"\n", filename); + exit(1); + } fclose(f); return data; } diff --git a/tools/gfx.c b/tools/gfx.c index 3e5624e95..8c4066ab3 100644 --- a/tools/gfx.c +++ b/tools/gfx.c @@ -230,8 +230,13 @@ int png_get_width(char *filename) { const int OFFSET_WIDTH = 16; uint8_t bytes[4]; fseek(f, OFFSET_WIDTH, SEEK_SET); - fread(bytes, 1, 4, f); + size_t size = 4; + size_t result = fread(bytes, 1, size, f); fclose(f); + if (result != size) { + fprintf(stderr, "Could not read file at offset 0x%x: \"%s\"\n", OFFSET_WIDTH, filename); + exit(1); + } int width = 0; for (int i = 0; i < 4; i++) { diff --git a/tools/palette.c b/tools/palette.c index 397c62651..9b575efdc 100644 --- a/tools/palette.c +++ b/tools/palette.c @@ -29,7 +29,11 @@ void print_pokemon_palette(char* palette_filename) { } fseek(f, 2, SEEK_SET); - fread(bytes, 1, 4, f); + size_t size = 4; + if (size != fread(bytes, 1, size, f)) { + fprintf(stderr, "failed to read file %s\n", palette_filename); + exit(1); + } fclose(f); print_rgb((bytes[1] << 8) | bytes[0]); @@ -39,7 +43,7 @@ void print_pokemon_palette(char* palette_filename) { void print_palette(char* palette_filename) { FILE* f; uint8_t* bytes; - long size; + size_t size; int i; f = fopen(palette_filename, "rb"); @@ -63,10 +67,13 @@ void print_palette(char* palette_filename) { } fseek(f, 0, SEEK_SET); - fread(bytes, 1, size, f); + if (size != fread(bytes, 1, size, f)) { + fprintf(stderr, "failed to read file %s\n", palette_filename); + exit(1); + } fclose(f); - for (i = 0; i + 1 < size; i += 2) { + for (i = 0; i + 1 < (int)size; i += 2) { print_rgb((bytes[i + 1] << 8) | bytes[i]); } } diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c index 7bd8550aa..900485bdf 100644 --- a/tools/png_dimensions.c +++ b/tools/png_dimensions.c @@ -22,8 +22,12 @@ void output_dimensions(char* png_filename, char* out_filename) { // width fseek(f, 16, SEEK_SET); - fread(bytes, 1, 4, f); + int size = fread(bytes, 1, 4, f); fclose(f); + if (size != 4) { + fprintf(stderr, "failed to read at offset 0x10 in file %s\n", png_filename); + exit(1); + } width = 0; for (i = 0; i < 4; i++) { diff --git a/tools/pokemon_animation.c b/tools/pokemon_animation.c index 315a1729f..6e330a975 100644 --- a/tools/pokemon_animation.c +++ b/tools/pokemon_animation.c @@ -38,7 +38,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap uint8_t* tilemap; uint8_t* this_frame; FILE* f; - long size; + size_t size; int width; int height; uint8_t byte; @@ -48,7 +48,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap f = fopen(tilemap_filename, "rb"); if (f == NULL) { - fprintf(stderr, "could not open file %s", tilemap_filename); + fprintf(stderr, "could not open file %s\n", tilemap_filename); exit(1); } @@ -65,15 +65,21 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap fprintf(stderr, "malloc failure\n"); exit(1); } - fread(tilemap, 1, size, f); + if (size != fread(tilemap, 1, size, f)) { + fprintf(stderr, "failed to read file %s\n", tilemap_filename); + exit(1); + } fclose(f); f = fopen(dimensions_filename, "rb"); if (f == NULL) { - fprintf(stderr, "could not open file %s", dimensions_filename); + fprintf(stderr, "could not open file %s\n", dimensions_filename); + exit(1); + } + if (1 != fread(&byte, 1, 1, f)) { + fprintf(stderr, "failed to read file %s\n", dimensions_filename); exit(1); } - fread(&byte, 1, 1, f); fclose(f); width = byte & 0xf; @@ -137,7 +143,7 @@ void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap //} //free(frames->frames); - //fprintf(stderr, "num bitmasks: %d", bitmasks->num_bitmasks); + //fprintf(stderr, "num bitmasks: %d\n", bitmasks->num_bitmasks); //for (i = 0; i < bitmasks->num_bitmasks; i++) { // free(bitmasks->bitmasks[i].data); // fprintf(stderr, "freed bitmask %d\n", i); @@ -263,7 +269,7 @@ int main(int argc, char* argv[]) { //ext = strrchr(argv[3], '.'); //if (!ext || ext == argv[3]) { - // fprintf(stderr, "need a file extension to determine what to write to %s", argv[3]); + // fprintf(stderr, "need a file extension to determine what to write to %s\n", argv[3]); //} make_frames(&frames, &bitmasks, tilemap_filename, dimensions_filename); diff --git a/tools/pokemon_animation_graphics.c b/tools/pokemon_animation_graphics.c index 3ee1bd4cb..d57fa5b9c 100644 --- a/tools/pokemon_animation_graphics.c +++ b/tools/pokemon_animation_graphics.c @@ -100,7 +100,10 @@ void create_tilemap(struct Tilemap* tilemap, struct Graphic* graphic, char* grap fprintf(stderr, "malloc failure\n"); exit(1); } - fread(graphics, 1, graphics_size, f); + if (graphics_size != (long)fread(graphics, 1, graphics_size, f)) { + fprintf(stderr, "failed to read file %s\n", graphics_filename); + exit(1); + } fclose(f); int num_tiles_per_frame = width * height; @@ -213,7 +216,10 @@ int main(int argc, char* argv[]) { if (!f) { exit(1); } - fread(bytes, 1, 1, f); + if (1 != fread(bytes, 1, 1, f)) { + fprintf(stderr, "failed to read file %s\n", dimensions_filename); + exit(1); + } fclose(f); width = bytes[0] & 0xf; height = bytes[0] >> 4; |