]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
xml2: Fix failure with xslt_process() under -fsanitize=undefined
authorMichael Paquier <michael@paquier.xyz>
Fri, 13 Mar 2026 07:06:47 +0000 (16:06 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 13 Mar 2026 07:06:47 +0000 (16:06 +0900)
The logic of xslt_process() has never considered the fact that
xsltSaveResultToString() would return NULL for an empty string (the
upstream code has always done so, with a string length of 0).  This
would cause memcpy() to be called with a NULL pointer, something
forbidden by POSIX.

Like 46ab07ffda9d and similar fixes, this is backpatched down to all the
supported branches, with a test case to cover this scenario.  An empty
string has been always returned in xml2 in this case, based on the
history of the module, so this is an old issue.

Reported-by: Alexander Lakhin <exclusion@gmail.com>
Discussion: https://postgr.es/m/c516a0d9-4406-47e3-9087-5ca5176ebcf9@gmail.com
Backpatch-through: 14

contrib/xml2/expected/xml2.out
contrib/xml2/expected/xml2_1.out
contrib/xml2/sql/xml2.sql
contrib/xml2/xslt_proc.c

index eba6ae60364988ce633db5a1137666f28e270408..3027e4df868d83a77aabfe3e2d935f361b74a0b6 100644 (file)
@@ -222,3 +222,13 @@ $$<xsl:stylesheet version="1.0"
   </xsl:template>
 </xsl:stylesheet>$$);
 ERROR:  failed to apply stylesheet
+-- empty output
+select xslt_process('<aaa/>',
+$$<xsl:stylesheet version="1.0"
+      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+</xsl:stylesheet>$$);
+ xslt_process 
+--------------
+(1 row)
+
index bac90e5a2a9d199f689def56f00425dd28b63d1c..ed3e399aa317597afb0f3dce8c7f2cf4a602040a 100644 (file)
@@ -166,3 +166,9 @@ $$<xsl:stylesheet version="1.0"
   </xsl:template>
 </xsl:stylesheet>$$);
 ERROR:  xslt_process() is not available without libxslt
+-- empty output
+select xslt_process('<aaa/>',
+$$<xsl:stylesheet version="1.0"
+      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+</xsl:stylesheet>$$);
+ERROR:  xslt_process() is not available without libxslt
index ac49cfa7c52315ca48c4829e58253acc7eed49eb..c7fe0c102dab616a8dbf8809b05fd9e429d99257 100644 (file)
@@ -137,3 +137,9 @@ $$<xsl:stylesheet version="1.0"
     </sax:output>
   </xsl:template>
 </xsl:stylesheet>$$);
+
+-- empty output
+select xslt_process('<aaa/>',
+$$<xsl:stylesheet version="1.0"
+      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+</xsl:stylesheet>$$);
index f30a3a42c03e9328d25f44a5d87b8eee76919938..9f3bf527d802e781bd548d3994349bbb693b3b8e 100644 (file)
@@ -179,7 +179,14 @@ xslt_process(PG_FUNCTION_ARGS)
        if (resstat < 0)
                PG_RETURN_NULL();
 
-       result = cstring_to_text_with_len((char *) resstr, reslen);
+       /*
+        * If an empty string has been returned, resstr would be NULL. In
+        * this case, assume that the result is an empty string.
+        */
+       if (reslen == 0)
+               result = cstring_to_text("");
+       else
+               result = cstring_to_text_with_len((char *) resstr, reslen);
 
        if (resstr)
                xmlFree(resstr);