Commit 6adafff3bb3 for php

commit 6adafff3bb342654cd72adfd1f786233f7a3761c
Author: Lazizbek Ergashev <lazerg2@gmail.com>
Date:   Sun Dec 6 02:04:17 2026 +0500

    Fix GH-24729: UAF in ReflectionClass::setStaticPropertyValue() (#24732)

diff --git a/NEWS b/NEWS
index 83eabf69b36..e2984094444 100644
--- a/NEWS
+++ b/NEWS
@@ -25,6 +25,11 @@ PHP                                                                        NEWS
   . Fixed type inference of JIT trace after FE_FETCH_R[W] with identical
     branches. (ilutov, Arnaud)

+- Reflection:
+  . Fixed bug GH-24729 (Use-after-free in
+    ReflectionClass::setStaticPropertyValue() when the old value's destructor
+    unbinds the property). (Lazizbek Ergashev)
+
 - Standard:
   . Fixed bug GH-25959 ("consumed" stream filter loses or repeats data).
     (Artem Ukrainskiy)
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index 354888a6fb4..22bbb35e9c2 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -4446,21 +4446,12 @@ ZEND_METHOD(ReflectionClass, setStaticPropertyValue)
 		RETURN_THROWS();
 	}

-	if (Z_ISREF_P(variable_ptr)) {
-		zend_reference *ref = Z_REF_P(variable_ptr);
-		variable_ptr = Z_REFVAL_P(variable_ptr);
-
-		if (!zend_verify_ref_assignable_zval(ref, value, 0)) {
-			return;
-		}
-	}
-
 	if (ZEND_TYPE_IS_SET(prop_info->type) && !zend_verify_property_type(prop_info, value, 0)) {
 		return;
 	}

-	zval_ptr_dtor(variable_ptr);
-	ZVAL_COPY(variable_ptr, value);
+	Z_TRY_ADDREF_P(value);
+	zend_assign_to_variable(variable_ptr, value, IS_TMP_VAR, 0);

 }
 /* }}} */
diff --git a/ext/reflection/tests/gh24729.phpt b/ext/reflection/tests/gh24729.phpt
new file mode 100644
index 00000000000..beacb476328
--- /dev/null
+++ b/ext/reflection/tests/gh24729.phpt
@@ -0,0 +1,34 @@
+--TEST--
+GH-24729 (Use-after-free in ReflectionClass::setStaticPropertyValue() when the old value's destructor unbinds the property)
+--FILE--
+<?php
+class A { public static $x; }
+class Old {
+    public function __destruct() {
+        unset($GLOBALS['y']);
+        $z = 1;
+        A::$x = &$z;
+    }
+}
+$y = new Old;
+A::$x = &$y;
+(new ReflectionClass('A'))->setStaticPropertyValue('x', str_repeat('A', 64));
+var_dump(A::$x);
+
+class B { public static string $x = ''; }
+class S {
+    public function __toString(): string {
+        unset($GLOBALS['w']);
+        $z = 'z';
+        B::$x = &$z;
+        return str_repeat('B', 8);
+    }
+}
+$w = 'old';
+B::$x = &$w;
+(new ReflectionClass('B'))->setStaticPropertyValue('x', new S);
+var_dump(B::$x);
+?>
+--EXPECT--
+int(1)
+string(8) "BBBBBBBB"