Skip to content

Commit f9883fb

Browse files
committed
JSON throw exception when throw_on_error declare is enabled
1 parent 6e8e478 commit f9883fb

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
@@ -205,7 +205,8 @@ PHP_JSON_API int php_json_decode_ex(zval *return_value, const char *str, size_t
205205

206206
if (php_json_yyparse(&parser)) {
207207
php_json_error_code error_code = php_json_parser_error_code(&parser);
208-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
208+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
209+
(EG(current_execute_data)->prev_execute_data->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
209210
JSON_G(error_code) = error_code;
210211
} else {
211212
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(error_code), error_code);
@@ -238,7 +239,9 @@ PHP_FUNCTION(json_encode)
238239
encoder.max_depth = (int)depth;
239240
php_json_encode_zval(&buf, parameter, (int)options, &encoder);
240241

241-
if (!(options & PHP_JSON_THROW_ON_ERROR) || (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
242+
if ((!(options & PHP_JSON_THROW_ON_ERROR) &&
243+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0)
244+
|| (options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
242245
JSON_G(error_code) = encoder.error_code;
243246
if (encoder.error_code != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
244247
smart_str_free(&buf);
@@ -278,17 +281,19 @@ PHP_FUNCTION(json_decode)
278281
Z_PARAM_LONG(options)
279282
ZEND_PARSE_PARAMETERS_END();
280283

281-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
284+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
285+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
282286
JSON_G(error_code) = PHP_JSON_ERROR_NONE;
283287
}
284288

285289
if (!str_len) {
286-
if (!(options & PHP_JSON_THROW_ON_ERROR)) {
290+
if (!(options & PHP_JSON_THROW_ON_ERROR) &&
291+
(EX(prev_execute_data)->func->common.fn_flags & ZEND_ACC_THROW_WARNING) == 0) {
287292
JSON_G(error_code) = PHP_JSON_ERROR_SYNTAX;
288-
} else {
289-
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
293+
RETURN_NULL();
290294
}
291-
RETURN_NULL();
295+
zend_throw_exception(php_json_exception_ce, php_json_get_error_msg(PHP_JSON_ERROR_SYNTAX), PHP_JSON_ERROR_SYNTAX);
296+
RETURN_THROWS();
292297
}
293298

294299
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)