Skip to content

Commit 2447fd9

Browse files
committed
Fixed bug #79683
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.
1 parent ee4683c commit 2447fd9

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PHP NEWS
99
Nikita)
1010
. Fixed bug #79657 ("yield from" hangs when invalid value encountered).
1111
(Nikita)
12+
. Fixed bug #79683 (Fake reflection scope affects __toString()). (Nikita)
1213

1314
- Filter:
1415
. Fixed bug #73527 (Invalid memory access in php_filter_strip). (cmb)

Zend/zend_object_handlers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,10 @@ ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int ty
17961796
case IS_STRING:
17971797
ce = Z_OBJCE_P(readobj);
17981798
if (ce->__tostring) {
1799+
zend_class_entry *fake_scope = EG(fake_scope);
1800+
EG(fake_scope) = NULL;
17991801
zend_call_method_with_0_params(readobj, ce, &ce->__tostring, "__tostring", &retval);
1802+
EG(fake_scope) = fake_scope;
18001803
if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
18011804
ZVAL_COPY_VALUE(writeobj, &retval);
18021805
return SUCCESS;

ext/reflection/tests/bug79683.phpt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Bug #79683: Fake reflection scope affects __toString()
3+
--FILE--
4+
<?php
5+
6+
class A
7+
{
8+
private string $prop1 = '123';
9+
10+
public function __toString()
11+
{
12+
return $this->prop1;
13+
}
14+
}
15+
16+
class B
17+
{
18+
private string $prop2;
19+
}
20+
21+
$b = new B();
22+
23+
$reflector = new ReflectionClass($b);
24+
$property = $reflector->getProperty('prop2');
25+
$property->setAccessible(true);
26+
$property->setValue($b, new A());
27+
28+
var_dump($b);
29+
30+
?>
31+
--EXPECT--
32+
object(B)#1 (1) {
33+
["prop2":"B":private]=>
34+
string(3) "123"
35+
}

0 commit comments

Comments
 (0)