[hitori/13-localised-board: 1/2] main: Validate the board size loaded from GSettings



commit 8277537cd538bafa63ce7fa29e207cf29913509a
Author: Philip Withnall <withnall endlessm com>
Date:   Tue Mar 5 13:35:38 2019 +0000

    main: Validate the board size loaded from GSettings
    
    This code isn’t pretty, but it works and I don’t have time for a larger
    refactor at the moment.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 src/interface.c | 13 +++++++++++--
 src/main.c      |  9 +++++++++
 src/main.h      |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
---
diff --git a/src/interface.c b/src/interface.c
index 0dc8c77..1e15d4a 100644
--- a/src/interface.c
+++ b/src/interface.c
@@ -682,7 +682,16 @@ board_size_change_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
 
        size_str = g_settings_get_string (self->settings, "board-size");
        size = g_ascii_strtoull (size_str, NULL, 10);
-       hitori_set_board_size (self, size);
-
        g_free (size_str);
+
+       if (size > MAX_BOARD_SIZE) {
+               GVariant *default_size = g_settings_get_default_value (self->settings, "board-size");
+               g_variant_get (default_size, "s", &size_str);
+               g_variant_unref (default_size);
+               size = g_ascii_strtoull (size_str, NULL, 10);
+               g_free (size_str);
+               g_assert (size <= MAX_BOARD_SIZE);
+       }
+
+       hitori_set_board_size (self, size);
 }
diff --git a/src/main.c b/src/main.c
index a3859a6..602cb40 100644
--- a/src/main.c
+++ b/src/main.c
@@ -190,6 +190,15 @@ activate (GApplication *application)
                self->board_size = g_ascii_strtoull (size_str, NULL, 10);
                g_free (size_str);
 
+               if (self->board_size > MAX_BOARD_SIZE) {
+                       GVariant *default_size = g_settings_get_default_value (self->settings, "board-size");
+                       g_variant_get (default_size, "s", &size_str);
+                       g_variant_unref (default_size);
+                       self->board_size = g_ascii_strtoull (size_str, NULL, 10);
+                       g_free (size_str);
+                       g_assert (self->board_size <= MAX_BOARD_SIZE);
+               }
+
                undo = g_new0 (HitoriUndo, 1);
                undo->type = UNDO_NEW_GAME;
                self->undo_stack = undo;
diff --git a/src/main.h b/src/main.h
index 8b307ef..3d713ef 100644
--- a/src/main.h
+++ b/src/main.h
@@ -26,6 +26,7 @@
 G_BEGIN_DECLS
 
 #define DEFAULT_BOARD_SIZE 5
+#define MAX_BOARD_SIZE 10
 
 typedef struct {
        guchar x;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]