[php-src] Issue #10166: The json_last_error function will not reset when using flags of JSON_THROW_ON_ERROR after using without flags of
JSON_THROW_ON_ERROR

From: Date: Mon, 26 Dec 2022 06:44:14 +0000
Subject: [php-src] Issue #10166: The json_last_error function will not reset when using flags of JSON_THROW_ON_ERROR after using without flags of
JSON_THROW_ON_ERROR
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243244@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/10166 Author: m3m0r7 ### Description The following code: ```php <?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 using flags of JSON_THROW_ON_ERROR. but when I turn off flags of JSON_THROW_ON_ERROR, and then JSON_G(error_code) = error_code is not called probably. ```c 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

« previous php.bugs (#243244) next »