php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61978 Object recursion not detected for classes that implement JsonSerializable
Submitted: 2012-05-08 21:42 UTC Modified: 2012-05-09 00:10 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: moisadoru at gmail dot com Assigned: felipe (profile)
Status: Closed Package: json (PECL)
PHP Version: 5.4.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: moisadoru at gmail dot com
New email:
PHP Version: OS:

 

 [2012-05-08 21:42 UTC] moisadoru at gmail dot com
Description:
------------
Calling json_encode() with an instance of a class that implements the 
JsonSerializable has a reference to itself (recursion), PHP coredumps (CLI) or 
exits with 'Allowed memory size of ... bytes exhusted'.

However, recursion in an instance of a class that does not implement the 
JsonSerializable interface is detected, a warning is shown and the actual JSON 
representation of the recursive reference is null.

In some situations, it tried to continously alocate memory, and eventually failed 
with the 'Allowed memory size of XXXX bytes exhausted' message.

Test script:
---------------
<?php // file: jsonserialize.php

ini_set('display_errors', 'on');
error_reporting(-1);
set_time_limit(1);
ini_set('memory_limit','512M');

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
        );
        // same results with:
        // return get_object_vars($this);
    }
}


$obj1 = new JsonTest1();
echo json_encode($obj1);

echo "\n==\n";

$obj2 = new JsonTest2();
echo json_encode($obj2);


Expected result:
----------------
Warning: json_encode(): recursion detected in jsonserialize.php on line 34
{"test":"123","me":{"test":"123","me":null}}
==
Warning: json_encode(): recursion detected in jsonserialize.php on line 39
{"test":"123","me":{"test":"123","me":null}}

Actual result:
--------------
Warning: json_encode(): recursion detected in /var/www/jsonserialize.php on line 
34
{"test":"123","me":{"test":"123","me":null}}
==
Segmentation fault (core dumped)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-09 00:08 UTC] felipe@php.net
Automatic comment on behalf of felipensp@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=92bc49b2b06417f86dc0fc537326e60f4d0a0c0b
Log: - Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
 [2012-05-09 00:10 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2012-05-09 00:10 UTC] felipe@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2014-01-22 13:52 UTC] smanikandan dot btech at gmail dot com
Hi guys,

i also getting the same issue. But i fixed like below. It's works for me.

    echo @json_encode(array or Object);

I hope, it will help.

Thanks.
 [2014-10-07 23:26 UTC] stas@php.net
Automatic comment on behalf of felipensp@gmail.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=92bc49b2b06417f86dc0fc537326e60f4d0a0c0b
Log: - Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
 [2014-10-07 23:37 UTC] stas@php.net
Automatic comment on behalf of felipensp@gmail.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=92bc49b2b06417f86dc0fc537326e60f4d0a0c0b
Log: - Fixed bug #61978 (Object recursion not detected for classes that implement JsonSerializable)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 16:01:33 2024 UTC