Skip to content

Commit 250393f

Browse files
committed
Revert JSON changes to PHP 5.3
This reverts the following commits: 9743246 4662151 84fe2cc This does not revert the JSON changes released in PHP 5.3.14.
1 parent 5f31c81 commit 250393f

12 files changed

+46
-195
lines changed

ext/json/JSON_parser.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ enum error_codes {
2424
PHP_JSON_ERROR_STATE_MISMATCH,
2525
PHP_JSON_ERROR_CTRL_CHAR,
2626
PHP_JSON_ERROR_SYNTAX,
27-
PHP_JSON_ERROR_UTF8,
28-
PHP_JSON_ERROR_RECURSION,
29-
PHP_JSON_ERROR_INF_OR_NAN,
30-
PHP_JSON_ERROR_UNSUPPORTED_TYPE
27+
PHP_JSON_ERROR_UTF8
3128
};
3229

3330
extern JSON_parser new_JSON_parser(int depth);

ext/json/json.c

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ static PHP_MINFO_FUNCTION(json);
3434
static PHP_FUNCTION(json_encode);
3535
static PHP_FUNCTION(json_decode);
3636
static PHP_FUNCTION(json_last_error);
37-
static PHP_FUNCTION(json_last_error_msg);
3837

3938
static const char digits[] = "0123456789abcdef";
4039

@@ -54,17 +53,13 @@ ZEND_END_ARG_INFO()
5453

5554
ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
5655
ZEND_END_ARG_INFO()
57-
58-
ZEND_BEGIN_ARG_INFO(arginfo_json_last_error_msg, 0)
59-
ZEND_END_ARG_INFO()
6056
/* }}} */
6157

6258
/* {{{ json_functions[] */
6359
static const function_entry json_functions[] = {
6460
PHP_FE(json_encode, arginfo_json_encode)
6561
PHP_FE(json_decode, arginfo_json_decode)
6662
PHP_FE(json_last_error, arginfo_json_last_error)
67-
PHP_FE(json_last_error_msg, arginfo_json_last_error_msg)
6863
PHP_FE_END
6964
};
7065
/* }}} */
@@ -86,9 +81,6 @@ static PHP_MINIT_FUNCTION(json)
8681
REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT);
8782
REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT);
8883
REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT);
89-
REGISTER_LONG_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION, CONST_CS | CONST_PERSISTENT);
90-
REGISTER_LONG_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN, CONST_CS | CONST_PERSISTENT);
91-
REGISTER_LONG_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE, CONST_CS | CONST_PERSISTENT);
9284

9385
return SUCCESS;
9486
}
@@ -189,7 +181,7 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
189181
}
190182

191183
if (myht && myht->nApplyCount > 1) {
192-
JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
184+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
193185
smart_str_appendl(buf, "null", 4);
194186
return;
195187
}
@@ -311,7 +303,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
311303
smart_str_appendl(buf, tmp, l);
312304
efree(tmp);
313305
} else {
314-
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
306+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d);
315307
smart_str_appendc(buf, '0');
316308
}
317309
}
@@ -329,6 +321,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
329321
}
330322
if (len < 0) {
331323
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
324+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
332325
smart_str_appendl(buf, "null", 4);
333326
} else {
334327
smart_str_appendl(buf, "\"\"", 2);
@@ -467,7 +460,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
467460
smart_str_appendl(buf, d, len);
468461
efree(d);
469462
} else {
470-
JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
463+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl);
471464
smart_str_appendc(buf, '0');
472465
}
473466
}
@@ -483,7 +476,7 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
483476
break;
484477

485478
default:
486-
JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
479+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null");
487480
smart_str_appendl(buf, "null", 4);
488481
break;
489482
}
@@ -577,7 +570,7 @@ static PHP_FUNCTION(json_encode)
577570

578571
php_json_encode(&buf, parameter, options TSRMLS_CC);
579572

