@@ -47,6 +47,7 @@ ZEND_DECLARE_MODULE_GLOBALS(json)
4747ZEND_BEGIN_ARG_INFO_EX (arginfo_json_encode , 0 , 0 , 1 )
4848 ZEND_ARG_INFO (0 , value )
4949 ZEND_ARG_INFO (0 , options )
50+ ZEND_ARG_INFO (0 , depth )
5051ZEND_END_ARG_INFO ()
5152
5253ZEND_BEGIN_ARG_INFO_EX (arginfo_json_decode , 0 , 0 , 1 )
@@ -126,6 +127,7 @@ static PHP_GINIT_FUNCTION(json)
126127{
127128 json_globals -> encoder_depth = 0 ;
128129 json_globals -> error_code = 0 ;
130+ json_globals -> encode_max_depth = 0 ;
129131}
130132/* }}} */
131133
@@ -341,6 +343,9 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
341343 }
342344 }
343345
346+ if (JSON_G (encoder_depth ) > JSON_G (encode_max_depth )) {
347+ JSON_G (error_code ) = PHP_JSON_ERROR_DEPTH ;
348+ }
344349 -- JSON_G (encoder_depth );
345350 json_pretty_print_char (buf , options , '\n' TSRMLS_CC );
346351 json_pretty_print_indent (buf , options TSRMLS_CC );
@@ -702,13 +707,16 @@ static PHP_FUNCTION(json_encode)
702707 zval * parameter ;
703708 smart_str buf = {0 };
704709 long options = 0 ;
710+ long depth = JSON_PARSER_DEFAULT_DEPTH ;
705711
706- if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "z|l " , & parameter , & options ) == FAILURE ) {
712+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "z|ll " , & parameter , & options , & depth ) == FAILURE ) {
707713 return ;
708714 }
709715
710716 JSON_G (error_code ) = PHP_JSON_ERROR_NONE ;
711717
718+ JSON_G (encode_max_depth ) = depth ;
719+
712720 php_json_encode (& buf , parameter , options TSRMLS_CC );
713721
714722 if (JSON_G (error_code ) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR )) {
0 commit comments