php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66025 Indent wrong when json_encode() called from jsonSerialize function
Submitted: 2013-11-03 13:19 UTC Modified: 2016-10-30 13:29 UTC
Votes:6
Avg. Score:4.5 ± 0.5
Reproduced:6 of 6 (100.0%)
Same Version:2 (33.3%)
Same OS:1 (16.7%)
From: pleasestand at live dot com Assigned: bukka (profile)
Status: Closed Package: JSON related
PHP Version: master-Git-2013-11-03 (Git) OS:
Private report: No CVE-ID: None
 [2013-11-03 13:19 UTC] pleasestand at live dot com
Description:
------------
This is because the current indent is stored in a variable JSON_G(encoder_depth), which is shared between json_encode() calls.

Test script:
---------------
<?php

class Foo implements JsonSerializable {
    public function jsonSerialize() {
        return json_encode([1], JSON_PRETTY_PRINT);
    }
}

echo json_encode([new Foo]), "\n";

Expected result:
----------------
["[\n    1\n]"]

Actual result:
--------------
["[\n        1\n    ]"]

Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-13 01:45 UTC] encryptio at gmail dot com
I'm hitting this in production at my workplace; specifically, when json_encode causes the interpereter to run out of memory and abort requests in mod_php, php_json_encode_array doesn't have a chance to decrement the encoder_depth variable, leaving a thread-local inconsistency in the interpereter.
 [2016-05-15 14:53 UTC] bukka@php.net
-Assigned To: +Assigned To: bukka
 [2016-05-15 14:53 UTC] bukka@php.net
The bug is caused by the fact that globals are used for encoder depth. I plan to introduce a separate context for each encoder run which will fix this and couple of others bugs related to globals in json encoding. This won't happen before 7.1
 [2016-10-30 13:29 UTC] bukka@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC