Fixed crash in error path of MEANS command 20130429010502/pspp
authorJohn Darrington <john@darrington.wattle.id.au>
Sun, 28 Apr 2013 15:48:16 +0000 (17:48 +0200)
committerJohn Darrington <john@darrington.wattle.id.au>
Sun, 28 Apr 2013 15:54:27 +0000 (17:54 +0200)
src/language/stats/means.c
tests/language/stats/means.at

index 44a140ecb5fc0e589d3db81a4348260b7921300d..9a07a14bb6f3dcffac0d82bfc5c98c36df95a200 100644 (file)
@@ -384,7 +384,7 @@ first_update (void *stat, double w UNUSED, double x)
   double *f = stat;
 
   if (*f == SYSMIS)
-     *f = x;
+    *f = x;
 }
 
 static double
@@ -502,11 +502,12 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct m
   table->ii = 0;
   table->n_layers = 0;
   table->layers = NULL;
+  table->interactions = NULL;
 
   /* Dependent variable (s) */
   if (!parse_variables_const_pool (lexer, cmd->pool, cmd->dict,
-                             &table->dep_vars, &table->n_dep_vars,
-                             PV_NO_DUPLICATE | PV_NUMERIC))
+                                  &table->dep_vars, &table->n_dep_vars,
+                                  PV_NO_DUPLICATE | PV_NUMERIC))
     return false;
 
   /* Factor variable (s) */
