From 52c75ce5f6fa11979710c2cf39305e8143e85c27 Mon Sep 17 00:00:00 2001 From: girst Date: Fri, 30 Jun 2017 00:47:52 +0200 Subject: [PATCH] add restart game key, fix free()-segfault if help screen was requested, the program will terminate. this causes quit() to be called (set by atexit()), which in turn tried to free the elements of f.c, which have not been created then. --- mines_2017.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mines_2017.c b/mines_2017.c index 71ca10d..9609f2a 100644 --- a/mines_2017.c +++ b/mines_2017.c @@ -169,6 +169,7 @@ int main (int argc, char** argv) { f.w = 30; f.h = 16; f.m = 99; + f.c = NULL; /*to not free() array before it is allocated*/ op.scheme = &symbols_mono; op.mode = FLAG; @@ -201,7 +202,8 @@ int main (int argc, char** argv) { "hjkl: move 1 left/down/up/right\n" "bduw: move 5 left/down/up/right\n" "left mouse/space: open/choord\n" - "right mouse/i: flag/unflag\n", argv[0]); + "right mouse/i: flag/unflag\n" + ":D / r: start a new game\n", argv[0]); return 1; } } @@ -277,6 +279,9 @@ newgame: if (f.p[1] < 0 || f.p[1] >= f.w || f.p[0] < 0 || f.p[1] >= f.h) break; /*out of bound*/ /* fallthrough */ + case 'r': /* start a new game */ + free_field (); + goto newgame; case 'i': if (f.c[f.p[0]][f.p[1]].o == CLOSED) flag_square (f.p[0], f.p[1]); @@ -616,6 +621,7 @@ unalloc: } void free_field () { + if (f.c == NULL) return; /* quit() could be called before alloc_array() */ for (int l = 0; l < f.h; l++) { free (f.c[l]); } -- 2.39.3