psppire-dict: g_return_if_fail for idx in psppire_dict_get_variable().
[pspp] / src / ui / gui / psppire-dict.c
index 1e8beac5e3f1f1023ebb7d13f763596554915dd3..16576dc3aa5f80e38b58c8e21f24805b85ba236d 100644 (file)
@@ -379,23 +379,24 @@ psppire_dict_generate_name (const PsppireDict *dict, char *name, size_t size)
   return name;
 }
 
-/* Insert a new variable at posn IDX, with the name NAME.
+/* Insert a new variable at posn IDX, with the name NAME, and return the
+   new variable.
    If NAME is null, then a name will be automatically assigned.
 */
-void
+struct variable *
 psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
 {
-  struct variable *var ;
+  struct variable *var;
   char tmpname[64];
 
-  g_return_if_fail (idx >= 0);
-  g_return_if_fail (d);
-  g_return_if_fail (PSPPIRE_IS_DICT (d));
+  g_return_val_if_fail (idx >= 0, NULL);
+  g_return_val_if_fail (d, NULL);
+  g_return_val_if_fail (PSPPIRE_IS_DICT (d), NULL);
 
-  if ( ! name )
+  if (name == NULL)
     {
       if (!psppire_dict_generate_name (d, tmpname, sizeof tmpname))
-        g_return_if_reached ();
+        g_return_val_if_reached (NULL);
 
       name = tmpname;
     }
@@ -409,6 +410,8 @@ psppire_dict_insert_variable (PsppireDict *d, gint idx, const gchar *name)
   d->disable_insert_signal = FALSE;
 
   g_signal_emit (d, signals[VARIABLE_INSERTED], 0, idx);
+
+  return var;
 }
 
 /* Delete N variables beginning at FIRST */
@@ -461,17 +464,14 @@ psppire_dict_set_name (PsppireDict* d, gint idx, const gchar *name)
 
 
 
-/* Return the IDXth variable.
-   Will return NULL if IDX  exceeds the number of variables in the dictionary.
- */
+/* Return the IDXth variable in D. */
 struct variable *
 psppire_dict_get_variable (const PsppireDict *d, gint idx)
 {
   g_return_val_if_fail (d, NULL);
   g_return_val_if_fail (d->dict, NULL);
-
-  if ( dict_get_var_cnt (d->dict) <= idx )
-    return NULL;
+  g_return_val_if_fail (idx < 0, NULL);
+  g_return_val_if_fail (dict_get_var_cnt (d->dict) <= idx, NULL);
 
   return dict_get_var (d->dict, idx);
 }