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

From: Date: Tue, 13 May 2014 15:21:14 +0000
Subject: Bug #67265 [Nab->Opn]: Segmentation fault on json_encode with json_serializable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-185802@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 User updated by: edu at offerum dot com Reported by: edu at offerum dot com Summary: Segmentation fault on json_encode with json_serializable -Status: Not a bug +Status: Open 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, I was wrong thinking it has something to do with deleting object variables. Indeed, the behaviour can be seen pretty clear here: class C implements JsonSerializable { public function jsonSerialize() { $a = clone($this); echo 1; return $a; } } echo json_encode(new C()); Example: http://3v4l.org/nRMRj Previous Comments: ------------------------------------------------------------------------ [2014-05-13 15:19:26] mike@php.net Yep. This is not a bug but infinite recursion. ------------------------------------------------------------------------ [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

« previous php.bugs (#185802) next »