Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 80656f0

Browse files
committed
Check column list length in XMLTABLE/JSON_TABLE alias
We weren't checking the length of the column list in the alias clause of an XMLTABLE or JSON_TABLE function (a "tablefunc" RTE), and it was possible to make the server crash by passing an overly long one. Fix it by throwing an error in that case, like the other places that deal with alias lists. In passing, modify the equivalent test used for join RTEs to look like the other ones, which was different for no apparent reason. This bug came in when XMLTABLE was born in version 10; backpatch to all stable versions. Reported-by: Wang Ke <krking@zju.edu.cn> Discussion: https://postgr.es/m/17480-1c9d73565bb28e90@postgresql.org
1 parent 2e9559b commit 80656f0

File tree

10 files changed

+40
-15
lines changed

10 files changed

+40
-15
lines changed

src/backend/parser/parse_clause.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,21 +1434,6 @@ transformFromClauseItem(ParseState *pstate, Node *n,
14341434
&res_colnames, &res_colvars,
14351435
res_nscolumns + res_colindex);
14361436

1437-
/*
1438-
* Check alias (AS clause), if any.
1439-
*/
1440-
if (j->alias)
1441-
{
1442-
if (j->alias->colnames != NIL)
1443-
{
1444-
if (list_length(j->alias->colnames) > list_length(res_colnames))
1445-
ereport(ERROR,
1446-
(errcode(ERRCODE_SYNTAX_ERROR),
1447-
errmsg("column alias list for \"%s\" has too many entries",
1448-
j->alias->aliasname)));
1449-
}
1450-
}
1451-
14521437
/*
14531438
* Now build an RTE and nsitem for the result of the join.
14541439
* res_nscolumns isn't totally done yet, but that's OK because

src/backend/parser/parse_relation.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,12 @@ addRangeTableEntryForTableFunc(ParseState *pstate,
19631963
eref->colnames = list_concat(eref->colnames,
19641964
list_copy_tail(tf->colnames, numaliases));
19651965

1966+
if (numaliases > list_length(tf->colnames))
1967+
ereport(ERROR,
1968+
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1969+
errmsg("%s function has %d columns available but %d columns specified",
1970+
"XMLTABLE", list_length(tf->colnames), numaliases)));
1971+
19661972
rte->eref = eref;
19671973

19681974
/*
@@ -2140,6 +2146,12 @@ addRangeTableEntryForJoin(ParseState *pstate,
21402146
eref->colnames = list_concat(eref->colnames,
21412147
list_copy_tail(colnames, numaliases));
21422148

2149+
if (numaliases > list_length(colnames))
2150+
ereport(ERROR,
2151+
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
2152+
errmsg("join expression \"%s\" has %d columns available but %d columns specified",
2153+
eref->aliasname, list_length(colnames), numaliases)));
2154+
21432155
rte->eref = eref;
21442156

21452157
/*

src/test/regress/expected/int2.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ SELECT '' AS five, * FROM INT2_TBL;
5151
| -32767
5252
(5 rows)
5353

54+
SELECT * FROM INT2_TBL AS f(a, b);
55+
ERROR: table "f" has 1 columns available but 2 columns specified
56+
SELECT * FROM (TABLE int2_tbl) AS s (a, b);
57+
ERROR: table "s" has 1 columns available but 2 columns specified
5458
SELECT '' AS four, i.* FROM INT2_TBL i WHERE i.f1 <> int2 '0';
5559
four | f1
5660
------+--------

src/test/regress/expected/join.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5788,6 +5788,9 @@ select * from
57885788
3 | 3
57895789
(6 rows)
57905790

5791+
-- check the number of columns specified
5792+
SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);
5793+
ERROR: join expression "ss" has 3 columns available but 4 columns specified
57915794
-- check we don't try to do a unique-ified semijoin with LATERAL
57925795
explain (verbose, costs off)
57935796
select * from

src/test/regress/expected/with.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,11 @@ DROP TABLE y;
10151015
--
10161016
-- error cases
10171017
--
1018+
WITH x(n, b) AS (SELECT 1)
1019+
SELECT * FROM x;
1020+
ERROR: WITH query "x" has 1 columns available but 2 columns specified
1021+
LINE 1: WITH x(n, b) AS (SELECT 1)
1022+
^
10181023
-- INTERSECT
10191024
WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT SELECT n+1 FROM x)
10201025
SELECT * FROM x;

src/test/regress/expected/xml.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,9 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
11451145
Table Function Call: XMLTABLE(('/ROWS/ROW'::text) PASSING (xmldata.data) COLUMNS id integer PATH ('@id'::text), _id FOR ORDINALITY, country_name text PATH ('COUNTRY_NAME/text()'::text) NOT NULL, country_id text PATH ('COUNTRY_ID'::text), region_id integer PATH ('REGION_ID'::text), size double precision PATH ('SIZE'::text), unit text PATH ('SIZE/@unit'::text), premier_name text DEFAULT ('not specified'::text) PATH ('PREMIER_NAME'::text))
11461146
(7 rows)
11471147

1148+
-- errors
1149+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
1150+
ERROR: XMLTABLE function has 1 columns available but 2 columns specified
11481151
-- XMLNAMESPACES tests
11491152
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
11501153
'/zz:rows/zz:row'

src/test/regress/sql/int2.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ INSERT INTO INT2_TBL(f1) VALUES ('');
2929

3030
SELECT '' AS five, * FROM INT2_TBL;
3131

32+
SELECT * FROM INT2_TBL AS f(a, b);
33+
34+
SELECT * FROM (TABLE int2_tbl) AS s (a, b);
35+
3236
SELECT '' AS four, i.* FROM INT2_TBL i WHERE i.f1 <> int2 '0';
3337

3438
SELECT '' AS four, i.* FROM INT2_TBL i WHERE i.f1 <> int4 '0';

src/test/regress/sql/join.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,9 @@ select * from
19711971
(select q1.v)
19721972
) as q2;
19731973

1974+
-- check the number of columns specified
1975+
SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);
1976+
19741977
-- check we don't try to do a unique-ified semijoin with LATERAL
19751978
explain (verbose, costs off)
19761979
select * from

src/test/regress/sql/with.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ DROP TABLE y;
454454
-- error cases
455455
--
456456

457+
WITH x(n, b) AS (SELECT 1)
458+
SELECT * FROM x;
459+
457460
-- INTERSECT
458461
WITH RECURSIVE x(n) AS (SELECT 1 INTERSECT SELECT n+1 FROM x)
459462
SELECT * FROM x;

src/test/regress/sql/xml.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ SELECT * FROM xmltableview1;
384384
EXPLAIN (COSTS OFF) SELECT * FROM xmltableview1;
385385
EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
386386

387+
-- errors
388+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
389+
387390
-- XMLNAMESPACES tests
388391
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
389392
'/zz:rows/zz:row'

0 commit comments

Comments
 (0)