diff --git a/genann.c b/genann.c index 9becd71..4571471 100644 --- a/genann.c +++ b/genann.c @@ -30,6 +30,7 @@ #include #include #include +#include #define LOOKUP_SIZE 4096 @@ -122,13 +123,27 @@ genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs) { genann *genann_read(FILE *in) { int inputs, hidden_layers, hidden, outputs; - fscanf(in, "%d %d %d %d", &inputs, &hidden_layers, &hidden, &outputs); + int rc; + + errno = 0; + rc = fscanf(in, "%d %d %d %d", &inputs, &hidden_layers, &hidden, &outputs); + if (rc < 4 || errno != 0) { + perror("fscanf"); + return NULL; + } genann *ann = genann_init(inputs, hidden_layers, hidden, outputs); int i; for (i = 0; i < ann->total_weights; ++i) { - fscanf(in, " %le", ann->weight + i); + errno = 0; + rc = fscanf(in, " %le", ann->weight + i); + if (rc < 1 || errno != 0) { + perror("fscanf"); + genann_free(ann); + + return NULL; + } } return ann;