Skip to content

Commit d372b33

Browse files
committed
Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3: Improve JSON error handling Conflicts: ext/json/tests/bug54058.phpt ext/json/tests/bug61537.phpt
2 parents 9a86784 + 4662151 commit d372b33

12 files changed

+114
-59
lines changed

ext/json/json.c

+33-9
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
5555
ZEND_ARG_INFO(0, options)
5656
ZEND_END_ARG_INFO()
5757

58-
ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
58+
ZEND_BEGIN_ARG_INFO_EX(arginfo_json_last_error, 0, 0, 0)
59+
ZEND_ARG_INFO(0, as_string)
5960
ZEND_END_ARG_INFO()
6061
/* }}} */
6162

@@ -236,7 +237,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
236237

237238
if (myht && myht->nApplyCount > 1) {
238239
JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
239-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
240240
smart_str_appendl(buf, "null", 4);
241241
return;
242242
}
@@ -378,7 +378,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
378378
efree(tmp);
379379
} else {
380380
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
381-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", d);
382381
smart_str_appendc(buf, '0');
383382
}
384383
}
@@ -395,7 +394,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
395394
}
396395
if (ulen < 0) {
397396
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
398-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
399397
smart_str_appendl(buf, "null", 4);
400398
} else {
401399
smart_str_appendl(buf, "\"\"", 2);
@@ -527,7 +525,6 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio
527525

528526
if (myht && myht->nApplyCount > 1) {
529527
JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
530-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
531528
smart_str_appendl(buf, "null", 4);
532529
return;
533530
}
@@ -592,7 +589,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
592589
efree(d);
593590
} else {
594591
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
595-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", dbl);
596592
smart_str_appendc(buf, '0');
597593
}
598594
}
@@ -614,7 +610,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
614610

615611
default:
616612
JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
617-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported");
618613
smart_str_appendl(buf, "null", 4);
619614
break;
620615
}
@@ -754,11 +749,40 @@ static PHP_FUNCTION(json_decode)
754749
Returns the error code of the last json_decode(). */
755750
static PHP_FUNCTION(json_last_error)
756751
{
757-
if (zend_parse_parameters_none() == FAILURE) {
752+
zend_bool as_string = 0;
753+
754+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &as_string) == FAILURE) {
758755
return;
759756
}
760757

761-
RETURN_LONG(JSON_G(error_code));
758+
/* return error code (JSON_ERROR_* constants) */
759+
if (!as_string) {
760+
RETURN_LONG(JSON_G(error_code));
761+
}
762+
763+
/* return error message (for debugging purposes) */
764+
switch(JSON_G(error_code)) {
765+
case PHP_JSON_ERROR_NONE:
766+
RETURN_STRING("No error", 1);
767+
case PHP_JSON_ERROR_DEPTH:
768+
RETURN_STRING("Maximum stack depth exceeded", 1);
769+
case PHP_JSON_ERROR_STATE_MISMATCH:
770+
RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
771+
case PHP_JSON_ERROR_CTRL_CHAR:
772+
RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
773+
case PHP_JSON_ERROR_SYNTAX:
774+
RETURN_STRING("Syntax error", 1);
775+
case PHP_JSON_ERROR_UTF8:
776+
RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
777+
case PHP_JSON_ERROR_RECURSION:
778+
RETURN_STRING("Recursion detected", 1);
779+
case PHP_JSON_ERROR_INF_OR_NAN:
780+
RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
781+
case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
782+
RETURN_STRING("Type is not supported", 1);
783+
default:
784+
RETURN_STRING("Unknown error", 1);
785+
}
762786
}
763787
/* }}} */
764788

ext/json/tests/003.phpt

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ $a[] = &$a;
1010

1111
var_dump($a);
1212

13+
echo "\n";
14+
1315
var_dump(json_encode($a));
1416
var_dump(json_last_error());
17+
var_dump(json_last_error(true));
18+
19+
echo "\n";
1520

1621
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
1722
var_dump(json_last_error());
23+
var_dump(json_last_error(true));
1824