580-
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) {
573+
if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && options ^ PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) {
581574
ZVAL_FALSE(return_value);
582575
} else {
583576
ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
@@ -611,7 +604,7 @@ static PHP_FUNCTION(json_decode)
611604
/* }}} */
612605

613606
/* {{{ proto int json_last_error()
614-
Returns the error code of the last json_encode() or json_decode() call. */
607+
Returns the error code of the last json_decode(). */
615608
static PHP_FUNCTION(json_last_error)
616609
{
617610
if (zend_parse_parameters_none() == FAILURE) {
@@ -622,40 +615,6 @@ static PHP_FUNCTION(json_last_error)
622615
}
623616
/* }}} */
624617

625-
/* {{{ proto string json_last_error_msg()
626-
Returns the error string of the last json_encode() or json_decode() call. */
627-
static PHP_FUNCTION(json_last_error_msg)
628-
{
629-
if (zend_parse_parameters_none() == FAILURE) {
630-
return;
631-
}
632-
633-
switch(JSON_G(error_code)) {
634-
case PHP_JSON_ERROR_NONE:
635-
RETURN_STRING("No error", 1);
636-
case PHP_JSON_ERROR_DEPTH:
637-
RETURN_STRING("Maximum stack depth exceeded", 1);
638-
case PHP_JSON_ERROR_STATE_MISMATCH:
639-
RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
640-
case PHP_JSON_ERROR_CTRL_CHAR:
641-
RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
642-
case PHP_JSON_ERROR_SYNTAX:
643-
RETURN_STRING("Syntax error", 1);
644-
case PHP_JSON_ERROR_UTF8:
645-
RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
646-
case PHP_JSON_ERROR_RECURSION:
647-
RETURN_STRING("Recursion detected", 1);
648-
case PHP_JSON_ERROR_INF_OR_NAN:
649-
RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
650-
case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
651-
RETURN_STRING("Type is not supported", 1);
652-
default:
653-
RETURN_STRING("Unknown error", 1);
654-
}
655-
656-
}
657-
/* }}} */
658-
659618
/*
660619
* Local variables:
661620
* tab-width: 4

ext/json/tests/003.phpt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ $a = array();
99
$a[] = &$a;
1010

1111
var_dump($a);
12-
13-
echo "\n";
14-
1512
var_dump(json_encode($a));
16-
var_dump(json_last_error(), json_last_error_msg());
1713

18-
echo "\n";
19-
20-
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
21-
var_dump(json_last_error(), json_last_error_msg());
14+
/* Break circular data structure to prevent memory leaks */
15+
unset($a[0]);
2216

2317
echo "Done\n";
2418
?>
@@ -31,11 +25,6 @@ array(1) {
3125
}
3226
}
3327

34-
bool(false)
35-
int(6)
36-
string(%d) "Recursion detected"
37-
28+
Warning: json_encode(): recursion detected in %s on line %d
3829
string(8) "[[null]]"
39-
int(6)
40-
string(%d) "Recursion detected"
4130
Done

ext/json/tests/004.phpt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@ $a = new stdclass;
99
$a->prop = $a;
1010

1111
var_dump($a);
12-
13-
echo "\n";
14-
1512
var_dump(json_encode($a));
16-
var_dump(json_last_error(), json_last_error_msg());
17-
18-
echo "\n";
19-
20-
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
21-
var_dump(json_last_error(), json_last_error_msg());
2213

2314
echo "Done\n";
2415
?>
@@ -28,11 +19,6 @@ object(stdClass)#%d (1) {
2819
*RECURSION*
2920
}
3021

31-
bool(false)
32-
int(6)
33-
string(%d) "Recursion detected"
34-
22+
Warning: json_encode(): recursion detected in %s on line %d
3523
string(22) "{"prop":{"prop":null}}"
36-
int(6)
37-
string(%d) "Recursion detected"
3824
Done

