Skip to content

Commit

Permalink
Fixed bug #77843
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Apr 23, 2019
1 parent ce73841 commit 4831e15
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ PHP NEWS
- FPM:
. Fixed bug #77921 (static.php.net doesn't work anymore). (Peter Kokot)

- JSON:
. Fixed bug #77843 (Use after free with json serializer). (Nikita)

- Session:
. Fixed bug #77911 (Wrong warning for session.sid_bits_per_character). (cmb)

Expand Down
12 changes: 10 additions & 2 deletions ext/json/json_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,16 @@ int php_json_encode_zval(smart_str *buf, zval *val, int options, php_json_encode
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);
Expand Down
25 changes: 25 additions & 0 deletions ext/json/tests/bug77843.phpt
Original file line number Diff line number Diff line change
@@ -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"}]]"

0 comments on commit 4831e15

Please sign in to comment.