ref: e2b378f5e32ea1416fbc9ac5e96d23be244e4a6b
parent: da28d1a84b0499bead314e17ae2ff0d13eb03196
parent: 9af2aee640b555f9f52503233bc06d299b99974d
author: yenatch <yenatch@gmail.com>
date: Wed Dec 27 20:31:00 EST 2017
Merge pull request #442 from yenatch/fix-fread fix fread warnings
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -18,5 +18,6 @@
clean:
rm -f $(tools)
+gfx md5: common.h
%: %.c
$(CC) $(CFLAGS) -o $@ $<
--- a/tools/common.h
+++ b/tools/common.h
@@ -21,7 +21,10 @@
*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;
}
--- a/tools/gfx.c
+++ b/tools/gfx.c
@@ -230,8 +230,13 @@
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++) {
--- a/tools/palette.c
+++ b/tools/palette.c
@@ -29,7 +29,11 @@
}
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_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 @@
}
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]);
}
}
--- a/tools/png_dimensions.c
+++ b/tools/png_dimensions.c
@@ -22,8 +22,12 @@
// 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++) {
--- a/tools/pokemon_animation.c
+++ b/tools/pokemon_animation.c
@@ -38,7 +38,7 @@
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 @@
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 @@
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);
}
- fread(&byte, 1, 1, f);
+ if (1 != fread(&byte, 1, 1, f)) {
+ fprintf(stderr, "failed to read file %s\n", dimensions_filename);
+ exit(1);
+ }
fclose(f);
width = byte & 0xf;
@@ -137,7 +143,7 @@
//}
//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 @@
//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);
--- a/tools/pokemon_animation_graphics.c
+++ b/tools/pokemon_animation_graphics.c
@@ -100,7 +100,10 @@
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 @@
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;