leaks
[pspp] / src / language / stats / ctables.c
index 6fcd0c7d35e2a86c3f991773f8b21981842efa54..e54be6d64bcfcb9d602438285945e8a41dad455e 100644 (file)
@@ -445,6 +445,8 @@ struct ctables_stack
     size_t n;
   };
 
+static void ctables_stack_uninit (struct ctables_stack *);
+
 struct ctables_value
   {
     struct hmap_node node;
@@ -470,6 +472,8 @@ struct ctables_section
     struct hmap domains[N_CTDTS]; /* Contains "struct ctables_domain"s. */
   };
 
+static void ctables_section_uninit (struct ctables_section *);
+
 struct ctables_table
   {
     struct ctables *ctables;
@@ -611,6 +615,7 @@ ctables_category_uninit (struct ctables_category *cat)
   if (!cat)
     return;
 
+  msg_location_destroy (cat->location);
   switch (cat->type)
     {
     case CCT_NUMBER:
@@ -830,7 +835,8 @@ static void
 ctables_summary_spec_set_clone (struct ctables_summary_spec_set *dst,
                                 const struct ctables_summary_spec_set *src)
 {
-  struct ctables_summary_spec *specs = xnmalloc (src->n, sizeof *specs);
+  struct ctables_summary_spec *specs
+    = (src->n ? xnmalloc (src->n, sizeof *specs) : NULL);
   for (size_t i = 0; i < src->n; i++)
     ctables_summary_spec_clone (&specs[i], &src->specs[i]);
 
@@ -1536,13 +1542,33 @@ ctables_table_destroy (struct ctables_table *t)
   if (!t)
     return;
 
+  for (size_t i = 0; i < t->n_sections; i++)
+    ctables_section_uninit (&t->sections[i]);
+  free (t->sections);
+
   for (size_t i = 0; i < t->n_categories; i++)
     ctables_categories_unref (t->categories[i]);
   free (t->categories);
 
-  ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
-  ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
-  ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
+  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+    {
+      ctables_axis_destroy (t->axes[a]);
+      ctables_stack_uninit (&t->stacks[a]);
+    }
+  free (t->summary_specs.specs);
+
+  struct ctables_value *ctv, *next_ctv;
+  HMAP_FOR_EACH_SAFE (ctv, next_ctv, struct ctables_value, node,
+                      &t->clabels_values_map)
+    {
+      value_destroy (&ctv->value, var_get_width (t->clabels_example));
+      hmap_delete (&t->clabels_values_map, &ctv->node);
+      free (ctv);
+    }
+  hmap_destroy (&t->clabels_values_map);
+  free (t->clabels_values);
+
+  free (t->sum_vars);
   free (t->caption);
   free (t->corner);
   free (t->title);
@@ -1557,6 +1583,24 @@ ctables_destroy (struct ctables *ct)
   if (!ct)
     return;
 
+  struct ctables_postcompute *pc, *next_pc;
+  HMAP_FOR_EACH_SAFE (pc, next_pc, struct ctables_postcompute, hmap_node,
+                      &ct->postcomputes)
+    {
+      free (pc->name);
+      msg_location_destroy (pc->location);
+      ctables_pcexpr_destroy (pc->expr);
+      free (pc->label);
+      if (pc->specs)
+        {
+          ctables_summary_spec_set_uninit (pc->specs);
+          free (pc->specs);
+        }
+      hmap_delete (&ct->postcomputes, &pc->hmap_node);
+      free (pc);
+    }
+
+  fmt_settings_uninit (&ct->ctables_formats);
   pivot_table_look_unref (ct->look);
   free (ct->zero);
   free (ct->missing);
@@ -2025,7 +2069,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
           int start_ofs = lex_ofs (lexer);
           struct ctables_category *cat = &c->cats[c->n_cats];
           if (!ctables_table_parse_explicit_category (lexer, dict, ct, cat))
-            return false;
+            goto error;
           cat->location = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1);
           c->n_cats++;
 
@@ -2055,7 +2099,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
           else
             {
               lex_error_expecting (lexer, "A", "D");
-              return false;
+              goto error;
             }
         }
       else if (!c->n_cats && lex_match_id (lexer, "KEY"))
@@ -2069,31 +2113,31 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
             {
               cat.type = CCT_FUNCTION;
               if (!parse_ctables_summary_function (lexer, &cat.sort_function))
-                return false;
+                goto error;
 
               if (lex_match (lexer, T_LPAREN))
                 {
                   cat.sort_var = parse_variable (lexer, dict);
                   if (!cat.sort_var)
-                    return false;
+                    goto error;
 
                   if (cat.sort_function == CTSF_PTILE)
                     {
                       lex_match (lexer, T_COMMA);
                       if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
-                        return false;
+                        goto error;
                       cat.percentile = lex_number (lexer);
                       lex_get (lexer);
                     }
 
                   if (!lex_force_match (lexer, T_RPAREN))
-                    return false;
+                    goto error;
                 }
               else if (ctables_function_availability (cat.sort_function)
                        == CTFA_SCALE)
                 {
                   bool UNUSED b = lex_force_match (lexer, T_LPAREN);
-                  return false;
+                  goto error;
                 }
             }
         }
