LibSQL: Return an error for empty common table expression lists

SQL::CommonTableExpressionList is required to be non-empty. Return an
error if zero common table expressions were parsed.

Fixes #7627
This commit is contained in:
Timothy Flynn 2021-06-01 08:34:51 -04:00 committed by Ali Mohammad Pur
parent 5b86a8bad1
commit ab79599a5e
Notes: sideshowbarker 2024-07-18 17:02:47 +09:00
3 changed files with 12 additions and 2 deletions

View file

@ -682,6 +682,8 @@ TEST_CASE(select)
TEST_CASE(common_table_expression)
{
EXPECT(parse("WITH").is_error());
EXPECT(parse("WITH;").is_error());
EXPECT(parse("WITH DELETE FROM table;").is_error());
EXPECT(parse("WITH table DELETE FROM table;").is_error());
EXPECT(parse("WITH table AS DELETE FROM table;").is_error());

View file

@ -23,6 +23,9 @@ NonnullRefPtr<Statement> Parser::next_statement()
if (match(TokenType::With)) {
auto common_table_expression_list = parse_common_table_expression_list();
if (!common_table_expression_list)
return create_ast_node<ErrorStatement>();
return terminate_statement(parse_statement_with_expression_list(move(common_table_expression_list)));
}
@ -331,7 +334,7 @@ NonnullRefPtr<Select> Parser::parse_select_statement(RefPtr<CommonTableExpressio
return create_ast_node<Select>(move(common_table_expression_list), select_all, move(result_column_list), move(table_or_subquery_list), move(where_clause), move(group_by_clause), move(ordering_term_list), move(limit_clause));
}
NonnullRefPtr<CommonTableExpressionList> Parser::parse_common_table_expression_list()
RefPtr<CommonTableExpressionList> Parser::parse_common_table_expression_list()
{
consume(TokenType::With);
bool recursive = consume_if(TokenType::Recursive);
@ -339,6 +342,11 @@ NonnullRefPtr<CommonTableExpressionList> Parser::parse_common_table_expression_l
NonnullRefPtrVector<CommonTableExpression> common_table_expression;
parse_comma_separated_list(false, [&]() { common_table_expression.append(parse_common_table_expression()); });
if (common_table_expression.is_empty()) {
expected("Common table expression list");
return {};
}
return create_ast_node<CommonTableExpressionList>(recursive, move(common_table_expression));
}

View file

@ -59,7 +59,7 @@ private:
NonnullRefPtr<Update> parse_update_statement(RefPtr<CommonTableExpressionList>);
NonnullRefPtr<Delete> parse_delete_statement(RefPtr<CommonTableExpressionList>);
NonnullRefPtr<Select> parse_select_statement(RefPtr<CommonTableExpressionList>);
NonnullRefPtr<CommonTableExpressionList> parse_common_table_expression_list();
RefPtr<CommonTableExpressionList> parse_common_table_expression_list();
NonnullRefPtr<Expression> parse_primary_expression();
NonnullRefPtr<Expression> parse_secondary_expression(NonnullRefPtr<Expression> primary);