Commit 2008209faf5 for php
commit 2008209faf5b0eb462b4855406c8548021a24583
Author: lacatoire <la.catoire@gmail.com>
Date: Fri Oct 16 11:02:00 2026 +0200
Fix an uncollected SimpleXMLElement reference cycle (#25790)
SimpleXML exposes a temporary value by reference for a missing element.
The value can hold the element itself. Report it to the cycle collector
so cycles through that reference can be collected.
Co-authored-by: Nora Dossche <7771979+ndossche@users.noreply.github.com>
Closes #25790
diff --git a/NEWS b/NEWS
index 37c3cd8a16a..6c9cfdbf068 100644
--- a/NEWS
+++ b/NEWS
@@ -285,6 +285,8 @@ PHP NEWS
property defaults use constants. A class with an undefined constant now
throws an Error instead of returning an object with unresolved values.
(Ilia Alshanetsky)
+ . Fixed a memory leak when a SimpleXMLElement forms a reference cycle
+ through a missing element. (lacatoire, ndossche)
- Sodium:
. Fixed sodium_add() and sodium_increment() retaining stale string hashes
diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
index e5dc6898eaf..fe427bb9feb 100644
--- a/ext/simplexml/simplexml.c
+++ b/ext/simplexml/simplexml.c
@@ -1224,8 +1224,9 @@ static HashTable *sxe_get_gc(zend_object *object, zval **table, int *n) /* {{{ *
php_sxe_object *sxe;
sxe = php_sxe_fetch_object(object);
- *table = NULL;
- *n = 0;
+ /* sxe->tmp is handed out by reference and can hold the element itself */
+ *table = &sxe->tmp;
+ *n = 1;
return sxe->properties;
}
/* }}} */
diff --git a/ext/simplexml/tests/get_gc_tmp_cycle.phpt b/ext/simplexml/tests/get_gc_tmp_cycle.phpt
new file mode 100644
index 00000000000..3750d6f5436
--- /dev/null
+++ b/ext/simplexml/tests/get_gc_tmp_cycle.phpt
@@ -0,0 +1,25 @@
+--TEST--
+SimpleXMLElement: a cycle through the by-reference property of a missing element is collected
+--EXTENSIONS--
+simplexml
+--FILE--
+<?php
+$x = simplexml_load_string('<r/>');
+$w = WeakReference::create($x);
+$r = &$x->n;
+$r = $x;
+unset($r, $x);
+gc_collect_cycles();
+var_dump($w->get() === null);
+
+function g(&$o, $v) { $o = $v; }
+$x = simplexml_load_string('<r/>');
+$w = WeakReference::create($x);
+g($x->n, $x);
+unset($x);
+gc_collect_cycles();
+var_dump($w->get() === null);
+?>
+--EXPECT--
+bool(true)
+bool(true)