summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-10-25 15:22:40 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2019-10-25 15:22:40 -0400
commit9bf24768664be6faf79333e1d4d6d14a0d33587b (patch)
tree7be70660be36e396038357ff72a1f5ee8a6fb8c2
parent84d1c5c45fd2d47c4d769ecc45210572a9bbb8f6 (diff)
Avoid failure when selecting a namespace node in XMLTABLE.
It appears that libxml2 doesn't bother to set the "children" field of an XML_NAMESPACE_DECL node to null; that field just contains garbage. In v10 and v11, this can result in a crash in XMLTABLE(). The rewrite done in commit 251cf2e27 fixed this, somewhat accidentally, in v12. We're not going to back-patch 251cf2e27, however. The case apparently doesn't have wide use, so rather than risk introducing other problems, just add a safety check to throw an error. Even though no bug manifests in v12/HEAD, add the relevant test case there too, to prevent future regressions. Chapman Flack (per private report)
-rw-r--r--src/backend/utils/adt/xml.c7
-rw-r--r--src/test/regress/expected/xml.out4
-rw-r--r--src/test/regress/expected/xml_1.out8
-rw-r--r--src/test/regress/expected/xml_2.out4
-rw-r--r--src/test/regress/sql/xml.sql4
5 files changed, 26 insertions, 1 deletions
diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 3a857bb0260..a456dca458d 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -4608,6 +4608,12 @@ XmlTableGetValue(TableFuncScanState *state, int colnum,
xmlChar *str;
xmlNodePtr node;
+ node = xpathobj->nodesetval->nodeTab[0];
+ if (node->type == XML_NAMESPACE_DECL)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("XMLTABLE cannot cast a namespace node to a non-XML result type")));
+
/*
* Most nodes (elements and even attributes) store their data
* in children nodes. If they don't have children nodes, it
@@ -4615,7 +4621,6 @@ XmlTableGetValue(TableFuncScanState *state, int colnum,
* CDATA sections are an exception: they don't have children
* but have content in the Text/CDATA node itself.
*/
- node = xpathobj->nodesetval->nodeTab[0];
if (node->type != XML_CDATA_SECTION_NODE &&
node->type != XML_TEXT_NODE)
node = node->xmlChildrenNode;
diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out
index 61fe3b44034..d887a314815 100644
--- a/src/test/regress/expected/xml.out
+++ b/src/test/regress/expected/xml.out
@@ -1167,6 +1167,10 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
COLUMNS a int PATH 'a');
ERROR: DEFAULT namespace is not supported
+SELECT * FROM XMLTABLE('/'
+ PASSING '<foo/>'
+ COLUMNS a text PATH 'foo/namespace::node()');
+ERROR: XMLTABLE cannot cast a namespace node to a non-XML result type
-- used in prepare statements
PREPARE pp AS
SELECT xmltable.*
diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out
index c20caea08af..9f2b0904f67 100644
--- a/src/test/regress/expected/xml_1.out
+++ b/src/test/regress/expected/xml_1.out
@@ -1042,6 +1042,14 @@ LINE 3: PASSING '<rows xmlns="http://x.y"><row...
^
DETAIL: This functionality requires the server to be built with libxml support.
HINT: You need to rebuild PostgreSQL using --with-libxml.
+SELECT * FROM XMLTABLE('/'
+ PASSING '<foo/>'
+ COLUMNS a text PATH 'foo/namespace::node()');
+ERROR: unsupported XML feature
+LINE 2: PASSING '<foo/>'
+ ^
+DETAIL: This functionality requires the server to be built with libxml support.
+HINT: You need to rebuild PostgreSQL using --with-libxml.
-- used in prepare statements
PREPARE pp AS
SELECT xmltable.*
diff --git a/src/test/regress/expected/xml_2.out b/src/test/regress/expected/xml_2.out
index 1e763acb444..f11eebfd911 100644
--- a/src/test/regress/expected/xml_2.out
+++ b/src/test/regress/expected/xml_2.out
@@ -1147,6 +1147,10 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
COLUMNS a int PATH 'a');
ERROR: DEFAULT namespace is not supported
+SELECT * FROM XMLTABLE('/'
+ PASSING '<foo/>'
+ COLUMNS a text PATH 'foo/namespace::node()');
+ERROR: XMLTABLE cannot cast a namespace node to a non-XML result type
-- used in prepare statements
PREPARE pp AS
SELECT xmltable.*
diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql
index e5eb0110e25..f7f5edc7171 100644
--- a/src/test/regress/sql/xml.sql
+++ b/src/test/regress/sql/xml.sql
@@ -401,6 +401,10 @@ SELECT * FROM XMLTABLE(XMLNAMESPACES(DEFAULT 'http://x.y'),
PASSING '<rows xmlns="http://x.y"><row><a>10</a></row></rows>'
COLUMNS a int PATH 'a');
+SELECT * FROM XMLTABLE('/'
+ PASSING '<foo/>'
+ COLUMNS a text PATH 'foo/namespace::node()');
+
-- used in prepare statements
PREPARE pp AS
SELECT xmltable.*