]> granicus.if.org Git - php/commitdiff
Fixed bug #79683
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 9 Jun 2020 13:51:05 +0000 (15:51 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 9 Jun 2020 13:51:05 +0000 (15:51 +0200)
Reset fake_scope during __toString() call.

I'll check if we can solve this more globally in master, by
resetting fake_scope in zend_call_function.

NEWS
Zend/zend_object_handlers.c
ext/reflection/tests/bug79683.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index d647eabb1a737f3661b940b9e6461411f622066f..472577937b0a885b681fd64e9af445fef3a53e11 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,7 @@ PHP                                                                        NEWS
     Nikita)
   . Fixed bug #79657 ("yield from" hangs when invalid value encountered).
     (Nikita)
+  . Fixed bug #79683 (Fake reflection scope affects __toString()). (Nikita)
 
 - Filter:
   . Fixed bug #73527 (Invalid memory access in php_filter_strip). (cmb)
index 610b15c6aa68b2f0dca6b6a672544e3e5c4fcedb..5be63b4cd7b7ac2082978d03803c503ca3e336ce 100644 (file)
@@ -1796,7 +1796,10 @@ ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int ty
                case IS_STRING:
                        ce = Z_OBJCE_P(readobj);
                        if (ce->__tostring) {
+                               zend_class_entry *fake_scope = EG(fake_scope);
+                               EG(fake_scope) = NULL;
                                zend_call_method_with_0_params(readobj, ce, &ce->__tostring, "__tostring", &retval);
+                               EG(fake_scope) = fake_scope;
                                if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
                                        ZVAL_COPY_VALUE(writeobj, &retval);
                                        return SUCCESS;
diff --git a/ext/reflection/tests/bug79683.phpt b/ext/reflection/tests/bug79683.phpt
new file mode 100644 (file)
index 0000000..7665719
--- /dev/null
@@ -0,0 +1,35 @@
+--TEST--
+Bug #79683: Fake reflection scope affects __toString()
+--FILE--
+<?php
+
+class A
+{
+    private string $prop1 = '123';
+
+    public function __toString()
+    {
+        return $this->prop1;
+    }
+}
+
+class B
+{
+    private string $prop2;
+}
+
+$b = new B();
+
+$reflector = new ReflectionClass($b);
+$property = $reflector->getProperty('prop2');
+$property->setAccessible(true);
+$property->setValue($b, new A());
+
+var_dump($b);
+
+?>
+--EXPECT--
+object(B)#1 (1) {
+  ["prop2":"B":private]=>
+  string(3) "123"
+}