Commit 4fd9867c604 for php

commit 4fd9867c60499ca3eab0394fb6d095f620be6109
Author: Ilia Alshanetsky <ilia@ilia.ws>
Date:   Thu Nov 19 07:48:42 2026 -0500

    ext/simplexml: Resolve constants of user classes before instantiation

    simplexml_load_string(), simplexml_load_file() and simplexml_import_dom()
    create the object for a class_name class through php_sxe_object_new() and
    so skip the zend_update_class_constants() call that object_init_ex() makes.
    The constants of such a class (class constants, static and default property
    values) are not resolved first: a default property is copied into the
    object as a constant AST, an undefined constant or a type mismatch goes
    unreported, and under opcache the immutable class has no mutable data yet,
    so object_properties_init() dereferences NULL. Resolve the constants before
    any XML is parsed or imported, as new does before calling the constructor.
    In simplexml_import_dom() this has to precede php_libxml_import_node(), as
    constant evaluation can run an autoloader that reloads the document and
    frees the node. GH-25585 is the SPL counterpart.

    Closes GH-25598

diff --git a/NEWS b/NEWS
index 5593b4ce2ac..992b0318b9e 100644
--- a/NEWS
+++ b/NEWS
@@ -147,6 +147,13 @@ PHP                                                                        NEWS
   . Fixed session upload progress to respect session.use_only_cookies.
     (jorgsowa)

+- SimpleXML:
+  . Fixed a crash with opcache when simplexml_load_string(),
+    simplexml_load_file() or simplexml_import_dom() is given a class whose
+    property defaults use constants. A class with an undefined constant now
+    throws an Error instead of returning an object with unresolved values.
+    (Ilia Alshanetsky)
+
 - Standard:
   . Fixed bug GH-25474 (version_compare() treats large numeric parts as equal
     on 64-bit Windows). (lazerg, timwolla)
diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
index b8475ec62c8..e5dc6898eaf 100644
--- a/ext/simplexml/simplexml.c
+++ b/ext/simplexml/simplexml.c
@@ -2286,6 +2286,10 @@ PHP_FUNCTION(simplexml_load_file)
 		RETURN_THROWS();
 	}

+	if (ce && UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
+		RETURN_THROWS();
+	}
+
 	PHP_LIBXML_SANITIZE_GLOBALS(read_file);
 	docp = xmlReadFile(filename, NULL, (int)options);
 	PHP_LIBXML_RESTORE_GLOBALS(read_file);
@@ -2340,6 +2344,10 @@ PHP_FUNCTION(simplexml_load_string)
 		RETURN_THROWS();
 	}

+	if (ce && UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
+		RETURN_THROWS();
+	}
+
 	PHP_LIBXML_SANITIZE_GLOBALS(read_memory);
 	docp = xmlReadMemory(data, (int)data_len, NULL, NULL, (int)options);
 	PHP_LIBXML_RESTORE_GLOBALS(read_memory);
@@ -2650,6 +2658,10 @@ PHP_FUNCTION(simplexml_import_dom)
 		RETURN_THROWS();
 	}

+	if (ce && UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
+		RETURN_THROWS();
+	}
+
 	nodep = php_libxml_import_node(node);

 	if (!nodep) {
diff --git a/ext/simplexml/tests/simplexml_load_class_constants.phpt b/ext/simplexml/tests/simplexml_load_class_constants.phpt
new file mode 100644
index 00000000000..2614c7d77dc
--- /dev/null
+++ b/ext/simplexml/tests/simplexml_load_class_constants.phpt
@@ -0,0 +1,73 @@
+--TEST--
+simplexml_load_string(), simplexml_load_file() and simplexml_import_dom() resolve constants of the given class
+--EXTENSIONS--
+simplexml
+dom
+--INI--
+opcache.enable_cli=1
+--FILE--
+<?php
+const VALUE = 42;
+const STR = "a";
+
+class StringElement extends SimpleXMLElement {
+    public $var = VALUE;
+}
+class FileElement extends SimpleXMLElement {
+    public $var = VALUE;
+}
+class ImportedElement extends SimpleXMLElement {
+    public $var = VALUE;
+}
+class UndefElement extends SimpleXMLElement {
+    public $var = UNDEF_CONST;
+}
+class TypedElement extends SimpleXMLElement {
+    public int $var = STR;
+}
+class ConstElement extends SimpleXMLElement {
+    const C = UNDEF_CONST;
+}
+
+$file = __DIR__ . "/book.xml";
+$dom = new DOMDocument();
+$dom->loadXML("<root/>");
+
+echo get_class(simplexml_load_string("<root/>", StringElement::class)), PHP_EOL;
+echo get_class(simplexml_load_file($file, FileElement::class)), PHP_EOL;
+echo get_class(simplexml_import_dom($dom, ImportedElement::class)), PHP_EOL;
+
+$loaders = [
+    fn($class) => simplexml_load_string("<root/>", $class),
+    fn($class) => simplexml_load_file($file, $class),
+    fn($class) => simplexml_import_dom($dom, $class),
+];
+foreach ($loaders as $load) {
+    try {
+        $load(UndefElement::class);
+    } catch (Error $e) {
+        echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+    }
+}
+
+try {
+    simplexml_load_string("<root/>", TypedElement::class);
+} catch (TypeError $e) {
+    echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+}
+
+try {
+    simplexml_load_string("<root/>", ConstElement::class);
+} catch (Error $e) {
+    echo $e::class, ": ", $e->getMessage(), PHP_EOL;
+}
+?>
+--EXPECT--
+StringElement
+FileElement
+ImportedElement
+Error: Undefined constant "UNDEF_CONST"
+Error: Undefined constant "UNDEF_CONST"
+Error: Undefined constant "UNDEF_CONST"
+TypeError: Cannot assign string to property TypedElement::$var of type int
+Error: Undefined constant "UNDEF_CONST"