From ff8d3d602fb97807eb5021c813f326a8d6466955 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 2 May 2024 17:36:07 -0700 Subject: [PATCH] MATRIX: Avoid assertion failure when DO IF has multiple ELSEs. 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 | 10 +++++++--- tests/language/commands/matrix.at | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/language/commands/matrix.c b/src/language/commands/matrix.c index 80febe3286..ce93258fb4 100644 --- a/src/language/commands/matrix.c +++ b/src/language/commands/matrix.c @@ -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")) diff --git a/tests/language/commands/matrix.at b/tests/language/commands/matrix.at index 0ae9d07bb4..d3cf0e34af 100644 --- a/tests/language/commands/matrix.at +++ b/tests/language/commands/matrix.at @@ -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 -- 2.30.2