Bug #78667 [Opn->Nab]: Segmentation fault when using clone in jsonSerilization method

From: Date: Sun, 13 Oct 2019 09:14:08 +0000
Subject: Bug #78667 [Opn->Nab]: Segmentation fault when using clone in jsonSerilization method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223191@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78667&edit=1 ID: 78667 Updated by: cmb@php.net Reported by: aparcos at gmail dot com Summary: Segmentation fault when using clone in jsonSerilization method -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related Operating System: Irrelevant PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Since the clone is also an instance of JsonSerializable, the code causes infinite recursion, similar to function foo() { foo(); } foo(); PHP deliberately does not catch this, but rather considers it to be a programming error[1]. You can use Xdebug[2] to catch such issues during development. You can use something like get_object_vars()[3] instead of clone to avoid recursion in the first place. [1] <https://www.php.net/manual/en/functions.user-defined.php> [2] <https://xdebug.org/docs/basic#max_nesting_level> [3] <https://www.php.net/manual/en/function.get-object-vars.php> Previous Comments: ------------------------------------------------------------------------ [2019-10-13 07:40:05] aparcos at gmail dot com Changed affected OS & Package ------------------------------------------------------------------------ [2019-10-12 21:44:49] aparcos at gmail dot com Description: ------------ A segmentation fault is going to happen when you accidentally uses clone inside the jsonSerialize method that implements JsonSerializable and return the new cloned object. If you uncomment the following test script, you will see that is going to be called several times until the recursive call get exhausted and finish given the segmentation fault. Test script: --------------- <?php class CloneThis implements JsonSerializable { public $date; public function jsonSerialize() { $clone = clone $this; // var_dump($this); // if ($this->date instanceof DateTime) { // $clone->date = $this->date->format('Y.m.d'); // } return $clone; } } $variable = new CloneThis; $variable->date = new DateTime(); echo json_encode($variable); Expected result: ---------------- I think should be : 1. or the current date. 2. or An error where many recursive calls are done (maybe) Actual result: -------------- //////////////////////////////// ...strings removed object(CloneThis)#52273 (1) { ["date"]=> string(10) "2019.10.12" } object(CloneThis)#52274 (1) { ["date"]=> string(10) "2019.10.12" } object(CloneThis)#52275 (1) { ["date"]=> string(10) "2019.10.12" } object(CloneThis)#52276 (1) { ["date"]=> string(10) "2019.10.12" } object(CloneThis)#52277 (1) { ["date"]=> Segment violation ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78667&edit=1

« previous php.bugs (#223191) next »