Skip to content

Commit 4a42b03

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Free the hooked property value when json_encode() sees an exception
2 parents 5795e33 + 8c5e258 commit 4a42b03

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

ext/json/json_encoder.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
269269
if (EG(exception)) {
270270
PHP_JSON_HASH_UNPROTECT_RECURSION(recursion_rc);
271271
zend_release_properties(prop_ht);
272+
zval_ptr_dtor(&tmp);
272273
return FAILURE;
273274
}
274275
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
--TEST--
2+
json_encode() releases the hooked property value when the get hook throws
3+
--FILE--
4+
<?php
5+
6+
class Value
7+
{
8+
public function __destruct()
9+
{
10+
echo "Value::__destruct\n";
11+
}
12+
}
13+
14+
class ThrowOnFree
15+
{
16+
public function __destruct()
17+
{
18+
throw new Exception('thrown while freeing the get hook frame');
19+
}
20+
}
21+
22+
class Container
23+
{
24+
public $hooked {
25+
get {
26+
$local = new ThrowOnFree();
27+
return new Value();
28+
}
29+
}
30+
}
31+
32+
try {
33+
json_encode(new Container());
34+
} catch (Throwable $e) {
35+
echo $e::class, ': ', $e->getMessage(), "\n";
36+
}
37+
38+
echo "done\n";
39+
40+
?>
41+
--EXPECT--
42+
Value::__destruct
43+
Exception: thrown while freeing the get hook frame
44+
done

0 commit comments

Comments
 (0)