[php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5
| From: | Ilia Alshanetsky | Date: | Fri, 20 Nov 2026 00:06:46 +0000 |
| Subject: | [php-src] PHP-8.5: Merge branch 'PHP-8.4' into PHP-8.5 | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-143948@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-11-19T18:57:40-05:00
Commit: https://github.com/php/php-src/commit/78bf11806b0ed979ffe524a1660c0a0e7f2af3c1
Raw diff: https://github.com/php/php-src/commit/78bf11806b0ed979ffe524a1660c0a0e7f2af3c1.diff
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4:
ext/dom: Resolve constants of a node class when registering it
Changed paths:
A ext/dom/tests/registerNodeClass_undefined_constant.phpt
M NEWS
M ext/dom/document.c
Diff:
diff --git a/NEWS b/NEWS
index 1e9d0c7e053c..917e3c1519e8 100644
--- a/NEWS
+++ b/NEWS
@@ -48,6 +48,9 @@ PHP NEWS
when lexbor allocation fails). (ndossche)
. Fixed bug GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor
document init failure). (David Carlier)
+ . 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 4a0fadab7da6..46ec89ff8ccc 100644
--- a/ext/dom/document.c
+++ b/ext/dom/document.c
@@ -2307,9 +2307,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 000000000000..f6809932a110
--- /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"