pivot-table: Don't allow data cells for nonexistent categories.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 1 Jan 2021 02:27:50 +0000 (18:27 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 1 Jan 2021 02:28:16 +0000 (18:28 -0800)
src/language/stats/roc.c
src/output/pivot-table.c

index f18964a56298876275958a24e2adbc7e26eb4168..6c3a971bc44cb5a2d98c074b749c5ddf80ea4b05 100644 (file)
@@ -1094,6 +1094,13 @@ show_coords (struct roc_state *rs, const struct cmd_roc *roc)
          const double sp = case_data_idx (cc, ROC_TN)->f /
            (case_data_idx (cc, ROC_TN)->f + case_data_idx (cc, ROC_FP)->f);
 
+          if (coord_idx >= n_coords)
+            {
+              assert (coord_idx == n_coords);
+              pivot_category_create_leaf (
+                coordinates->root, pivot_value_new_integer (++n_coords));
+            }
+
           pivot_table_put3 (
             table, 0, coord_idx, var_idx,
             pivot_value_new_var_value (roc->vars[i],
@@ -1106,16 +1113,9 @@ show_coords (struct roc_state *rs, const struct cmd_roc *roc)
           coord_idx++;
        }
 
-      if (coord_idx > n_coords)
-        n_coords = coord_idx;
-
       casereader_destroy (r);
     }
 
-  for (size_t i = 0; i < n_coords; i++)
-    pivot_category_create_leaf (coordinates->root,
-                                pivot_value_new_integer (i + 1));
-
   pivot_table_submit (table);
 }
 
index 5c6467147300d43c0c98720c28a9b25ecaaecab8..4c1b2c752212981d9a61c98c82c594f8446d3dd4 100644 (file)
@@ -1057,6 +1057,8 @@ pivot_table_put (struct pivot_table *table, const size_t *dindexes, size_t n,
                  struct pivot_value *value)
 {
   assert (n == table->n_dimensions);
+  for (size_t i = 0; i < n; i++)
+    assert (dindexes[i] < table->dimensions[i]->n_leaves);
 
   if (value->type == PIVOT_VALUE_NUMERIC && !value->numeric.format.w)
     {