Skip to content

Commit 94e4c96

Browse files
committed
JSON throw exception when throw_on_error declare is enabled
1 parent 27442e8 commit 94e4c96

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

ext/json/json.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ PHP_JSON_API int php_json_decode_ex(zval *return_value, const char *str, size_t
201201

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

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

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

281285
if (!str_len) {
282-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
286+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
287+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
283288
JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
284-
} else {
285-
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
289+
RETURN_NULL();
286290
}
287-
RETURN_NULL();
291+
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
292+
RETURN_THROWS();
288293
}
289294

290295
if (depth <= 0) {
Lines changed: 49 additions & 0 deletions
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+
}
Lines changed: 53 additions & 0 deletions
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)