mirror of
https://github.com/php/php-src.git
synced 2024-10-02 07:16:11 +00:00
Merge branch 'PHP-5.4'
* PHP-5.4: - Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
This commit is contained in:
commit
4dc9545aaa
@ -513,6 +513,19 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio
|
||||
{
|
||||
zend_class_entry *ce = Z_OBJCE_P(val);
|
||||
zval *retval = NULL, fname;
|
||||
HashTable* myht;
|
||||
|
||||
if (Z_TYPE_P(val) == IS_ARRAY) {
|
||||
myht = HASH_OF(val);
|
||||
} else {
|
||||
myht = Z_OBJPROP_P(val);
|
||||
}
|
||||
|
||||
if (myht && myht->nApplyCount > 1) {
|
||||
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
|
||||
smart_str_appendl(buf, "null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
ZVAL_STRING(&fname, "jsonSerialize", 0);
|
||||
|
||||
|
47
ext/json/tests/bug61978.phpt
Normal file
47
ext/json/tests/bug61978.phpt
Normal file
@ -0,0 +1,47 @@
|
||||
--TEST--
|
||||
Bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
|
||||
--SKIPIF--
|
||||
<?php if (!extension_loaded("json")) print "skip"; ?>
|
||||
--FILE--
|
||||
<?php
|
||||
|
||||
class JsonTest1 {
|
||||
public $test;
|
||||
public $me;
|
||||
public function __construct() {
|
||||
$this->test = '123';
|
||||
$this->me = $this;
|
||||
}
|
||||
}
|
||||
|
||||
class JsonTest2 implements JsonSerializable {
|
||||
public $test;
|
||||
public function __construct() {
|
||||
$this->test = '123';
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return array(
|
||||
'test' => $this->test,
|
||||
'me' => $this
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$obj1 = new JsonTest1();
|
||||
var_dump(json_encode($obj1));
|
||||
|
||||
echo "\n==\n";
|
||||
|
||||
$obj2 = new JsonTest2();
|
||||
var_dump(json_encode($obj2));
|
||||
|
||||
?>
|
||||
--EXPECTF--
|
||||
Warning: json_encode(): recursion detected in %s on line %d
|
||||
string(44) "{"test":"123","me":{"test":"123","me":null}}"
|
||||
|
||||
==
|
||||
|
||||
Warning: json_encode(): recursion detected in %s on line %d
|
||||
string(44) "{"test":"123","me":{"test":"123","me":null}}"
|
Loading…
Reference in New Issue
Block a user