Commit:    92bc49b2b06417f86dc0fc537326e60f4d0a0c0b
Author:    Felipe Pena <felipe...@gmail.com>         Tue, 8 May 2012 21:05:51 
-0300
Parents:   2cb0ed1328da3bca00990648c3dfc90c957395f1
Branches:  PHP-5.4 master

Link:       
http://git.php.net/?p=php-src.git;a=commitdiff;h=92bc49b2b06417f86dc0fc537326e60f4d0a0c0b

Log:
- Fixed bug #61978 (Object recursion not detected for classes that implement 
JsonSerializable)

Bugs:
https://bugs.php.net/61978

Changed paths:
  M  ext/json/json.c
  A  ext/json/tests/bug61978.phpt


Diff:
diff --git a/ext/json/json.c b/ext/json/json.c
index fc1fcb7..557fbc3 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 0000000..2c73297
--- /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}}"


--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to