Skip to content

Commit eb7d57e

Browse files
committed
Merge branch 'PHP-7.3' into PHP-7.4
2 parents a7a318d + 6889241 commit eb7d57e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ext/json/json_encoder.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,16 @@ int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encode
567567
return php_json_encode_serializable_object(buf, val, options, encoder);
568568
}
569569
/* fallthrough -- Non-serializable object */
570-
case IS_ARRAY:
571-
return php_json_encode_array(buf, val, options, encoder);
570+
case IS_ARRAY: {
571+
/* Avoid modifications (and potential freeing) of the array through a reference when a
572+
* jsonSerialize() method is invoked. */
573+
zval zv;
574+
int res;
575+
ZVAL_COPY(&zv, val);
576+
res = php_json_encode_array(buf, &zv, options, encoder);
577+
zval_ptr_dtor_nogc(&zv);
578+
return res;
579+
}
572580

573581
case IS_REFERENCE:
574582
val = Z_REFVAL_P(val);

ext/json/tests/bug77843.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #77843: Use after free with json serializer
3+
--FILE--
4+
<?php
5+
6+
class X implements JsonSerializable {
7+
public $prop = "value";
8+
public function jsonSerialize() {
9+
global $arr;
10+
unset($arr[0]);
11+
var_dump($this);
12+
return $this;
13+
}
14+
}
15+
16+
$arr = [new X()];
17+
var_dump(json_encode([&$arr]));
18+
19+
?>
20+
--EXPECT--
21+
object(X)#1 (1) {
22+
["prop"]=>
23+
string(5) "value"
24+
}
25+
string(20) "[[{"prop":"value"}]]"

0 commit comments

Comments
 (0)