php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62369 Segfault on json_encode(deeply_nested_array);
Submitted: 2012-06-20 09:10 UTC Modified: 2013-08-30 13:02 UTC
From: arjen at react dot com Assigned: fa (profile)
Status: Closed Package: JSON related
PHP Version: 5.4.4 OS: CENTOS
Private report: No CVE-ID: None
 [2012-06-20 09:10 UTC] arjen at react dot com
Description:
------------
Trying to json_encode a 50.000 levels deep nested array causes a segfault.

Segfault occurs in PHP versions 5.2.0 - 5.2.17, 5.3.0 - 5.3.14, 5.4.0 - 5.4.4, 
see http://3v4l.org/uZOgV

Found while trying to construct a testcase for json_last_error() == 
JSON_ERROR_DEPTH

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

$array = array();

for ($i = 0; $i < 50000; $i++)
	$array = array($array);

json_encode($array);

Expected result:
----------------
Correct result or 0 with json_last_error() == JSON_ERROR_DEPTH

Actual result:
--------------
#0  0x00007fffed1e2d59 in json_determine_array_type (buf=0x7fffffffba50, 
val=0xfa5ea8, options=0) at /home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:139
#1  json_encode_array (buf=0x7fffffffba50, val=0xfa5ea8, options=0) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:176
#2  php_json_encode (buf=0x7fffffffba50, val=0xfa5ea8, options=0) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:476
#3  0x00007fffed1e32df in json_encode_array (buf=<value optimized out>, val=
<value optimized out>, options=<value optimized out>) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:258
#4  php_json_encode (buf=<value optimized out>, val=<value optimized out>, 
options=<value optimized out>) at /home/edwin/rpm/BUILD/php-
5.3.13/ext/json/json.c:476
#5  0x00007fffed1e32df in json_encode_array (buf=<value optimized out>, val=
<value optimized out>, options=<value optimized out>) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:258
#6  php_json_encode (buf=<value optimized out>, val=<value optimized out>, 
options=<value optimized out>) at /home/edwin/rpm/BUILD/php-
5.3.13/ext/json/json.c:476
#7  0x00007fffed1e32df in json_encode_array (buf=<value optimized out>, val=
<value optimized out>, options=<value optimized out>) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:258
#8  php_json_encode (buf=<value optimized out>, val=<value optimized out>, 
options=<value optimized out>) at /home/edwin/rpm/BUILD/php-
5.3.13/ext/json/json.c:476
#9  0x00007fffed1e32df in json_encode_array (buf=<value optimized out>, val=
<value optimized out>, options=<value optimized out>) at 
/home/edwin/rpm/BUILD/php-5.3.13/ext/json/json.c:258
#10 php_json_encode (buf=<value optimized out>, val=<value optimized out>, 
options=<value optimized out>) at /home/edwin/rpm/BUILD/php-
5.3.13/ext/json/json.c:476
[line 9/10 repeated]

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-21 08:01 UTC] laruence@php.net
stack overflow... it's kind of no bug...  anyway maybe we can introduce a new 
parameter `max_depth` to json_encode too.
 [2012-06-25 06:10 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2012-06-25 06:10 UTC] laruence@php.net
change to FR: add a max_depth limitation to json_encode
 [2012-06-27 13:12 UTC] felipe@php.net
-Package: json +Package: JSON related
 [2012-07-24 11:02 UTC] fa@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: fa
 [2012-07-24 12:42 UTC] fa@php.net
-Type: Feature/Change Request +Type: Documentation Problem
 [2012-07-24 12:42 UTC] fa@php.net
Added in master, we'll see if it gets into 5.4.

Needs to be documented.

Thanks!
 [2013-08-30 13:02 UTC] maarten@php.net
-Status: Assigned +Status: Closed
 [2013-08-30 13:02 UTC] maarten@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

$depth parameter (with rather safe default) was added to json_encode() in PHP 5.5. :)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Dec 01 04:01:25 2020 UTC