Skip to content

Reset JSON_G(error_code) after calls with JSON_THROW_ON_ERROR #10166

@m3m0r7

Description

@m3m0r7

Description

The following code:

<?php
json_decode("\00invalid json");
var_dump(json_last_error());

json_decode("[]", true, 512, JSON_THROW_ON_ERROR);
var_dump(json_last_error());

Resulted in this output:

int(3)
int(3)

But I expected this output instead:

int(3)
int(0)

In json.c file, the function calls JSON_G(error_code) = error_code when I turn off flags of JSON_THROW_ON_ERROR.
but when I turn on flags of JSON_THROW_ON_ERROR, and then JSON_G(error_code) = error_code is not called probably.

PHP_JSON_API zend_result php_json_decode_ex(zval *return_value, const char *str, size_t str_len, zend_long options, zend_long depth) /* {{{ */
{
	php_json_parser parser;

	php_json_parser_init(&parser, return_value, str, str_len, (int)options, (int)depth);

	if (php_json_yyparse(&parser)) {
		php_json_error_code error_code = php_json_parser_error_code(&parser);
		if (!(options & PHP_JSON_THROW_ON_ERROR)) {
			JSON_G(error_code) = error_code; // <--- here
		} else {
			zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code);
		}
		RETVAL_NULL();
		return FAILURE;
	}

	return SUCCESS;
}

PHP Version

PHP 8.1

Operating System

mac

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions