Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lqAzg-0004R3-2J for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Jun 2021 08:52:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lqAzd-0007S2-Td for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Jun 2021 08:52:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lqAzd-0007Qq-KR for pgsql-hackers@lists.postgresql.org; Mon, 07 Jun 2021 08:52:21 +0000 Received: from wforward3-smtp.messagingengine.com ([64.147.123.22]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lqAzZ-0006Ua-L7 for pgsql-hackers@lists.postgresql.org; Mon, 07 Jun 2021 08:52:21 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailforward.west.internal (Postfix) with ESMTP id 59BDF191A; Mon, 7 Jun 2021 04:52:14 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 07 Jun 2021 04:52:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=T4HpJz uUo5sE9I7A9pS1i481fSd2B83d6eXdXnFkxgU=; b=QEYMUROgANSijb9b2TEYF3 SRkjao6NPGSeetDCyBRf1k/n/VOm9F7fjqfbx++GTfedBRTtBm94Acpcq/45GlUh u63qQqvEZHE9M+O6h2X9vEZKoS+m8dfNTD6Mzdc4lQpsZskzUtrSRwH9640rpgp3 pZKOADjBXvzl2flkqTtsqr2Qvy3h8fXUjXdSVxC+NLmNxwEknMZVE2Vv1xFMSk9R y1U+TtzoUlhxTH4Y3GBuIELAmAUmlddfkN6cBen4+tw2mc5yNbVvMHXvqd9D0sUx sDH6ShmauksrIuKbeAqfpfssuTBEhxH8YclG7b7yY4nWUWmHPdzpTBCo8t3R/e1Q == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfedtjedgtdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtsehmtderredtfeehnecuhfhrohhmpefrvghtvghr ucfgihhsvghnthhrrghuthcuoehpvghtvghrrdgvihhsvghnthhrrghuthesvghnthgvrh hprhhishgvuggsrdgtohhmqeenucggtffrrghtthgvrhhnpefhudelveeiteevvedtudet heehueeiueejveejgeehveejvdfhheeivefhjeeileenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpvghtvghrrdgvihhsvghnthhrrghuthes vghnthgvrhhprhhishgvuggsrdgtohhm X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 7 Jun 2021 04:52:11 -0400 (EDT) Subject: Re: SQL-standard function body To: Julien Rouhaud , Noah Misch Cc: Jaime Casanova , PostgreSQL Hackers References: <20210315080344.wed62yy54fuse7jg@nol> <20210315081448.63vagqr3ly45l74i@nol> <20210331101256.d7h7d74o4celskt4@nol> <9277ef6a-bfa8-9c06-8e91-16c2892120cc@2ndquadrant.com> <20210403033943.dp6um65shvrovoj5@nol> <01c65ea6-e5bf-22f4-cacc-6d0f6853a965@2ndquadrant.com> <20210606044418.GA297923@rfd.leadboat.com> <20210606073220.42ytzr6r5bbhqh3q@nol> From: Peter Eisentraut Message-ID: <993a2fe6-93b9-3364-21bb-6e2d8c0fde13@enterprisedb.com> Date: Mon, 7 Jun 2021 10:52:10 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 MIME-Version: 1.0 In-Reply-To: <20210606073220.42ytzr6r5bbhqh3q@nol> Content-Type: multipart/mixed; boundary="------------F62BEB39F73530B25088A8E5" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------F62BEB39F73530B25088A8E5 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 06.06.21 09:32, Julien Rouhaud wrote: > On Sat, Jun 05, 2021 at 09:44:18PM -0700, Noah Misch wrote: >> I get a NULL pointer dereference if the function body has a doubled semicolon: >> >> create function f() returns int language sql begin atomic select 1;; end; > > You don't even need a statements to reproduce the problem, a body containing > only semi-colon(s) will behave the same. > > Attached patch should fix the problem. Your patch filters out empty statements at the parse transformation phase, so they are no longer present when you dump the body back out. So your edits in the test expected files don't fit. I suggest we just prohibit empty statements at the parse stage. I don't see a strong reason to allow them, and if we wanted to, we'd have to do more work, e.g., in ruleutils.c to print them back out correctly. --------------F62BEB39F73530B25088A8E5 Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="0001-Prevent-empty-statement-in-unquoted-SQL-function-bod.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-Prevent-empty-statement-in-unquoted-SQL-function-bod.pa"; filename*1="tch" 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 --------------F62BEB39F73530B25088A8E5--