ext/json/tests/007.phpt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ json_last_error() tests
55
--FILE--
66
<?php
77
var_dump(json_decode("[1]"));
8-
var_dump(json_last_error(), json_last_error_msg());
8+
var_dump(json_last_error());
99
var_dump(json_decode("[[1]]", false, 2));
10-
var_dump(json_last_error(), json_last_error_msg());
10+
var_dump(json_last_error());
1111
var_dump(json_decode("[1}"));
12-
var_dump(json_last_error(), json_last_error_msg());
12+
var_dump(json_last_error());
1313
var_dump(json_decode('["' . chr(0) . 'abcd"]'));
14-
var_dump(json_last_error(), json_last_error_msg());
14+
var_dump(json_last_error());
1515
var_dump(json_decode("[1"));
16-
var_dump(json_last_error(), json_last_error_msg());
16+
var_dump(json_last_error());
1717

1818

1919
echo "Done\n";
@@ -24,17 +24,13 @@ array(1) {
2424
int(1)
2525
}
2626
int(0)
27-
string(8) "No error"
2827
NULL
2928
int(1)
30-
string(28) "Maximum stack depth exceeded"
3129
NULL
3230
int(2)
33-
string(42) "State mismatch (invalid or malformed JSON)"
3431
NULL
3532
int(3)
36-
string(53) "Control character error, possibly incorrectly encoded"
3733
NULL
3834
int(4)
39-
string(12) "Syntax error"
4035
Done
36+

ext/json/tests/bug54058.phpt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,35 @@ Bug #54058 (json_last_error() invalid UTF-8 produces wrong error)
88
$bad_utf8 = quoted_printable_decode('=B0');
99

1010
json_encode($bad_utf8);
11-
var_dump(json_last_error(), json_last_error_msg());
11+
var_dump(json_last_error());
1212

1313
$a = new stdclass;
1414
$a->foo = quoted_printable_decode('=B0');
1515
json_encode($a);
16-
var_dump(json_last_error(), json_last_error_msg());
16+
var_dump(json_last_error());
1717

1818
$b = new stdclass;
1919
$b->foo = $bad_utf8;
2020
$b->bar = 1;
2121
json_encode($b);
22-
var_dump(json_last_error(), json_last_error_msg());
22+
var_dump(json_last_error());
2323

2424
$c = array(
2525
'foo' => $bad_utf8,
2626
'bar' => 1
2727
);
2828
json_encode($c);
29-
var_dump(json_last_error(), json_last_error_msg());
30-
29+
var_dump(json_last_error());
3130
?>
3231
--EXPECTF--
32+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3333
int(5)
34-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
34+
35+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3536
int(5)
36-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
37+
38+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3739
int(5)
38-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
40+
41+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3942
int(5)
40-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"

ext/json/tests/bug61537.phpt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,26 @@ Bug #61537 (json_encode() incorrectly truncates/discards information)
55
--FILE--
66
<?php
77
$invalid_utf8 = "\x9f";
8-
9-
var_dump(json_encode($invalid_utf8));
10-
var_dump(json_last_error(), json_last_error_msg());
11-
12-
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
13-
var_dump(json_last_error(), json_last_error_msg());
14-
15-
echo "\n";
8+
var_dump(json_encode($invalid_utf8), json_last_error());
9+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
1610

1711
$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_msg());
21-
22-
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
23-
var_dump(json_last_error(), json_last_error_msg());
24-
12+
var_dump(json_encode($invalid_utf8), json_last_error());
13+
var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
2514
?>
2615
--EXPECTF--
16+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
2717
bool(false)
2818
int(5)
29-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
19+
20+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3021
string(4) "null"
3122
int(5)
32-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
3323

24+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3425
bool(false)
3526
int(5)
36-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
27+
28+
Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
3729
string(4) "null"
3830
int(5)
39-
string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"

ext/json/tests/inf_nan_error.phpt

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)