Commit d8e34952e27 for php
commit d8e34952e274711b22fa43a93884ee3c8b991f1c
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Thu Nov 19 08:37:32 2026 -0500
ext/xsl: Fix crash in transformToDoc() with an uninstantiable class
XSLTProcessor::transformToDoc() ignored the result of object_init_ex() for
the return class and then used the zval as a libxml node object.
object_init_ex() fails when the class is abstract, which includes the
internal Dom\Document, or has a constant expression that cannot be
evaluated, such as a property default naming an undefined constant. The
zval is then NULL and the call crashed. Free the result document and return
with the pending exception instead. Without a return class the document is
wrapped by php_dom_create_object() with no class map, which only
instantiates internal classes.
Closes GH-25603
diff --git a/NEWS b/NEWS
index f77eaa9174e..84b2777e258 100644
--- a/NEWS
+++ b/NEWS
@@ -192,6 +192,10 @@ PHP NEWS
. Fixed bug GH-15701 (Build failure: incompatible funopen() seeker pointer
type on 32-bit platforms). (Kamil Tekiela)
+- XSL:
+ . Fixed a crash in XSLTProcessor::transformToDoc() when the return class
+ cannot be instantiated. (Ilia Alshanetsky)
+
19 Nov 2026, PHP 8.4.28
- Core:
diff --git a/ext/xsl/tests/transformToDoc_uninstantiable_class.phpt b/ext/xsl/tests/transformToDoc_uninstantiable_class.phpt
new file mode 100644
index 00000000000..328e6888a44
--- /dev/null
+++ b/ext/xsl/tests/transformToDoc_uninstantiable_class.phpt
@@ -0,0 +1,47 @@
+--TEST--
+XSLTProcessor::transformToDoc() with a return class that cannot be instantiated
+--EXTENSIONS--
+xsl
+dom
+simplexml
+--FILE--
+<?php
+
+abstract class AbstractDocument extends DOMDocument {}
+abstract class AbstractElement extends SimpleXMLElement {}
+class UndefinedConstantDocument extends DOMDocument {
+ public $value = UNDEFINED_CONSTANT;
+}
+
+$stylesheet = new DOMDocument;
+$stylesheet->loadXML('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><out/></xsl:template></xsl:stylesheet>');
+$processor = new XSLTProcessor;
+$processor->importStylesheet($stylesheet);
+
+$dom = new DOMDocument;
+$dom->loadXML('<root/>');
+$sxe = simplexml_load_string('<root/>');
+$modern = Dom\XMLDocument::createFromString('<root/>');
+
+foreach ([[$dom, AbstractDocument::class], [$sxe, AbstractElement::class], [$dom, UndefinedConstantDocument::class], [$modern, Dom\Document::class]] as [$input, $class]) {
+ try {
+ $processor->transformToDoc($input, $class);
+ } catch (Error $e) {
+ echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+ }
+}
+
+define('UNDEFINED_CONSTANT', 'defined');
+$result = $processor->transformToDoc($dom, UndefinedConstantDocument::class);
+echo $result::class, ": ", $result->value, PHP_EOL;
+echo $result->saveXML();
+
+?>
+--EXPECT--
+Error: Cannot instantiate abstract class AbstractDocument
+Error: Cannot instantiate abstract class AbstractElement
+Error: Undefined constant "UNDEFINED_CONSTANT"
+Error: Cannot instantiate abstract class Dom\Document
+UndefinedConstantDocument: defined
+<?xml version="1.0"?>
+<out/>
diff --git a/ext/xsl/xsltprocessor.c b/ext/xsl/xsltprocessor.c
index fa6b2d41e60..fb8bec67713 100644
--- a/ext/xsl/xsltprocessor.c
+++ b/ext/xsl/xsltprocessor.c
@@ -492,7 +492,10 @@ PHP_METHOD(XSLTProcessor, transformToDoc)
RETURN_THROWS();
}
- object_init_ex(return_value, ret_class);
+ if (UNEXPECTED(object_init_ex(return_value, ret_class) != SUCCESS)) {
+ xmlFreeDoc(newdocp);
+ RETURN_THROWS();
+ }
interndoc = Z_LIBXML_NODE_P(return_value);
php_libxml_increment_doc_ref(interndoc, newdocp);