Avoid failure when selecting a namespace node in XMLTABLE.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Oct 2019 19:22:40 +0000 (15:22 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 25 Oct 2019 19:22:40 +0000 (15:22 -0400)
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)

src/backend/utils/adt/xml.c
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 48d98e96cf50af51f4cb236daeb3fec19bdf5525..edac2754aba283b66f3b158e9ff4dd473a099be9 100644 (file)
@@ -4612,6 +4612,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
@@ -4619,7 +4625,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;
index 2ed0d44b1e091edbbaf8b8f708d4303b31f3e96b..39058a007f19f9dd016a7b247cb09bcd67145c2a 100644 (file)
@@ -1173,6 +1173,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.*
index 9dd9b3f7ca5c1fbd4edcefade6502eaec3915d85..4d8eaaf4ec7d299435ccaa43746f4b328ffa9f9b 100644 (file)
@@ -1048,6 +1048,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.*
index 6fe363142bfc37f730b07ba7bf4a332b237c3aee..74a2ddd3b771a203ec27dea2b2bcb0a67b7a89f4 100644 (file)
@@ -1153,6 +1153,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.*
index 4e844234c53fd78bf622719043f7efb22bba7986..9af7cd7aeb7f0d0683b579ed2c5e494a5c39691c 100644 (file)
@@ -402,6 +402,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.*