Introduce json encoder to fix globals related issues

It fixes bugs #66025 and #73254 by replacing globals with
a passed structure holding depth and error code. In addition
it fixes #72069 in a more generic way.
This commit is contained in:
Jakub Zelenka 2016-10-30 13:20:10 +00:00
parent be6bf71274
commit c34de0b61c
6 changed files with 120 additions and 47 deletions

View file

@ -186,7 +186,17 @@ static PHP_MINFO_FUNCTION(json)
PHP_JSON_API int php_json_encode(smart_str *buf, zval *val, int options) /* {{{ */
{
return php_json_encode_zval(buf, val, options);
php_json_encoder encoder;
int return_code;
php_json_encode_init(&encoder);
encoder.max_depth = JSON_G(encode_max_depth);
encoder.error_code = PHP_JSON_ERROR_NONE;
return_code = php_json_encode_zval(buf, val, options, &encoder);
JSON_G(error_code) = encoder.error_code;
return return_code;
}
/* }}} */
@ -211,6 +221,7 @@ PHP_JSON_API int php_json_decode_ex(zval *return_value, char *str, size_t str_le
static PHP_FUNCTION(json_encode)
{
zval *parameter;
php_json_encoder encoder;
smart_str buf = {0};
zend_long options = 0;
zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
@ -219,22 +230,22 @@ static PHP_FUNCTION(json_encode)
return;
}
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
php_json_encode_init(&encoder);
encoder.max_depth = (int)depth;
encoder.error_code = PHP_JSON_ERROR_NONE;
php_json_encode_zval(&buf, parameter, (int)options, &encoder);
JSON_G(error_code) = encoder.error_code;
JSON_G(encode_max_depth) = (int)depth;
php_json_encode(&buf, parameter, (int)options);
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
smart_str_free(&buf);
ZVAL_FALSE(return_value);
} else {
smart_str_0(&buf); /* copy? */
if (buf.s) {
RETURN_NEW_STR(buf.s);
}
RETURN_EMPTY_STRING();
RETURN_FALSE;
}
smart_str_0(&buf); /* copy? */
if (buf.s) {
RETURN_NEW_STR(buf.s);
}
RETURN_EMPTY_STRING();
}
/* }}} */