1925
echo "Done\n";
2026
?>
@@ -27,11 +33,11 @@ array(1) {
2733
}
2834
}
2935

30-
Warning: json_encode(): recursion detected in %s on line %d
3136
bool(false)
3237
int(6)
38+
string(%d) "Recursion detected"
3339

34-
Warning: json_encode(): recursion detected in %s on line %d
3540
string(8) "[[null]]"
3641
int(6)
42+
string(%d) "Recursion detected"
3743
Done

ext/json/tests/004.phpt

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ $a->prop = $a;
1010

1111
var_dump($a);
1212

13+
echo "\n";
14+
1315
var_dump(json_encode($a));
1416
var_dump(json_last_error());
17+
var_dump(json_last_error(true));
18+
19+
echo "\n";
1520

1621
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
1722
var_dump(json_last_error());
23+
var_dump(json_last_error(true));
1824

1925
echo "Done\n";
2026
?>
@@ -24,11 +30,11 @@ object(stdClass)#%d (1) {
2430
*RECURSION*
2531
}
2632

27-
Warning: json_encode(): recursion detected in %s on line %d
2833
bool(false)
2934
int(6)
35+
string(%d) "Recursion detected"
3036

31-
Warning: json_encode(): recursion detected in %s on line %d
3237
string(22) "{"prop":{"prop":null}}"
3338
int(6)
39+
string(%d) "Recursion detected"
3440
Done

ext/json/tests/bug43941.phpt

-8
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ echo "Done\n";
1414
?>
1515
--EXPECTF--
1616
string(5) ""abc""
17-
18-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
1917
bool(false)
20-
21-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
2218
string(4) "null"
23-
24-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
25-
26-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
2719
string(17) "[null,null,"abc"]"
2820
Done

ext/json/tests/bug53946.phpt

-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,5 @@ var_dump(json_encode("ab\xE0", JSON_UNESCAPED_UNICODE));
1212
--EXPECTF--
1313
string(156) ""latin 1234 -\/ russian \u043c\u0430\u043c\u0430 \u043c\u044b\u043b\u0430 \u0440\u0430\u043c\u0443 specialchars \u0002 \b \n U+1D11E >\ud834\udd1e<""
1414
string(100) ""latin 1234 -\/ russian мама мыла раму specialchars \u0002 \b \n U+1D11E >𝄞<""
15-
16-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
1715
bool(false)
18-
19-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
2016
bool(false)

ext/json/tests/bug54058.phpt

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,36 @@ $bad_utf8 = quoted_printable_decode('=B0');
99

1010
json_encode($bad_utf8);
1111
var_dump(json_last_error());
12+
var_dump(json_last_error(true));
1213

1314
$a = new stdclass;
1415
$a->foo = quoted_printable_decode('=B0');
1516
json_encode($a);
1617
var_dump(json_last_error());
18+
var_dump(json_last_error(true));
1719

1820
$b = new stdclass;
1921
$b->foo = $bad_utf8;
2022
$b->bar = 1;
2123
json_encode($b);
2224
var_dump(json_last_error());
25+
var_dump(json_last_error(true));
2326

2427
$c = array(
2528
'foo' => $bad_utf8,
2629
'bar' => 1
2730
);
2831
json_encode($c);
2932
var_dump(json_last_error());
33+
var_dump(json_last_error(true));
34+
3035
?>
3136
--EXPECTF--
32-
33-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3437
int(5)
35-
36-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
38+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
3739
int(5)
38-
39-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
40+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
4041
int(5)
41-
42-
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
42+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
4343
int(5)
44+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"

ext/json/tests/bug61537.phpt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
Bug #61537 (json_encode() incorrectly truncates/discards information)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$invalid_utf8 = "\x9f";
8+
9+
var_dump(json_encode($invalid_utf8));
10+
var_dump(json_last_error(), json_last_error(true));
11+
12+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
13+
var_dump(json_last_error(), json_last_error(true));
14+
15+
echo "\n";
16+
17+
$invalid_utf8 = "an invalid sequen\xce in the middle of a string";
18+
19+
var_dump(json_encode($invalid_utf8));
20+
var_dump(json_last_error(), json_last_error(true));
21+
22+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
23+
var_dump(json_last_error(), json_last_error(true));
24+
25+
?>
26+
--EXPECTF--
27+
bool(false)
28+
int(5)
29+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
30+
string(4) "null"
31+
int(5)
32+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
33+
34+
bool(false)
35+
int(5)
36+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
37+
string(4) "null"
38+
int(5)
39+
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"