@@ -2107,20 +2151,20 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
           else
             {
               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
-              return false;
+              goto error;
             }
         }
       else if (lex_match_id (lexer, "TOTAL"))
         {
           lex_match (lexer, T_EQUALS);
           if (!parse_bool (lexer, &show_totals))
-            return false;
+            goto error;
         }
       else if (lex_match_id (lexer, "LABEL"))
         {
           lex_match (lexer, T_EQUALS);
           if (!lex_force_string (lexer))
-            return false;
+            goto error;
           free (total_label);
           total_label = ss_xstrdup (lex_tokss (lexer));
           lex_get (lexer);
@@ -2135,7 +2179,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
           else
             {
               lex_error_expecting (lexer, "BEFORE", "AFTER");
-              return false;
+              goto error;
             }
         }
       else if (lex_match_id (lexer, "EMPTY"))
@@ -2148,7 +2192,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
           else
             {
               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
-              return false;
+              goto error;
             }
         }
       else
@@ -2158,7 +2202,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                                  "TOTAL", "LABEL", "POSITION", "EMPTY");
           else
             lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
-          return false;
+          goto error;
         }
     }
 
@@ -2225,8 +2269,6 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
 
   if (cats_start_ofs != -1)
     {
-      struct msg_location *cats_location
-        = lex_ofs_location (lexer, cats_start_ofs, cats_end_ofs);
       for (size_t i = 0; i < c->n_cats; i++)
         {
           struct ctables_category *cat = &c->cats[i];
@@ -2234,9 +2276,13 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
             {
             case CCT_POSTCOMPUTE:
               cat->parse_format = parse_strings ? common_format->type : FMT_F;
-              if (!ctables_recursive_check_postcompute (dict, cat->pc->expr,
-                                                        cat, c, cats_location))
-                return false;
+              struct msg_location *cats_location
+                = lex_ofs_location (lexer, cats_start_ofs, cats_end_ofs);
+              bool ok = ctables_recursive_check_postcompute (
+                dict, cat->pc->expr, cat, c, cats_location);
+              msg_location_destroy (cats_location);
+              if (!ok)
+                goto error;
               break;
 
             case CCT_NUMBER:
@@ -2250,7 +2296,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                               "subcommand tries to apply it to string "
                               "variable %s."),
                             var_get_name (vars[j]));
-                    return false;
+                    goto error;
                   }
               break;
 
@@ -2260,7 +2306,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                   double n;
                   if (!parse_category_string (cat->location, cat->string, dict,
                                               common_format->type, &n))
-                    return false;
+                    goto error;
 
                   ss_dealloc (&cat->string);
 
@@ -2268,7 +2314,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                   cat->number = n;
                 }
               else if (!all_strings (vars, n_vars, cat))
-                return false;
+                goto error;
               break;
 
             case CCT_SRANGE:
@@ -2281,14 +2327,14 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                   else if (!parse_category_string (cat->location,
                                                    cat->srange[0], dict,
                                                    common_format->type, &n[0]))
-                    return false;
+                    goto error;
 
                   if (!cat->srange[1].string)
                     n[1] = DBL_MAX;
                   else if (!parse_category_string (cat->location,
                                                    cat->srange[1], dict,
                                                    common_format->type, &n[1]))
-                    return false;
+                    goto error;
 
                   ss_dealloc (&cat->srange[0]);
                   ss_dealloc (&cat->srange[1]);
@@ -2298,7 +2344,7 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
                   cat->nrange[1] = n[1];
                 }
               else if (!all_strings (vars, n_vars, cat))
-                return false;
+                goto error;
               break;
 
             case CCT_MISSING:
@@ -2314,14 +2360,20 @@ ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
         }
     }
 
+  free (vars);
   return true;
+
+error:
+  free (vars);
+  return false;
 }
 
 static void
 ctables_nest_uninit (struct ctables_nest *nest)
 {
-  if (nest)
-    free (nest->vars);
+  free (nest->vars);
+  for (enum ctables_summary_variant sv = 0; sv < N_CSVS; sv++)
+    ctables_summary_spec_set_uninit (&nest->specs[sv]);
 }
 
 static void
@@ -4652,6 +4704,8 @@ ctables_table_output (struct ctables *ct, struct ctables_table *t)
             }
           free (sorted);
           free (groups);
+          free (levels);
+          free (sections);
         }
     }
 
@@ -5035,6 +5089,7 @@ ctables_prepare_table (struct ctables_table *t)
           j++;
         }
     }
+  free (items);
 
 #if 0
   for (size_t j = 0; j < merged->n; j++)
@@ -5335,6 +5390,29 @@ ctables_section_clear (struct ctables_section *s)
     }
 }
 
+static void
+ctables_section_uninit (struct ctables_section *s)
+{
+  ctables_section_clear (s);
+
+  for (enum pivot_axis_type a = 0; a < PIVOT_N_AXES; a++)
+    {
+      struct ctables_nest *nest = s->nests[a];
+      for (size_t i = 0; i < nest->n; i++)
+        hmap_destroy (&s->occurrences[a][i]);
+      free (s->occurrences[a]);
+      for (enum ctables_domain_type dt = 0; dt < N_CTDTS; dt++)
+        {
+          free (nest->domains[dt]);
+          nest->domains[dt] = NULL;
+        }
+    }
+
+  hmap_destroy (&s->cells);
+  for (size_t i = 0; i < N_CTDTS; i++)
+    hmap_destroy (&s->domains[i]);
+}
+
 static void
 ctables_table_clear (struct ctables_table *t)
 {