@@ -517,7 +518,7 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct m
          table->n_layers++;
           table->layers = 
            pool_realloc (cmd->pool, table->layers, 
-                     sizeof (*table->layers) * table->n_layers);
+                         sizeof (*table->layers) * table->n_layers);
 
          if (!parse_variables_const_pool 
               (lexer, cmd->pool, cmd->dict,
@@ -538,7 +539,7 @@ parse_means_table_syntax (struct lexer *lexer, const struct means *cmd, struct m
   table->n_layers++;
   table->layers = 
     pool_realloc (cmd->pool, table->layers, 
-              sizeof (*table->layers) * table->n_layers);
+                 sizeof (*table->layers) * table->n_layers);
   table->layers[table->n_layers - 1].factor_vars = NULL;
   table->layers[table->n_layers - 1].n_factor_vars = 0;
 
@@ -633,10 +634,10 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
       if (lex_match_id (lexer, "MISSING"))
        {
          /*
-            If no MISSING subcommand is specified, each combination of
-            a dependent variable and categorical variables is handled
-            separately.
-          */
+           If no MISSING subcommand is specified, each combination of
+           a dependent variable and categorical variables is handled
+           separately.
+         */
          lex_match (lexer, T_EQUALS);
          if (lex_match_id (lexer, "INCLUDE"))
            {
@@ -661,17 +662,17 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
            }
          else if (lex_match_id (lexer, "DEPENDENT"))
            /*
-            Use the command "/MISSING=DEPENDENT" to
-            include user-missing values for the categorical variables, 
-            while excluding them for the dependent variables.
+             Use the command "/MISSING=DEPENDENT" to
+             include user-missing values for the categorical variables, 
+             while excluding them for the dependent variables.
 
-            Cases are dropped only when user-missing values
-            appear in dependent  variables.  User-missing
-            values for categorical variables are treated according to
-            their face value.
+             Cases are dropped only when user-missing values
+             appear in dependent  variables.  User-missing
+             values for categorical variables are treated according to
+             their face value.
 
-            Cases are ALWAYS dropped when System Missing values appear 
-            in the categorical variables.
+             Cases are ALWAYS dropped when System Missing values appear 
+             in the categorical variables.
            */
            {
              means.dep_exclude = MV_ANY;
@@ -698,7 +699,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                  int x;
                  means.cells =
                    pool_realloc (means.pool, means.cells,
-                             (means.n_cells += n_C) * sizeof (*means.cells));
+                                 (means.n_cells += n_C) * sizeof (*means.cells));
 
                  for (x = 0; x < n_C; ++x)
                    means.cells[means.n_cells - (n_C - 1 - x) - 1] = x;
@@ -711,7 +712,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                {
                  means.cells =
                    pool_realloc (means.pool, means.cells,
-                             (means.n_cells += 3) * sizeof (*means.cells));
+                                 (means.n_cells += 3) * sizeof (*means.cells));
                  
                  means.cells[means.n_cells - 2 - 1] = MEANS_MEAN;
                  means.cells[means.n_cells - 1 - 1] = MEANS_N;
@@ -725,7 +726,7 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
                        {
                          means.cells =
                            pool_realloc (means.pool, means.cells,
-                                     ++means.n_cells * sizeof (*means.cells));
+                                         ++means.n_cells * sizeof (*means.cells));
                        
                          means.cells[means.n_cells - 1] = k;
                          break;
@@ -749,28 +750,28 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
 
 
   for (t = 0; t < means.n_tables; ++t)
-  {
-    struct mtable *table = &means.table[t];
+    {
+      struct mtable *table = &means.table[t];
 
-    table->interactions =
-      pool_calloc (means.pool, table->n_layers, sizeof (*table->interactions));
+      table->interactions =
+       pool_calloc (means.pool, table->n_layers, sizeof (*table->interactions));
 
-    table->summary =
-      pool_calloc (means.pool, table->n_dep_vars * table->n_layers, sizeof (*table->summary));
+      table->summary =
+       pool_calloc (means.pool, table->n_dep_vars * table->n_layers, sizeof (*table->summary));
 
-    for (l = 0; l < table->n_layers; ++l)
-      {
-        int v;
-        const struct layer *lyr = &table->layers[l];
-       const int n_vars = lyr->n_factor_vars;
-        table->interactions[l] = interaction_create (NULL);
-        for (v = 0; v < n_vars ; ++v)
-          {
-            interaction_add_variable (table->interactions[l],
-                                      lyr->factor_vars[v]);
-          }
-      }
-  }
+      for (l = 0; l < table->n_layers; ++l)
+       {
+         int v;
+         const struct layer *lyr = &table->layers[l];
+         const int n_vars = lyr->n_factor_vars;
+         table->interactions[l] = interaction_create (NULL);
+         for (v = 0; v < n_vars ; ++v)
+           {
+             interaction_add_variable (table->interactions[l],
+                                       lyr->factor_vars[v]);
+           }
+       }
+    }
 
   {
     struct casegrouper *grouper;
@@ -787,28 +788,30 @@ cmd_means (struct lexer *lexer, struct dataset *ds)
   }
 
   for (t = 0; t < means.n_tables; ++t)
-  {
-    int l;
-    struct mtable *table = &means.table[t];
-    for (l = 0; l < table->n_layers; ++l)
-      {
-       interaction_destroy (table->interactions[l]);
-      }
-  }
+    {
+      int l;
+      struct mtable *table = &means.table[t];
+      if (table->interactions)
+       for (l = 0; l < table->n_layers; ++l)
+         {
+           interaction_destroy (table->interactions[l]);
+         }
+    }
 
   pool_destroy (means.pool);
   return CMD_SUCCESS;
 
-error:
+ error:
 
   for (t = 0; t < means.n_tables; ++t)
     {
       int l;
       struct mtable *table = &means.table[t];
-      for (l = 0; l < table->n_layers; ++l)
-       {
-         interaction_destroy (table->interactions[l]);
-       }
+      if (table->interactions)
+       for (l = 0; l < table->n_layers; ++l)
+         {
+           interaction_destroy (table->interactions[l]);
+         }  
     }
   
   pool_destroy (means.pool);
@@ -977,14 +980,14 @@ run_means (struct means *cmd, struct casereader *input,
   payload.destroy = destroy_n;
   
   for (t = 0; t < cmd->n_tables; ++t)
-  {
-    struct mtable *table = &cmd->table[t];
-    table->cats
-      = categoricals_create (table->interactions,
-                            table->n_layers, wv, cmd->dep_exclude, cmd->exclude);
+    {
+      struct mtable *table = &cmd->table[t];
+      table->cats
+       = categoricals_create (table->interactions,
+                              table->n_layers, wv, cmd->dep_exclude, cmd->exclude);
 
-    categoricals_set_payload (table->cats, &payload, cmd, table);
-  }
+      categoricals_set_payload (table->cats, &payload, cmd, table);
+    }
 
   for (reader = input;
        (c = casereader_read (reader)) != NULL; case_unref (c))
index 4a82786c1155f38bbccf410d67c9cb4cfa96357d..56ab455ba87734b5638a35f3cf7caba626f52fa5 100644 (file)
@@ -554,3 +554,20 @@ AT_CLEANUP
 
 
 
+AT_SETUP([MEANS empty factor spec])
+
+AT_DATA([means-bad.sps], [dnl
+data list list /outcome *.
+begin data.
+1
+2
+3
+end data.
+
+MEANS TABLES =  outcome 
+       BY.
+])
+
+AT_CHECK([pspp -O format=csv means-bad.sps], [1], [ignore])
+
+AT_CLEANUP
\ No newline at end of file