]> granicus.if.org Git - php/commitdiff
- Fixed bug #61978 (Object recursion not detected for classes that implement JsonSeri...
authorFelipe Pena <felipensp@gmail.com>
Wed, 9 May 2012 00:05:51 +0000 (21:05 -0300)
committerFelipe Pena <felipensp@gmail.com>
Wed, 9 May 2012 00:05:51 +0000 (21:05 -0300)
ext/json/json.c
ext/json/tests/bug61978.phpt [new file with mode: 0644]

index fc1fcb7f1f5e6ee4cc135eaef468b4a32767633b..557fbc3e0c8d6cb98b75a2fc01a3b982007717c2 100644 (file)
@@ -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 (file)
index 0000000..2c73297
--- /dev/null
@@ -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}}"