MATRIX: Avoid assertion failure when DO IF has multiple ELSEs.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 3 May 2024 00:36:07 +0000 (17:36 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 3 May 2024 00:36:24 +0000 (17:36 -0700)
Thanks to Zhou Geng for reporting this bug as poc42 in the report here:
https://lists.gnu.org/archive/html/bug-gnu-pspp/2024-03/msg00015.html

src/language/commands/matrix.c
tests/language/commands/matrix.at

index 80febe3286c9e69ae1d85f712a44064659b883e7..ce93258fb4bd5213bc40894c814aa56312f191c4 100644 (file)
@@ -5753,6 +5753,7 @@ static bool
 matrix_do_if_clause_parse (struct matrix_state *s,
                            struct matrix_do_if *ifc,
                            bool parse_condition,
+                           bool stop_at_else,
                            size_t *allocated_clauses)
 {
   if (ifc->n_clauses >= *allocated_clauses)
@@ -5768,7 +5769,8 @@ matrix_do_if_clause_parse (struct matrix_state *s,
         return false;
     }
 
-  return matrix_commands_parse (s, &c->commands, "DO IF", "ELSE", "END IF");
+  return matrix_commands_parse (s, &c->commands, "DO IF", "END IF",
+                                stop_at_else ? "ELSE" : NULL);
 }
 
 static struct matrix_command *
@@ -5783,13 +5785,15 @@ matrix_do_if_parse (struct matrix_state *s)
   size_t allocated_clauses = 0;
   do
     {
-      if (!matrix_do_if_clause_parse (s, &cmd->do_if, true, &allocated_clauses))
+      if (!matrix_do_if_clause_parse (s, &cmd->do_if, true, true,
+                                      &allocated_clauses))
         goto error;
     }
   while (lex_match_phrase (s->lexer, "ELSE IF"));
 
   if (lex_match_id (s->lexer, "ELSE")
-      && !matrix_do_if_clause_parse (s, &cmd->do_if, false, &allocated_clauses))
+      && !matrix_do_if_clause_parse (s, &cmd->do_if, false, false,
+                                     &allocated_clauses))
     goto error;
 
   if (!lex_match_phrase (s->lexer, "END IF"))
index 0ae9d07bb4fbf8729bdadbc7bc3ed6ad728d2073..d3cf0e34afb1b1876f1aa8a811f1509162c50ec8 100644 (file)
@@ -2969,6 +2969,12 @@ DO IF 0.
 ELSE IF {}.
 END IF.
 END MATRIX.
+
+MATRIX.
+DO IF 0.
+ELSE.
+ELSE.
+END MATRIX.
 ])
 AT_CHECK([pspp matrix.sps], [1], [dnl
 1
@@ -2986,6 +2992,10 @@ matrix.sps:24.9-24.10: error: MATRIX: Expression for ELSE IF must evaluate to
 scalar, not a 0×0 matrix.
    24 | ELSE IF {}.
       |         ^~
+
+matrix.sps:31.1-31.4: error: DO IF: Unknown matrix command.
+   31 | ELSE.
+      | ^~~~
 ])
 AT_CLEANUP