php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71835 json_encode sometimes incorrectly detects recursion with JsonSerializable
Submitted: 2016-03-16 02:00 UTC Modified: 2016-03-17 20:00 UTC
From: icarpenter at leadid dot com Assigned: bukka (profile)
Status: Closed Package: JSON related
PHP Version: 7.0.4 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: icarpenter at leadid dot com
New email:
PHP Version: OS:

 

 [2016-03-16 02:00 UTC] icarpenter at leadid dot com
Description:
------------
If a class that implements JsonSerializable returns an array of get_object_vars($this) in its jsonSerialize method, and if that class is in an array that is json_encode'd, JSON_ERROR_RECURSION is returned.

This does not seem to happen if jsonSerialize returns get_object_vars($this) not enclosed an array, nor does it seem to happen if the object itself is json_encode'd

Test script:
---------------
https://3v4l.org/OoR7D

Expected result:
----------------
string(4) "[[]]"
bool(false)
string(6) "[[[]]]"
bool(false)
string(2) "[]"
bool(false)
string(4) "[[]]"
bool(false)

Actual result:
--------------
string(4) "[[]]"
bool(false)
bool(false)
bool(true)
string(2) "[]"
bool(false)
string(4) "[[]]"
bool(false)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-16 02:59 UTC] laruence@php.net
hmm, the problem is get_object_vars maybe return directly object->properties, which make it looks like a recursive array ,
 [2016-03-16 02:59 UTC] laruence@php.net
-Status: Open +Status: Analyzed
 [2016-03-16 13:01 UTC] laruence@php.net
-Assigned To: +Assigned To: bukka
 [2016-03-16 13:01 UTC] laruence@php.net
Hmm, I can not think out of a good fix for this, @bukka, what do you think?
 [2016-03-17 20:00 UTC] bukka@php.net
@laruence yeah that's a tricky one. I will think about it a bit more during this or next weekend.
 [2016-03-21 08:22 UTC] laruence@php.net
Automatic comment on behalf of bukka
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7e069daa89f05c1fa4127b2cedfd5586dbfc810e
Log: Fix bug #71835 (json_encode sometimes incorrectly detects recursion with JsonSerializable)
 [2016-03-21 08:22 UTC] laruence@php.net
-Status: Analyzed +Status: Closed
 [2016-07-20 11:32 UTC] davey@php.net
Automatic comment on behalf of bukka
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7e069daa89f05c1fa4127b2cedfd5586dbfc810e
Log: Fix bug #71835 (json_encode sometimes incorrectly detects recursion with JsonSerializable)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Dec 03 14:01:27 2020 UTC