From afa5df1ffc12856fa4b55856b06c2abb8fc79f8b Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Sun, 22 Oct 2017 08:04:36 +1030 Subject: [PATCH 1/5] Makefile: Use $(RM), silencing errors on missing files $(RM) includes the -f flag, so the clean target now succeeds when files to remove don't exist. The post-condition of clean is that compilation artifacts are not present; this is trivially satisfied if they never existed. Signed-off-by: Andrew Jeffery --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 4c0cc48..6ff4e80 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,6 @@ example4: example4.o genann.o clean: - rm *.o - rm *.exe - rm persist.txt + $(RM) *.o + $(RM) *.exe + $(RM) persist.txt From 4ef0a3f87435fdb7138553089eac176900b178fc Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Sun, 22 Oct 2017 08:23:21 +1030 Subject: [PATCH 2/5] example4: Fix unused-result warning for fgets() Signed-off-by: Andrew Jeffery --- example4.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/example4.c b/example4.c index e458324..787e42a 100644 --- a/example4.c +++ b/example4.c @@ -42,7 +42,10 @@ void load_data() { double *c = class + i * 3; c[0] = c[1] = c[2] = 0.0; - fgets(line, 1024, in); + if (fgets(line, 1024, in) == NULL) { + perror("fgets"); + exit(1); + } char *split = strtok(line, ","); for (j = 0; j < 4; ++j) { From 9e86fc903eed2e7a19e6b6cf3e84d1387a1a0a6f Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Sun, 22 Oct 2017 08:23:58 +1030 Subject: [PATCH 3/5] genann: Fix unused-result warnings for fscanf() Signed-off-by: Andrew Jeffery --- genann.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) 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; From 8b35090f062847fa8dbdfb1239467415f613ddde Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Sun, 22 Oct 2017 08:24:44 +1030 Subject: [PATCH 4/5] genann: Sort headers Signed-off-by: Andrew Jeffery --- genann.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/genann.c b/genann.c index 4571471..0ec9b72 100644 --- a/genann.c +++ b/genann.c @@ -25,12 +25,12 @@ #include "genann.h" +#include +#include +#include +#include #include #include -#include -#include -#include -#include #define LOOKUP_SIZE 4096 From e4e40304e01728ad0efcdae3a7f807694249dc03 Mon Sep 17 00:00:00 2001 From: Andrew Jeffery Date: Sun, 22 Oct 2017 09:52:50 +1030 Subject: [PATCH 5/5] Makefile: Use standard make variables and recipes Signed-off-by: Andrew Jeffery --- Makefile | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 6ff4e80..c8f10ab 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,22 @@ CCFLAGS = -Wall -Wshadow -O2 -g -LFLAGS = -lm +LDLIBS = -lm all: test example1 example2 example3 example4 test: test.o genann.o - $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) - ./$@ +check: test + ./$^ example1: example1.o genann.o - $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) example2: example2.o genann.o - $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) example3: example3.o genann.o - $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) example4: example4.o genann.o - $(CC) $(CCFLAGS) -o $@ $^ $(LFLAGS) - -.c.o: - $(CC) -c $(CCFLAGS) $< -o $@ clean: