]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix Assert failure in XMLTABLE parser
authorRichard Guo <rguo@postgresql.org>
Thu, 15 May 2025 08:09:04 +0000 (17:09 +0900)
committerRichard Guo <rguo@postgresql.org>
Thu, 15 May 2025 08:21:15 +0000 (17:21 +0900)
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

src/backend/parser/gram.y
src/test/regress/expected/xml.out
src/test/regress/expected/xml_1.out
src/test/regress/expected/xml_2.out
src/test/regress/sql/xml.sql

index 365b4481ff4f5c0275ce907e7a5be48a01627240..702522f3d304af3ceda344a520d2e81caf4ac7e2 100644 (file)
@@ -13838,7 +13838,7 @@ xmltable_column_el:
                                                                                 parser_errposition(defel->location)));
                                                        fc->colexpr = defel->arg;
                                                }
-                                               else if (strcmp(defel->defname, "is_not_null") == 0)
+                                               else if (strcmp(defel->defname, "__pg__is_not_null") == 0)
                                                {
                                                        if (nullability_seen)
                                                                ereport(ERROR,
@@ -13881,13 +13881,20 @@ xmltable_column_option_list:
 
 xmltable_column_option_el:
                        IDENT b_expr
-                               { $$ = makeDefElem($1, $2, @1); }
+                               {
+                                       if (strcmp($1, "__pg__is_not_null") == 0)
+                                               ereport(ERROR,
+                                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                                errmsg("option name \"%s\" cannot be used in XMLTABLE", $1),
+                                                                parser_errposition(@1)));
+                                       $$ = makeDefElem($1, $2, @1);
+                               }
                        | DEFAULT b_expr
                                { $$ = makeDefElem("default", $2, @1); }
                        | NOT NULL_P
-                               { $$ = makeDefElem("is_not_null", (Node *) makeBoolean(true), @1); }
+                               { $$ = makeDefElem("__pg__is_not_null", (Node *) makeBoolean(true), @1); }
                        | NULL_P
-                               { $$ = makeDefElem("is_not_null", (Node *) makeBoolean(false), @1); }
+                               { $$ = makeDefElem("__pg__is_not_null", (Node *) makeBoolean(false), @1); }
                ;
 
 xml_namespace_list:
index c4b8a2101d7a840dcf84c9ab4b940865bc14e26d..caec8f830ed2c78274e48edce028d7c531cce246 100644 (file)
@@ -1373,6 +1373,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
 -- errors
 SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
 ERROR:  XMLTABLE function has 1 columns available but 2 columns specified
+SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
+ERROR:  option name "__pg__is_not_null" cannot be used in XMLTABLE
+LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
+                                                             ^
 -- XMLNAMESPACES tests
 SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
                       '/zz:rows/zz:row'
index 5efe284c9326504bcdf6548049ebbc07f2b36ef4..e89828cb4899db559abf962cc04d876f1a55e1f5 100644 (file)
@@ -1047,6 +1047,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
 -- errors
 SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
 ERROR:  XMLTABLE function has 1 columns available but 2 columns specified
+SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
+ERROR:  option name "__pg__is_not_null" cannot be used in XMLTABLE
+LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
+                                                             ^
 -- XMLNAMESPACES tests
 SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
                       '/zz:rows/zz:row'
index 7915a6c2c3b486de76d60af60586bd42fafae944..36bc9c2447b26ef7251ad4378298dc3ccea3f0ae 100644 (file)
@@ -1359,6 +1359,10 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
 -- errors
 SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
 ERROR:  XMLTABLE function has 1 columns available but 2 columns specified
+SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
+ERROR:  option name "__pg__is_not_null" cannot be used in XMLTABLE
+LINE 1: ...MLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_n...
+                                                             ^
 -- XMLNAMESPACES tests
 SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
                       '/zz:rows/zz:row'
index fd0bc71e7bb1545f809939448d86c1a00162f31e..efd17edfcc75ee5a4e9774159d0938c1ada0bbf7 100644 (file)
@@ -435,6 +435,8 @@ EXPLAIN (COSTS OFF, VERBOSE) SELECT * FROM xmltableview1;
 -- errors
 SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp) AS f (v1, v2);
 
+SELECT * FROM XMLTABLE (ROW () PASSING null COLUMNS v1 timestamp __pg__is_not_null 1) AS f (v1);
+
 -- XMLNAMESPACES tests
 SELECT * FROM XMLTABLE(XMLNAMESPACES('http://x.y' AS zz),
                       '/zz:rows/zz:row'