ZEND_BEGIN_ARG_INFO_EX(arginfo_json_encode, 0, 0, 1)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, options)
+ ZEND_ARG_INFO(0, depth)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
{
json_globals->encoder_depth = 0;
json_globals->error_code = 0;
+ json_globals->encode_max_depth = 0;
}
/* }}} */
}
}
+ if (JSON_G(encoder_depth) > JSON_G(encode_max_depth)) {
+ JSON_G(error_code) = PHP_JSON_ERROR_DEPTH;
+ }
--JSON_G(encoder_depth);
json_pretty_print_char(buf, options, '\n' TSRMLS_CC);
json_pretty_print_indent(buf, options TSRMLS_CC);
zval *parameter;
smart_str buf = {0};
long options = 0;
+ long depth = JSON_PARSER_DEFAULT_DEPTH;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", ¶meter, &options) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ll", ¶meter, &options, &depth) == FAILURE) {
return;
}
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
+ JSON_G(encode_max_depth) = depth;
+
php_json_encode(&buf, parameter, options TSRMLS_CC);
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
--- /dev/null
+--TEST--
+FR #62369 (Segfault on json_encode(deeply_nested_array)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+
+$array = array();
+for ($i=0; $i<550; $i++) {
+ $array = array($array);
+}
+
+json_encode($array, 0, 551);
+switch (json_last_error()) {
+ case JSON_ERROR_NONE:
+ echo 'OK'.PHP_EOL;
+ break;
+ case JSON_ERROR_DEPTH:
+ echo 'ERROR'.PHP_EOL;
+ break;
+}
+
+json_encode($array, 0, 540);
+switch (json_last_error()) {
+ case JSON_ERROR_NONE:
+ echo 'OK'.PHP_EOL;
+ break;
+ case JSON_ERROR_DEPTH:
+ echo 'ERROR'.PHP_EOL;
+ break;
+}
+--EXPECTF--
+OK
+ERROR