From 4520781f25805bd9e3b0a7b861d55a22baeff7e3 Mon Sep 17 00:00:00 2001 From: tiehuis Date: Wed, 30 Dec 2015 11:49:04 +1300 Subject: [PATCH] Remove various compilation warnings With the removal of these warnings we slightly improve the robustness of the highscore parsing, handling some more cases of bad parsing/writing and failure to open files. --- Makefile | 2 +- src/engine.h | 3 ++- src/highscore.c | 24 +++++++++++++++++++++--- src/highscore.h | 2 ++ src/main.c | 2 +- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index c3724f6..bc72348 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC ?= clang -CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100 +CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra -O2 -DINVERT_COLORS -DVT100 -O2 LFLAGS += PROGRAM := 2048 diff --git a/src/engine.h b/src/engine.h index 941dcb1..3f9310f 100644 --- a/src/engine.h +++ b/src/engine.h @@ -2,7 +2,6 @@ #define ENGINE_H #include -#include "gfx.h" #include "options.h" #define fatal(msg)\ @@ -34,6 +33,8 @@ enum { dir_up }; +struct gfx_state; + int gamestate_end_condition(struct gamestate*); void gamestate_new_block(struct gamestate*); int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*)); diff --git a/src/highscore.c b/src/highscore.c index b5446a2..f6ba15c 100644 --- a/src/highscore.c +++ b/src/highscore.c @@ -57,7 +57,9 @@ void highscore_reset(void) while (1) { /* fgets is used to avoid queuing that may occur with getchar */ - fgets(resp, resp_length, stdin); + if (fgets(resp, resp_length, stdin) == NULL) + return; + string_to_lower(resp); const size_t sl = strlen(resp); @@ -87,7 +89,16 @@ long highscore_load(struct gamestate *g) if (fd == NULL) fd = fopen(hsfile, "w+"); - fscanf(fd, "%ld", &result); + if (fd == NULL) { + fprintf(stderr, "load: Failed to open highscore file\n"); + return 0; + } + + if (fscanf(fd, "%ld", &result) != 1) { + fprintf(stderr, "load: Failed to parse highscore file\n"); + result = 0; + } + fclose(fd); if (g) g->score_high = result; @@ -105,6 +116,13 @@ void highscore_save(struct gamestate *g) const char *hsfile = highscore_retrieve_file(); FILE *fd = fopen(hsfile, "w"); - fprintf(fd, "%ld", g->score); + if (fd == NULL) { + fprintf(stderr, "save: Failed to open highscore file\n"); + return; + } + + if (fprintf(fd, "%ld", g->score) < 0) { + fprintf(stderr, "save: Failed to write highscore file\n"); + } fclose(fd); } diff --git a/src/highscore.h b/src/highscore.h index de62a5a..e1b4e19 100644 --- a/src/highscore.h +++ b/src/highscore.h @@ -1,6 +1,8 @@ #ifndef HIGHSCORE_H #define HIGHSCORE_H +#include "engine.h" + void highscore_reset(void); long highscore_load(struct gamestate *g); void highscore_save(struct gamestate *g); diff --git a/src/main.c b/src/main.c index f6e6644..29b0a96 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g) int main(int argc, char **argv) { struct gamestate *g = gamestate_init(argc, argv); - struct gfx_state *s; + struct gfx_state *s = NULL; if (g->opts->interactive) s = gfx_init(g);