Bug #67265 [Opn->Nab]: Segmentation fault on json_encode with json_serializable

From: Date: Tue, 13 May 2014 15:19:26 +0000
Subject: Bug #67265 [Opn->Nab]: Segmentation fault on json_encode with json_serializable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185800@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67265&edit=1

 ID:                 67265
 Updated by:         mike@php.net
 Reported by:        edu at offerum dot com
 Summary:            Segmentation fault on json_encode with
                     json_serializable
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            JSON related
 Operating System:   Linux (Ubuntu 14.04)
 PHP Version:        5.5.12
 Block user comment: N
 Private report:     N

 New Comment:

Yep. This is not a bug but infinite recursion.


Previous Comments:
------------------------------------------------------------------------
[2014-05-13 15:18:08] levim@php.net

My $0.02; find some way to protect against recursion and then fail nicely.

------------------------------------------------------------------------
[2014-05-13 15:14:46] levim@php.net

Additionally, the code is recursively calling jsonSerialize; you need to return an array.

------------------------------------------------------------------------
[2014-05-13 15:12:52] levim@php.net

Glancing at the backtrace on my local machine this has to do with clone call.

------------------------------------------------------------------------
[2014-05-13 14:09:16] edu at offerum dot com

Description:
------------
php-cgi segfaults when trying to encode a class which extends json_serializable.
At least this happens when the json_serialize method unsets a variable declared in the class.

Example in:
http://3v4l.org/TSgMV

---
From manual page: http://www.php.net/class.jsonserializable
---



Test script:
---------------
class C implements JsonSerializable {
    var $a = 'lol';
    var $b = 'fa';
    public function jsonSerialize() { 
	$aux = clone($this);
        unset($aux->a);
        return $aux; 
    }
}
echo json_encode(new C()); //Ka-boom!!

Expected result:
----------------
The object json-encoded only with var $b.

Actual result:
--------------
Segmentation fault.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=67265&edit=1


Thread (10 messages)

« previous php.bugs (#185800) next »