missing-val-dialog: Avoid destroying uninitialized data.
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 19 Aug 2012 18:41:25 +0000 (11:41 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 19 Aug 2012 18:41:25 +0000 (11:41 -0700)
If text_to_value() fails for low_val, then this code would still
destroy high_val, even though high_val hadn't been initialized,
thus accessing indeterminate data.

This also eliminates some redundancy in the code.

src/ui/gui/missing-val-dialog.c

index 36a1331521e6ef4171bf03671571c2a16831c407..8c27cf366203a66f815e28b9b837cf67c2127aff 100644 (file)
@@ -125,28 +125,23 @@ missing_val_dialog_accept (GtkWidget *w, gpointer data)
       union value high_val;
       const gchar *low_text = gtk_entry_get_text (GTK_ENTRY (dialog->low));
       const gchar *high_text = gtk_entry_get_text (GTK_ENTRY (dialog->high));
-
-      if ( text_to_value (low_text, dialog->pv, &low_val)
-          &&
-          text_to_value (high_text, dialog->pv, &high_val))
-       {
-         if ( low_val.f > high_val.f )
-           {
-             err_dialog (_("Incorrect range specification"),
-                         GTK_WINDOW (dialog->window));
-             value_destroy (&low_val, var_get_width (dialog->pv));
-             value_destroy (&high_val, var_get_width (dialog->pv));
-             return ;
-           }
-       }
-      else
-       {
-         err_dialog (_("Incorrect range specification"),
-                     GTK_WINDOW (dialog->window));
-         value_destroy (&low_val, var_get_width (dialog->pv));
-         value_destroy (&high_val, var_get_width (dialog->pv));
-         return;
-       }
+      gboolean low_ok;
+      gboolean high_ok;
+      gboolean ok;
+
+      low_ok = text_to_value (low_text, dialog->pv, &low_val) != NULL;
+      high_ok = text_to_value (high_text, dialog->pv, &high_val) != NULL;
+      ok = low_ok && high_ok && low_val.f <= high_val.f;
+      if (!ok)
+        {
+          err_dialog (_("Incorrect range specification"),
+                      GTK_WINDOW (dialog->window));
+          if (low_ok)
+            value_destroy (&low_val, var_get_width (dialog->pv));
+          if (high_ok)
+            value_destroy (&high_val, var_get_width (dialog->pv));
+          return;
+        }
 
       discrete_text =
        g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->discrete)));