Skip to content

Commit ca54f9b

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 fb3a77f commit ca54f9b

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
@@ -3626,7 +3626,7 @@ transformJsonArrayQueryConstructor(ParseState *pstate,
36263626
/* Transform query only for counting target list entries. */
36273627
qpstate = make_parsestate(pstate);
36283628

3629-
query = transformStmt(qpstate, ctor->query);
3629+
query = transformStmt(qpstate, copyObject(ctor->query));
36303630

36313631
if (count_nonjunk_tlist_entries(query->targetList) != 1)
36323632
ereport(ERROR,

src/test/regress/expected/sqljson.out

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

448+
SELECT JSON_ARRAY(WITH x AS (SELECT 1) VALUES (TRUE));
449+
json_array
450+
------------
451+
[true]
452+
(1 row)
453+
448454
-- Should fail
449455
SELECT JSON_ARRAY(SELECT FROM (VALUES (1)) foo(i));
450456
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
@@ -124,6 +124,8 @@ SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL)
124124
--SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL), ('{3,4}'), (NULL)) foo(i) NULL ON NULL);
125125
--SELECT JSON_ARRAY(SELECT i FROM (VALUES (NULL::int[]), ('{1,2}'), (NULL), (NULL), ('{3,4}'), (NULL)) foo(i) NULL ON NULL RETURNING jsonb);
126126
SELECT JSON_ARRAY(SELECT i FROM (VALUES (3), (1), (NULL), (2)) foo(i) ORDER BY i);
127+
SELECT JSON_ARRAY(WITH x AS (SELECT 1) VALUES (TRUE));
128+
127129
-- Should fail
128130
SELECT JSON_ARRAY(SELECT FROM (VALUES (1)) foo(i));
129131
SELECT JSON_ARRAY(SELECT i, i FROM (VALUES (1)) foo(i));

0 commit comments

Comments
 (0)