Skip to content

Commit aa006f1

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix use-after-free in property coercion with __toString()
2 parents 7e022ea + aca2322 commit aa006f1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Zend/tests/gh14969.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
GH-14969: Crash on coercion with throwing __toString()
3+
--FILE--
4+
<?php
5+
6+
class C {
7+
public function __toString() {
8+
global $c;
9+
$c = [];
10+
throw new Exception(__METHOD__);
11+
}
12+
}
13+
14+
class D {
15+
public string $prop;
16+
}
17+
18+
$c = new C();
19+
$d = new D();
20+
try {
21+
$d->prop = $c;
22+
} catch (Throwable $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
var_dump($d);
26+
27+
$c = new C();
28+
$d->prop = 'foo';
29+
try {
30+
$d->prop = $c;
31+
} catch (Throwable $e) {
32+
echo $e->getMessage(), "\n";
33+
}
34+
var_dump($d);
35+
36+
?>
37+
--EXPECTF--
38+
C::__toString
39+
object(D)#%d (0) {
40+
["prop"]=>
41+
uninitialized(string)
42+
}
43+
C::__toString
44+
object(D)#2 (1) {
45+
["prop"]=>
46+
string(3) "foo"
47+
}

Zend/zend_object_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ ZEND_API zval *zend_std_write_property(zend_object *zobj, zend_string *name, zva
974974
goto exit;
975975
}
976976
if (UNEXPECTED(!type_matched)) {
977-
Z_TRY_DELREF_P(value);
977+
zval_ptr_dtor(&tmp);
978978
variable_ptr = &EG(error_zval);
979979
goto exit;
980980
}

0 commit comments

Comments
 (0)