Skip to content

Commit 42f599a

Browse files
committed
JSON throw exception when throw_on_error declare is enabled
1 parent f7b95fe commit 42f599a

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

ext/json/json.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ PHP_JSON_API int php_json_decode_ex(zval *return_value, const char *str, size_t
200200

201201
if (php_json_yyparse(&parser)) {
202202
php_json_error_code error_code = php_json_parser_error_code(&parser);
203-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
203+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
204+
(EG(current_execute_data)->prev_execute_data->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
204205
JSON_G(error_code) = error_code;
205206
} else {
206207
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code);
@@ -233,7 +234,9 @@ PHP_FUNCTION(json_encode)
233234
encoder.max_depth = (int)depth;
234235
php_json_encode_zval(&buf, parameter, (int)options, &encoder);
235236

236-
if (!(options & PHP_JSON_THROW_ON_ERROR) || (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
237+
if ((!(options & PHP_JSON_THROW_ON_ERROR) &&
238+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0)
239+
|| (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
237240
JSON_G(error_code) = encoder.error_code;
238241
if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
239242
smart_str_free(&buf);
@@ -273,17 +276,19 @@ PHP_FUNCTION(json_decode)
273276
Z_PARAM_LONG(options)
274277
ZEND_PARSE_PARAMETERS_END();
275278

276-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
279+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
280+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
277281
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
278282
}
279283

280284
if (!str_len) {
281-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
285+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
286+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
282287
JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
283-
} else {
284-
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
288+
RETURN_NULL();
285289
}
286-
RETURN_NULL();
290+
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
291+
RETURN_THROWS();
287292
}
288293

289294
if (depth <= 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
Test json_decode() function when throw_on_error declare is present
3+
--FILE--
4+
<?php
5+
declare(throw_on_error=1);
6+
7+
try {
8+
var_dump(json_decode("{", false, 512));
9+
} catch (JsonException $e) {
10+
var_dump($e);
11+
}
12+
13+
?>
14+
--EXPECTF--
15+
object(JsonException)#1 (7) {
16+
["message":protected]=>
17+
string(12) "Syntax error"
18+
["string":"Exception":private]=>
19+
string(0) ""
20+
["code":protected]=>
21+
int(4)
22+
["file":protected]=>
23+
string(%d) "%s"
24+
["line":protected]=>
25+
int(%d)
26+
["trace":"Exception":private]=>
27+
array(1) {
28+
[0]=>
29+
array(4) {
30+
["file"]=>
31+
string(%d) "%s"
32+
["line"]=>
33+
int(%d)
34+
["function"]=>
35+
string(11) "json_decode"
36+
["args"]=>
37+
array(3) {
38+
[0]=>
39+
string(1) "{"
40+
[1]=>
41+
bool(false)
42+
[2]=>
43+
int(512)
44+
}
45+
}
46+
}
47+
["previous":"Exception":private]=>
48+
NULL
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Test json_encode() function when throw_on_error declare is present
3+
--FILE--
4+
<?php
5+
declare(throw_on_error=1);
6+
7+
try {
8+
var_dump(json_encode("\x80"));
9+
} catch (JsonException $e) {
10+
var_dump($e);
11+
}
12+
13+
// JSON_PARTIAL_OUTPUT_ON_ERROR is incompatible with exceptions
14+
var_dump(json_encode("\x80",JSON_PARTIAL_OUTPUT_ON_ERROR));
15+
var_dump(json_last_error());
16+
var_dump(json_last_error_msg());
17+
18+
?>
19+
--EXPECTF--
20+
object(JsonException)#1 (7) {
21+
["message":protected]=>
22+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
23+
["string":"Exception":private]=>
24+
string(0) ""
25+
["code":protected]=>
26+
int(5)
27+
["file":protected]=>
28+
string(%d) "%s"
29+
["line":protected]=>
30+
int(%d)
31+
["trace":"Exception":private]=>
32+
array(1) {
33+
[0]=>
34+
array(4) {
35+
["file"]=>
36+
string(%d) "%s"
37+
["line"]=>
38+
int(%d)
39+
["function"]=>
40+
string(11) "json_encode"
41+
["args"]=>
42+
array(1) {
43+
[0]=>
44+
string(1) "%s"
45+
}
46+
}
47+
}
48+
["previous":"Exception":private]=>
49+
NULL
50+
}
51+
string(4) "null"
52+
int(5)
53+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"

0 commit comments

Comments
 (0)