ext/json/tests/bug61978.phpt

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@ class JsonTest2 implements JsonSerializable {
3131
$obj1 = new JsonTest1();
3232
var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR));
3333

34-
echo "\n==\n";
34+
echo "==\n";
3535

3636
$obj2 = new JsonTest2();
3737
var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR));
3838

3939
?>
4040
--EXPECTF--
41-
Warning: json_encode(): recursion detected in %s on line %d
4241
string(44) "{"test":"123","me":{"test":"123","me":null}}"
43-
4442
==
45-
46-
Warning: json_encode(): recursion detected in %s on line %d
4743
string(44) "{"test":"123","me":{"test":"123","me":null}}"

ext/json/tests/inf_nan_error.phpt

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,36 @@ $inf = INF;
88
var_dump($inf);
99

1010
var_dump(json_encode($inf));
11-
var_dump(json_last_error());
11+
var_dump(json_last_error(), json_last_error(true));
1212

1313
var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR));
14-
var_dump(json_last_error());
14+
var_dump(json_last_error(), json_last_error(true));
15+
16+
echo "\n";
1517

1618
$nan = NAN;
1719

1820
var_dump($nan);
1921

2022
var_dump(json_encode($nan));
21-
var_dump(json_last_error());
23+
var_dump(json_last_error(), json_last_error(true));
2224

2325
var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR));
24-
var_dump(json_last_error());
26+
var_dump(json_last_error(), json_last_error(true));
2527
?>
2628
--EXPECTF--
2729
float(INF)
28-
29-
Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d
3030
bool(false)
3131
int(7)
32-
33-
Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d
32+
string(34) "Inf and NaN cannot be JSON encoded"
3433
string(1) "0"
3534
int(7)
36-
float(NAN)
35+
string(34) "Inf and NaN cannot be JSON encoded"
3736

38-
Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
37+
float(NAN)
3938
bool(false)
4039
int(7)
41-
42-
Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
40+
string(34) "Inf and NaN cannot be JSON encoded"
4341
string(1) "0"
4442
int(7)
43+
string(34) "Inf and NaN cannot be JSON encoded"

ext/json/tests/json_encode_basic.phpt

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ string(4) "null"
150150
-- Iteration 25 --
151151
string(4) "null"
152152
-- Iteration 26 --
153-
154-
Warning: json_encode(): type is unsupported in %s on line %d
155153
bool(false)
156154
-- Iteration 27 --
157155
string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}"

ext/json/tests/unsupported_type_error.phpt

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ $resource = fopen(__FILE__, "r");
88
var_dump($resource);
99

1010
var_dump(json_encode($resource));
11-
var_dump(json_last_error());
11+
var_dump(json_last_error(), json_last_error(true));
1212

1313
var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR));
14-
var_dump(json_last_error());
14+
var_dump(json_last_error(), json_last_error(true));
1515

1616
?>
1717
--EXPECTF--
1818
resource(5) of type (stream)
19-
20-
Warning: json_encode(): type is unsupported in %s on line %d
2119
bool(false)
2220
int(8)
23-
24-
Warning: json_encode(): type is unsupported in %s on line %d
21+
string(21) "Type is not supported"
2522
string(4) "null"
2623
int(8)
24+
string(21) "Type is not supported"

main/php_version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
#define PHP_RELEASE_VERSION 5
66
#define PHP_EXTRA_VERSION "-dev"
77
#define PHP_VERSION "5.4.5-dev"
8-
#define PHP_VERSION_ID 50404
8+
#define PHP_VERSION_ID 50405

0 commit comments

Comments
 (0)