Skip to content

Commit fe29b2a

Browse files
author
Richard Guo
committed
Fix Assert failure in XMLTABLE parser
In an XMLTABLE expression, columns can be marked NOT NULL, and the parser internally fabricates an option named "is_not_null" to represent this. However, the parser also allows users to specify arbitrary option names. This creates a conflict: a user can explicitly use "is_not_null" as an option name and assign it a non-Boolean value, which violates internal assumptions and triggers an assertion failure. To fix, this patch checks whether a user-supplied name collides with the internally reserved option name and raises an error if so. Additionally, the internal name is renamed to "__pg__is_not_null" to further reduce the risk of collision with user-defined names. Reported-by: Евгений Горбанев <gorbanyoves@basealt.ru> Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Alvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/6bac9886-65bf-4cec-96bd-e304159f28db@basealt.ru Backpatch-through: 15
1 parent 2c0ed86 commit fe29b2a

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

src/backend/parser/gram.y

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14230,7 +14230,7 @@ xmltable_column_el:
1423014230
parser_errposition(defel->location)));
1423114231
fc->colexpr = defel->arg;
1423214232
}
14233-
else if (strcmp(defel->defname, "is_not_null") == 0)
14233+
else if (strcmp(defel->defname, "__pg__is_not_null") == 0)
1423414234
{
1423514235
if (nullability_seen)
1423614236
ereport(ERROR,
@@ -14273,13 +14273,20 @@ xmltable_column_option_list:
1427314273

1427414274
xmltable_column_option_el:
1427514275
IDENT b_expr
14276-
{ $$ = makeDefElem($1, $2, @1); }
14276+
{
14277+
if (strcmp($1, "__pg__is_not_null") == 0)
14278+
ereport(ERROR,
14279+
(errcode(ERRCODE_SYNTAX_ERROR),
14280+
errmsg("option name \"%s\" cannot be used in XMLTABLE", $1),
14281+
parser_errposition(@1)));
14282+
$$ = makeDefElem($1, $2, @1);
14283+
}
1427714284
| DEFAULT b_expr
1427814285
{ $$ = makeDefElem("default", $2, @1); }
1427914286
| NOT NULL_P
14280-
{ $$ = makeDefElem("is_not_null", (Node *) makeBoolean(true), @1); }
14287+
{ $$ = makeDefElem("__pg__is_not_null", (Node *) makeBoolean(true), @1); }
1428114288
| NULL_P
14282-
{ $$ = makeDefElem("is_not_null", (Node *) makeBoolean(false), @1); }
14289+
{ $$ = makeDefElem("__pg__is_not_null", (Node *) makeBoolean(false), @1); }
1428314290
| PATH b_expr
1428414291
{ $$ = makeDefElem("path", $2, @1); }
1428514292
;

src/test/regress/expected/xml.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
13731373
-- errors
13741374
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
13751375
ERROR: XMLTABLE function has 1 columns available but 2 columns specified
1376+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
1377+
ERROR: option name "__pg__is_not_null" cannot be used in XMLTABLE
1378+
LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
1379+
^
13761380
-- XMLNAMESPACES tests
13771381
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
13781382
'/zz:rows/zz:row'

src/test/regress/expected/xml_1.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
10471047
-- errors
10481048
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
10491049
ERROR: XMLTABLE function has 1 columns available but 2 columns specified
1050+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
1051+
ERROR: option name "__pg__is_not_null" cannot be used in XMLTABLE
1052+
LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
1053+
^
10501054
-- XMLNAMESPACES tests
10511055
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
10521056
'/zz:rows/zz:row'

src/test/regress/expected/xml_2.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
13591359
-- errors
13601360
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
13611361
ERROR: XMLTABLE function has 1 columns available but 2 columns specified
1362+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
1363+
ERROR: option name "__pg__is_not_null" cannot be used in XMLTABLE
1364+
LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
1365+
^
13621366
-- XMLNAMESPACES tests
13631367
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
13641368
'/zz:rows/zz:row'

src/test/regress/sql/xml.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
435435
-- errors
436436
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
437437

438+
SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
439+
438440
-- XMLNAMESPACES tests
439441
SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
440442
'/zz:rows/zz:row'

0 commit comments

Comments
 (0)