From 19817fa97da9afa4fd35365c6ecb968b53aff7fe Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 7 Jun 2021 10:49:36 +0200 Subject: [PATCH] Prevent empty statement in unquoted SQL function body This currently crashes. It was not meant to be supported, and there is no support for example in ruleutils.c, so just prohibit it in the parser. Reported-by: Noah Misch Discussion: https://www.postgresql.org/message-id/20210606044418.GA297923@rfd.leadboat.com --- src/backend/parser/gram.y | 7 ++++--- src/test/regress/expected/create_function_3.out | 8 ++++++++ src/test/regress/sql/create_function_3.sql | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 9ee90e3f13..3cd30c15ec 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -893,11 +893,14 @@ stmtmulti: stmtmulti ';' toplevel_stmt /* * toplevel_stmt includes BEGIN and END. stmt does not include them, because - * those words have different meanings in function bodys. + * those words have different meanings in function bodys. Also, empty + * statements are allowed at the top level but not in function bodies. */ toplevel_stmt: stmt | TransactionStmtLegacy + | /*EMPTY*/ + { $$ = NULL; } ; stmt: @@ -1024,8 +1027,6 @@ stmt: | VariableSetStmt | VariableShowStmt | ViewStmt - | /*EMPTY*/ - { $$ = NULL; } ; /***************************************************************************** diff --git a/src/test/regress/expected/create_function_3.out b/src/test/regress/expected/create_function_3.out index 5b6bc5eddb..41d8e49a23 100644 --- a/src/test/regress/expected/create_function_3.out +++ b/src/test/regress/expected/create_function_3.out @@ -290,6 +290,14 @@ CREATE FUNCTION functest_S_xx(x anyarray) RETURNS anyelement LANGUAGE SQL RETURN x[1]; ERROR: SQL function with unquoted function body cannot have polymorphic arguments +-- error: empty statement +CREATE FUNCTION functest_S_xx() RETURNS boolean + BEGIN ATOMIC + RETURN false;; + END; +ERROR: syntax error at or near ";" +LINE 3: RETURN false;; + ^ -- check reporting of parse-analysis errors CREATE FUNCTION functest_S_xx(x date) RETURNS boolean LANGUAGE SQL diff --git a/src/test/regress/sql/create_function_3.sql b/src/test/regress/sql/create_function_3.sql index 4b778999ed..5c28ddcbce 100644 --- a/src/test/regress/sql/create_function_3.sql +++ b/src/test/regress/sql/create_function_3.sql @@ -191,6 +191,12 @@ CREATE FUNCTION functest_S_xx(x anyarray) RETURNS anyelement LANGUAGE SQL RETURN x[1]; +-- error: empty statement +CREATE FUNCTION functest_S_xx() RETURNS boolean + BEGIN ATOMIC + RETURN false;; + END; + -- check reporting of parse-analysis errors CREATE FUNCTION functest_S_xx(x date) RETURNS boolean LANGUAGE SQL -- 2.31.1