Skip to content

Commit 717e8a1

Browse files
committed
Avoid double transformation of json_array()'s subquery.
transformJsonArrayQueryConstructor() applied transformStmt() to the same subquery tree twice. While this causes no issue in many cases, there are some where it causes a coredump, thanks to the parser's habit of scribbling on its input. Fix by making a copy before the first transformation (compare 0f43083). This is quite brute-force, but then so is the whole business of transforming the input twice. Per discussion in the bug thread, this implementation of json_array() parsing should be replaced completely. But that will take some work and will surely not be back-patchable, so for the moment let's take the easy way out. Oversight in 7081ac4. Back-patch to v16 where that came in. Bug: #18877 Reported-by: Yu Liang <luy70@psu.edu> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/18877-c3c3ad75845833bb@postgresql.org Backpatch-through: 16
1 parent 5e6e97f commit 717e8a1

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

src/backend/parser/parse_expr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3764,7 +3764,7 @@ transformJsonArrayQueryConstructor(ParseState *pstate,
37643764
/* Transform query only for counting target list entries. */
37653765
qpstate = make_parsestate(pstate);
37663766

3767-
query = transformStmt(qpstate, ctor->query);
3767+
query = transformStmt(qpstate, copyObject(ctor->query));
37683768

37693769
if (count_nonjunk_tlist_entries(query->targetList) != 1)
37703770
ereport(ERROR,

src/test/regress/expected/sqljson.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,12 @@ SELECT JSON_ARRAY(SELECT i FROM (VALUES (3), (1), (NULL), (2)) foo(i) ORDER BY i
747747
[1, 2, 3]
748748
(1 row)
749749

750+
SELECT JSON_ARRAY(WITH x AS (SELECT 1) VALUES (TRUE));
751+
json_array
752+
------------
753+
[true]
754+
(1 row)
755+
750756
-- Should fail
751757
SELECT JSON_ARRAY(SELECT FROM (VALUES (1)) foo(i));
752758
ERROR: subquery must return only one column

src/test/regress/sql/sqljson.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL)
199199
--SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL), ('{3,4}'), (NULL)) foo(i) NULL ON NULL);
200200
--SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL), ('{3,4}'), (NULL)) foo(i) NULL ON NULL RETURNING jsonb);
201201
SELECT JSON_ARRAY(SELECT i FROM (VALUES (3), (1), (NULL), (2)) foo(i) ORDER BY i);
202+
SELECT JSON_ARRAY(WITH x AS (SELECT 1) VALUES (TRUE));
203+
202204
-- Should fail
203205
SELECT JSON_ARRAY(SELECT FROM (VALUES (1)) foo(i));
204206
SELECT JSON_ARRAY(SELECT i, i FROM (VALUES (1)) foo(i));

0 commit comments

Comments
 (0)