pivot-table: Avoid null dereference in "pspp-output dump" in corner cases.
[pspp] / src / output / pivot-table.c
index f1e0e73372177d1ed566c690523f7b0e1b7bf0dc..4744e66c4b936db575e4b62509be369b7cf488ab 100644 (file)
@@ -1939,6 +1939,8 @@ compose_headings (const struct pivot_table *pt,
 static void
 free_headings (const struct pivot_axis *axis, char ***headings)
 {
+  if (!headings)
+    return;
   for (size_t i = 0; i < axis->label_depth; i++)
     {
       for (size_t j = 0; j < axis->extent; j++)
@@ -2103,7 +2105,7 @@ pivot_table_dump (const struct pivot_table *table, int indentation)
             {
               if (x)
                 fputs ("; ", stdout);
-              if (column_headings[y][x])
+              if (column_headings && column_headings[y] && column_headings[y][x])
                 fputs (column_headings[y][x], stdout);
             }
           putchar ('\n');
@@ -2844,13 +2846,22 @@ pivot_value_new_value (const union value *value, int width,
 /* Returns a new pivot_value for VARIABLE. */
 struct pivot_value *
 pivot_value_new_variable (const struct variable *variable)
+{
+  return pivot_value_new_variable__ (var_get_name (variable),
+                                     var_get_label (variable));
+}
+
+/* Returns a new pivot_value for a variable with the given NAME and optional
+   LABEL. */
+struct pivot_value *
+pivot_value_new_variable__ (const char *name, const char *label)
 {
   struct pivot_value *value = xmalloc (sizeof *value);
   *value = (struct pivot_value) {
     .variable = {
       .type = PIVOT_VALUE_VARIABLE,
-      .var_name = xstrdup (var_get_name (variable)),
-      .var_label = xstrdup_if_nonempty (var_get_label (variable)),
+      .var_name = xstrdup (name),
+      .var_label = xstrdup_if_nonempty (label),
     },
   };
   return value;