summaryrefslogtreecommitdiff
path: root/ext/json
diff options
context:
space:
mode:
authorFelipe Pena <felipensp@gmail.com>2012-05-08 21:05:51 -0300
committerFelipe Pena <felipensp@gmail.com>2012-05-08 21:05:51 -0300
commit92bc49b2b06417f86dc0fc537326e60f4d0a0c0b (patch)
tree570b7211d80f3ed83bc1938b4daf458837215363 /ext/json
parent2cb0ed1328da3bca00990648c3dfc90c957395f1 (diff)
downloadphp-git-92bc49b2b06417f86dc0fc537326e60f4d0a0c0b.tar.gz
- Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
Diffstat (limited to 'ext/json')
-rw-r--r--ext/json/json.c13
-rw-r--r--ext/json/tests/bug61978.phpt47
2 files changed, 60 insertions, 0 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index fc1fcb7f1f..557fbc3e0c 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -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);
diff --git a/ext/json/tests/bug61978.phpt b/ext/json/tests/bug61978.phpt
new file mode 100644
index 0000000000..2c732979ef
--- /dev/null
+++ b/ext/json/tests/bug61978.phpt
@@ -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}}"