diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2019-04-23 12:43:37 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2019-04-23 12:43:37 +0200 |
commit | eb7d57eb778ae5ac13e7beffc3946e20882d5e51 (patch) | |
tree | 827c4fd186f14148f4221e3d0468f25dd7d05c03 /ext/json | |
parent | a7a318d3c0251cb618a322daf3fbff13a5b1baa5 (diff) | |
parent | 68892410f7540c5ce7f10fa6cbc9c4c8b747620b (diff) | |
download | php-git-eb7d57eb778ae5ac13e7beffc3946e20882d5e51.tar.gz |
Merge branch 'PHP-7.3' into PHP-7.4
Diffstat (limited to 'ext/json')
-rw-r--r-- | ext/json/json_encoder.c | 12 | ||||
-rw-r--r-- | ext/json/tests/bug77843.phpt | 25 |
2 files changed, 35 insertions, 2 deletions
diff --git a/ext/json/json_encoder.c b/ext/json/json_encoder.c index 23825696b3..c0fcbe3282 100644 --- a/ext/json/json_encoder.c +++ b/ext/json/json_encoder.c @@ -567,8 +567,16 @@ again: return php_json_encode_serializable_object(buf, val, options, encoder); } /* fallthrough -- Non-serializable object */ - case IS_ARRAY: - return php_json_encode_array(buf, val, options, encoder); + case IS_ARRAY: { + /* Avoid modifications (and potential freeing) of the array through a reference when a + * jsonSerialize() method is invoked. */ + zval zv; + int res; + ZVAL_COPY(&zv, val); + res = php_json_encode_array(buf, &zv, options, encoder); + zval_ptr_dtor_nogc(&zv); + return res; + } case IS_REFERENCE: val = Z_REFVAL_P(val); diff --git a/ext/json/tests/bug77843.phpt b/ext/json/tests/bug77843.phpt new file mode 100644 index 0000000000..c525285bf5 --- /dev/null +++ b/ext/json/tests/bug77843.phpt @@ -0,0 +1,25 @@ +--TEST-- +Bug #77843: Use after free with json serializer +--FILE-- +<?php + +class X implements JsonSerializable { + public $prop = "value"; + public function jsonSerialize() { + global $arr; + unset($arr[0]); + var_dump($this); + return $this; + } +} + +$arr = [new X()]; +var_dump(json_encode([&$arr])); + +?> +--EXPECT-- +object(X)#1 (1) { + ["prop"]=> + string(5) "value" +} +string(20) "[[{"prop":"value"}]]" |