Commit 51d440cbc62 for php
commit 51d440cbc62f51514758611e269c9a938c031095
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date: Thu Nov 19 08:32:06 2026 -0500
ext/dom: Resolve constants of a node class when registering it
object_init_ex() fails when a class has a constant expression that cannot
be evaluated, such as a property default naming an undefined constant.
php_dom_instantiate_object_helper() does not check for that failure, so a
class registered with registerNodeClass() crashed the first time a node
wrapper of that class was created. Evaluate the constants in
registerNodeClass() and throw there, as is already done for abstract
classes, so that every registered class can be instantiated. Registering
such a class now throws, where it previously succeeded until a node was
created.
Closes GH-25601
diff --git a/NEWS b/NEWS
index 2f4e243efe7..f77eaa9174e 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,9 @@ PHP NEWS
when lexbor allocation fails). (ndossche)
. Fixed bug GH-25434 (C14NFile() on a Dom\XMLDocument leaves the document
changed and leaks memory). (lazerg)
+ . Fixed crash when registerNodeClass() is given a class with a constant
+ expression that cannot be evaluated; registerNodeClass() now throws the
+ evaluation Error instead. (Ilia Alshanetsky)
- Filter:
. Fixed stack overflow in filter_var() and filter_var_array() with deeply
diff --git a/ext/dom/document.c b/ext/dom/document.c
index 377aee8029c..c7dd8a0c7a0 100644
--- a/ext/dom/document.c
+++ b/ext/dom/document.c
@@ -2313,9 +2313,14 @@ static void dom_document_register_node_class(INTERNAL_FUNCTION_PARAMETERS, bool
}
if (ce == NULL || instanceof_function(ce, basece)) {
- if (UNEXPECTED(ce != NULL && (ce->ce_flags & ZEND_ACC_ABSTRACT))) {
- zend_argument_value_error(2, "must not be an abstract class");
- RETURN_THROWS();
+ if (ce != NULL) {
+ if (UNEXPECTED(ce->ce_flags & ZEND_ACC_ABSTRACT)) {
+ zend_argument_value_error(2, "must not be an abstract class");
+ RETURN_THROWS();
+ }
+ if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
+ RETURN_THROWS();
+ }
}
DOM_GET_THIS_INTERN(intern);
dom_set_doc_classmap(intern->document, basece, ce);
diff --git a/ext/dom/tests/registerNodeClass_undefined_constant.phpt b/ext/dom/tests/registerNodeClass_undefined_constant.phpt
new file mode 100644
index 00000000000..f6809932a11
--- /dev/null
+++ b/ext/dom/tests/registerNodeClass_undefined_constant.phpt
@@ -0,0 +1,75 @@
+--TEST--
+registerNodeClass() with a class whose constant expressions cannot be evaluated
+--EXTENSIONS--
+dom
+--FILE--
+<?php
+
+const STRING_CONSTANT = "abc";
+
+class MyElement extends DOMElement {
+ public $x = UNDEFINED_CONSTANT;
+}
+
+class MyTypedElement extends DOMElement {
+ public int $x = STRING_CONSTANT;
+}
+
+class MyText extends DOMText {
+ const C = UNDEFINED_CONSTANT;
+}
+
+class MyDocument extends DOMDocument {
+ public static $x = UNDEFINED_CONSTANT;
+}
+
+class MyModernElement extends Dom\Element {
+ public $x = UNDEFINED_CONSTANT;
+}
+
+function register($doc, string $base, string $class) {
+ try {
+ $doc->registerNodeClass($base, $class);
+ echo "$class: registered\n";
+ } catch (Error $e) {
+ echo "$class: ", $e::class, ": ", $e->getMessage(), "\n";
+ }
+}
+
+$doc = new DOMDocument;
+$doc->loadXML('<root>text<a/></root>');
+register($doc, 'DOMElement', 'MyElement');
+register($doc, 'DOMElement', 'MyTypedElement');
+register($doc, 'DOMText', 'MyText');
+register($doc, 'DOMDocument', 'MyDocument');
+$root = $doc->documentElement;
+unset($doc);
+var_dump(get_class($root));
+var_dump(get_class($root->firstChild));
+var_dump(get_class($root->ownerDocument));
+
+$modern = Dom\XMLDocument::createFromString('<root><a/></root>');
+register($modern, 'Dom\Element', 'MyModernElement');
+var_dump(get_class($modern->documentElement));
+
+define('UNDEFINED_CONSTANT', 1);
+register($root->ownerDocument, 'DOMElement', 'MyElement');
+register($modern, 'Dom\Element', 'MyModernElement');
+var_dump(get_class($root->lastChild));
+var_dump(get_class($modern->documentElement->firstChild));
+
+?>
+--EXPECT--
+MyElement: Error: Undefined constant "UNDEFINED_CONSTANT"
+MyTypedElement: TypeError: Cannot assign string to property MyTypedElement::$x of type int
+MyText: Error: Undefined constant "UNDEFINED_CONSTANT"
+MyDocument: Error: Undefined constant "UNDEFINED_CONSTANT"
+string(10) "DOMElement"
+string(7) "DOMText"
+string(11) "DOMDocument"
+MyModernElement: Error: Undefined constant "UNDEFINED_CONSTANT"
+string(11) "Dom\Element"
+MyElement: registered
+MyModernElement: registered
+string(9) "MyElement"
+string(15) "MyModernElement"