Check for invalid puzzle file added
authorHarishankar <v.harishankar@gmail.com>
Mon, 11 May 2020 05:24:33 +0000 (10:54 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 11 May 2020 05:24:33 +0000 (10:54 +0530)
Check for an invalid puzzle file is added. This will avoid a
segmentation fault when puzzle is tried to be loaded from an
invalid file or different file format

constantstrings.h
wordblah.c
wordblah.h
wordblah_player.c

index e571501..8c4c888 100644 (file)
@@ -23,6 +23,7 @@
 #define ERROR_READING_FILE "Error reading file"
 #define COMPRESSED " (compressed)"
 #define INVALID_WORD "Word contains illegal characters. Only alphabets allowed!"
+#define INVALID_PUZZLE "Puzzle is an invalid wordblah file or corrupted!"
 #define FILE_SAVED "File saved successfully"
 #define PASSWORD_SET "Password set successfully"
 #define MASTER_PASSWORD_RESET "Master password reset successfully. Puzzle no \
index 2dbc366..db5c605 100644 (file)
@@ -340,6 +340,12 @@ void do_open_puzzle (const char *filename)
        }
 
        p = load_puzzle (filename);
+       if (p.grid_size == 0)
+       {
+               printf (INVALID_PUZZLE);
+               char ch = getchar ();
+               return;
+       }
        
        if (strcmp (p.hashed_master_password, "\0") != 0)
        {
index 372ca60..48e1f28 100644 (file)
@@ -641,6 +641,13 @@ Puzzle load_puzzle (const char* file)
        char line[MAX_CLUE_LENGTH+10];
        fgets (line, MAX_CLUE_LENGTH + 10, infile);
        p.grid_size = atoi (line);
+       /* if puzzle is invalid or otherwise not proper grid, return an invalid
+          puzzle object */
+       if (p.grid_size == 0)
+       {
+               init_puzzle (&p, 0);
+               return p;
+       }
        fgets (line, MAX_CLUE_LENGTH + 10, infile);
        p.grid_frozen = atoi (line) == 0 ? false : true ;
        fgets (line, MAX_CLUE_LENGTH + 10, infile);
index 26dab29..08db02e 100644 (file)
@@ -622,15 +622,22 @@ void on_menu_open_activate (GtkMenuItem *item, GtkDrawingArea* area)
                MainPlayerData temp;
                reset_player_data (&temp, filename);
 
-               /* if the grid is not frozen then the game cannot be played */
-               if (temp.is_loaded == false)
+               /* if the puzzle is either an invalid puzzle file or grid is not frozen
+                then the game cannot be played */
+               if (temp.puzzle.grid_size == 0 || temp.is_loaded == false)
                {
                        GtkWidget *errordlg ;
+                       char message[256];
+                       if (temp.puzzle.grid_size == 0)
+                               strcpy (message, INVALID_PUZZLE);
+                       else
+                               strcpy (message, UNFROZEN_GRID_PLAYER);
+                               
                        errordlg = gtk_message_dialog_new (GTK_WINDOW(main_window), 
                                                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                                GTK_MESSAGE_ERROR,
                                                                                                GTK_BUTTONS_CLOSE,
-                                                                                               UNFROZEN_GRID_PLAYER);
+                                                                                               "%s",message);
                        gtk_dialog_run (GTK_DIALOG(errordlg));
                        gtk_widget_destroy (errordlg);
                }