diff options
author | Nikita Popov <nikic@php.net> | 2012-06-23 21:14:45 +0200 |
---|---|---|
committer | Nikita Popov <nikic@php.net> | 2012-06-23 21:14:45 +0200 |
commit | 36fa17a5fae84ab332366a202f0a709279a2466a (patch) | |
tree | 409c529b90ee598cfcc04357b9d4904735c89edd /ext/json | |
parent | e8862725770c3d5ee27c6c8c8a01b226b610fa08 (diff) | |
parent | 84fe2cc890e49f40bac7c3ba74b3cfc6dc4cef2f (diff) | |
download | php-git-36fa17a5fae84ab332366a202f0a709279a2466a.tar.gz |
Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
Improve json_encode error handling
- BFN
Conflicts:
ext/json/json.c
Diffstat (limited to 'ext/json')
-rw-r--r-- | ext/json/JSON_parser.h | 5 | ||||
-rw-r--r-- | ext/json/json.c | 25 | ||||
-rw-r--r-- | ext/json/php_json.h | 1 | ||||
-rw-r--r-- | ext/json/tests/003.phpt | 11 | ||||
-rw-r--r-- | ext/json/tests/004.phpt | 10 | ||||
-rw-r--r-- | ext/json/tests/bug43941.phpt | 15 | ||||
-rw-r--r-- | ext/json/tests/bug53946.phpt | 10 | ||||
-rw-r--r-- | ext/json/tests/bug54058.phpt | 8 | ||||
-rw-r--r-- | ext/json/tests/bug61978.phpt | 4 | ||||
-rw-r--r-- | ext/json/tests/inf_nan_error.phpt | 44 | ||||
-rw-r--r-- | ext/json/tests/json_encode_basic.phpt | 6 | ||||
-rw-r--r-- | ext/json/tests/pass001.1.phpt | 4 | ||||
-rw-r--r-- | ext/json/tests/pass001.phpt | 4 | ||||
-rw-r--r-- | ext/json/tests/unsupported_type_error.phpt | 26 |
14 files changed, 147 insertions, 26 deletions
diff --git a/ext/json/JSON_parser.h b/ext/json/JSON_parser.h index 541664b8c6..8671765b4d 100644 --- a/ext/json/JSON_parser.h +++ b/ext/json/JSON_parser.h @@ -25,7 +25,10 @@ enum error_codes { PHP_JSON_ERROR_STATE_MISMATCH, PHP_JSON_ERROR_CTRL_CHAR, PHP_JSON_ERROR_SYNTAX, - PHP_JSON_ERROR_UTF8 + PHP_JSON_ERROR_UTF8, + PHP_JSON_ERROR_RECURSION, + PHP_JSON_ERROR_INF_OR_NAN, + PHP_JSON_ERROR_UNSUPPORTED_TYPE }; extern JSON_parser new_JSON_parser(int depth); diff --git a/ext/json/json.c b/ext/json/json.c index 557fbc3e0c..bc30251430 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -96,6 +96,7 @@ static PHP_MINIT_FUNCTION(json) REGISTER_LONG_CONSTANT("JSON_UNESCAPED_SLASHES", PHP_JSON_UNESCAPED_SLASHES, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_PRETTY_PRINT", PHP_JSON_PRETTY_PRINT, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_UNESCAPED_UNICODE", PHP_JSON_UNESCAPED_UNICODE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_PARTIAL_OUTPUT_ON_ERROR", PHP_JSON_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_ERROR_NONE", PHP_JSON_ERROR_NONE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_ERROR_DEPTH", PHP_JSON_ERROR_DEPTH, CONST_CS | CONST_PERSISTENT); @@ -103,6 +104,9 @@ static PHP_MINIT_FUNCTION(json) REGISTER_LONG_CONSTANT("JSON_ERROR_CTRL_CHAR", PHP_JSON_ERROR_CTRL_CHAR, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_ERROR_SYNTAX", PHP_JSON_ERROR_SYNTAX, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_ERROR_UTF8", PHP_JSON_ERROR_UTF8, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_ERROR_RECURSION", PHP_JSON_ERROR_RECURSION, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_ERROR_INF_OR_NAN", PHP_JSON_ERROR_INF_OR_NAN, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("JSON_ERROR_UNSUPPORTED_TYPE", PHP_JSON_ERROR_UNSUPPORTED_TYPE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_OBJECT_AS_ARRAY", PHP_JSON_OBJECT_AS_ARRAY, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("JSON_BIGINT_AS_STRING", PHP_JSON_BIGINT_AS_STRING, CONST_CS | CONST_PERSISTENT); @@ -231,6 +235,7 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC) } if (myht && myht->nApplyCount > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected"); smart_str_appendl(buf, "null", 4); return; @@ -372,7 +377,8 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR smart_str_appendl(buf, tmp, l); efree(tmp); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", d); + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", d); smart_str_appendc(buf, '0'); } } @@ -389,9 +395,7 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR } if (ulen < 0) { JSON_G(error_code) = PHP_JSON_ERROR_UTF8; - if (!PG(display_errors)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); - } + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument"); smart_str_appendl(buf, "null", 4); } else { smart_str_appendl(buf, "\"\"", 2); @@ -522,6 +526,7 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio } if (myht && myht->nApplyCount > 1) { + JSON_G(error_code) = PHP_JSON_ERROR_RECURSION; php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected"); smart_str_appendl(buf, "null", 4); return; @@ -586,7 +591,8 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_ smart_str_appendl(buf, d, len); efree(d); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0", dbl); + JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", dbl); smart_str_appendc(buf, '0'); } } @@ -607,7 +613,8 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_ break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported, encoded as null"); + JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE; + php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported"); smart_str_appendl(buf, "null", 4); break; } @@ -702,7 +709,11 @@ static PHP_FUNCTION(json_encode) php_json_encode(&buf, parameter, options TSRMLS_CC); - ZVAL_STRINGL(return_value, buf.c, buf.len, 1); + if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && !(options & PHP_JSON_PARTIAL_OUTPUT_ON_ERROR)) { + ZVAL_FALSE(return_value); + } else { + ZVAL_STRINGL(return_value, buf.c, buf.len, 1); + } smart_str_free(&buf); } diff --git a/ext/json/php_json.h b/ext/json/php_json.h index ef3e4b5a79..afeff3f6cc 100644 --- a/ext/json/php_json.h +++ b/ext/json/php_json.h @@ -63,6 +63,7 @@ extern zend_class_entry *php_json_serializable_ce; #define PHP_JSON_UNESCAPED_SLASHES (1<<6) #define PHP_JSON_PRETTY_PRINT (1<<7) #define PHP_JSON_UNESCAPED_UNICODE (1<<8) +#define PHP_JSON_PARTIAL_OUTPUT_ON_ERROR (1<<9) /* Internal flags */ #define PHP_JSON_OUTPUT_ARRAY 0 diff --git a/ext/json/tests/003.phpt b/ext/json/tests/003.phpt index 3b52fb0884..ab63711008 100644 --- a/ext/json/tests/003.phpt +++ b/ext/json/tests/003.phpt @@ -9,10 +9,12 @@ $a = array(); $a[] = &$a; var_dump($a); + var_dump(json_encode($a)); +var_dump(json_last_error()); -/* Break circular data structure to prevent memory leaks */ -unset($a[0]); +var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_last_error()); echo "Done\n"; ?> @@ -26,5 +28,10 @@ array(1) { } Warning: json_encode(): recursion detected in %s on line %d +bool(false) +int(6) + +Warning: json_encode(): recursion detected in %s on line %d string(8) "[[null]]" +int(6) Done diff --git a/ext/json/tests/004.phpt b/ext/json/tests/004.phpt index 1d282f9a96..9f9abfe46a 100644 --- a/ext/json/tests/004.phpt +++ b/ext/json/tests/004.phpt @@ -9,7 +9,12 @@ $a = new stdclass; $a->prop = $a; var_dump($a); + var_dump(json_encode($a)); +var_dump(json_last_error()); + +var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_last_error()); echo "Done\n"; ?> @@ -20,5 +25,10 @@ object(stdClass)#%d (1) { } Warning: json_encode(): recursion detected in %s on line %d +bool(false) +int(6) + +Warning: json_encode(): recursion detected in %s on line %d string(22) "{"prop":{"prop":null}}" +int(6) Done diff --git a/ext/json/tests/bug43941.phpt b/ext/json/tests/bug43941.phpt index 0f86d1dfad..fb59b71c96 100644 --- a/ext/json/tests/bug43941.phpt +++ b/ext/json/tests/bug43941.phpt @@ -7,15 +7,22 @@ Bug #43941 (json_encode() invalid UTF-8) var_dump(json_encode("abc")); var_dump(json_encode("ab\xE0")); -var_dump(json_encode("ab\xE0c")); -var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc"))); +var_dump(json_encode("ab\xE0", JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc"), JSON_PARTIAL_OUTPUT_ON_ERROR)); echo "Done\n"; ?> --EXPECTF-- string(5) ""abc"" + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d +bool(false) + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d string(4) "null" -string(4) "null" + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d string(17) "[null,null,"abc"]" Done - diff --git a/ext/json/tests/bug53946.phpt b/ext/json/tests/bug53946.phpt index abbb81238b..079906fa33 100644 --- a/ext/json/tests/bug53946.phpt +++ b/ext/json/tests/bug53946.phpt @@ -9,8 +9,12 @@ var_dump(json_encode("latin 1234 -/ russian мама мыла раму speci var_dump(json_encode("ab\xE0")); var_dump(json_encode("ab\xE0", JSON_UNESCAPED_UNICODE)); ?> ---EXPECT-- +--EXPECTF-- 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<"" string(100) ""latin 1234 -\/ russian мама мыла раму specialchars \u0002 \b \n U+1D11E >𝄞<"" -string(4) "null" -string(4) "null" + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d +bool(false) + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d +bool(false) diff --git a/ext/json/tests/bug54058.phpt b/ext/json/tests/bug54058.phpt index 3b1136bdd9..e3596eb62b 100644 --- a/ext/json/tests/bug54058.phpt +++ b/ext/json/tests/bug54058.phpt @@ -29,7 +29,15 @@ json_encode($c); var_dump(json_last_error()); ?> --EXPECTF-- + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d int(5) + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d int(5) + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d int(5) + +Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d int(5) diff --git a/ext/json/tests/bug61978.phpt b/ext/json/tests/bug61978.phpt index 2c732979ef..4c863c689e 100644 --- a/ext/json/tests/bug61978.phpt +++ b/ext/json/tests/bug61978.phpt @@ -29,12 +29,12 @@ class JsonTest2 implements JsonSerializable { $obj1 = new JsonTest1(); -var_dump(json_encode($obj1)); +var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR)); echo "\n==\n"; $obj2 = new JsonTest2(); -var_dump(json_encode($obj2)); +var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR)); ?> --EXPECTF-- diff --git a/ext/json/tests/inf_nan_error.phpt b/ext/json/tests/inf_nan_error.phpt new file mode 100644 index 0000000000..a3ed5e7b88 --- /dev/null +++ b/ext/json/tests/inf_nan_error.phpt @@ -0,0 +1,44 @@ +--TEST-- +An error is thrown when INF or NaN are encoded +--FILE-- +<?php + +$inf = INF; + +var_dump($inf); + +var_dump(json_encode($inf)); +var_dump(json_last_error()); + +var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_last_error()); + +$nan = NAN; + +var_dump($nan); + +var_dump(json_encode($nan)); +var_dump(json_last_error()); + +var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_last_error()); +?> +--EXPECTF-- +float(INF) + +Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d +bool(false) +int(7) + +Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d +string(1) "0" +int(7) +float(NAN) + +Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d +bool(false) +int(7) + +Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d +string(1) "0" +int(7) diff --git a/ext/json/tests/json_encode_basic.phpt b/ext/json/tests/json_encode_basic.phpt index 152e24444c..7ee68c58ca 100644 --- a/ext/json/tests/json_encode_basic.phpt +++ b/ext/json/tests/json_encode_basic.phpt @@ -151,8 +151,8 @@ string(4) "null" string(4) "null" -- Iteration 26 -- -Warning: json_encode(): type is unsupported, encoded as null in %s on line %d -string(4) "null" +Warning: json_encode(): type is unsupported in %s on line %d +bool(false) -- Iteration 27 -- string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}" -===Done===
\ No newline at end of file +===Done=== diff --git a/ext/json/tests/pass001.1.phpt b/ext/json/tests/pass001.1.phpt index 7e15a7622a..a51f885780 100644 --- a/ext/json/tests/pass001.1.phpt +++ b/ext/json/tests/pass001.1.phpt @@ -90,10 +90,10 @@ $arr = json_decode($test, true); var_dump($arr); echo "ENCODE: FROM OBJECT\n"; -$obj_enc = json_encode($obj); +$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR); echo $obj_enc . "\n"; echo "ENCODE: FROM ARRAY\n"; -$arr_enc = json_encode($arr); +$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR); echo $arr_enc . "\n"; echo "DECODE AGAIN: AS OBJECT\n"; diff --git a/ext/json/tests/pass001.phpt b/ext/json/tests/pass001.phpt index 43be11e2b0..1fd05fcdd8 100644 --- a/ext/json/tests/pass001.phpt +++ b/ext/json/tests/pass001.phpt @@ -79,10 +79,10 @@ $arr = json_decode($test, true); var_dump($arr); echo "ENCODE: FROM OBJECT\n"; -$obj_enc = json_encode($obj); +$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR); echo $obj_enc . "\n"; echo "ENCODE: FROM ARRAY\n"; -$arr_enc = json_encode($arr); +$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR); echo $arr_enc . "\n"; echo "DECODE AGAIN: AS OBJECT\n"; diff --git a/ext/json/tests/unsupported_type_error.phpt b/ext/json/tests/unsupported_type_error.phpt new file mode 100644 index 0000000000..2564c6a3c8 --- /dev/null +++ b/ext/json/tests/unsupported_type_error.phpt @@ -0,0 +1,26 @@ +--TEST-- +An error is thrown when an unsupported type is encoded +--FILE-- +<?php + +$resource = fopen(__FILE__, "r"); + +var_dump($resource); + +var_dump(json_encode($resource)); +var_dump(json_last_error()); + +var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR)); +var_dump(json_last_error()); + +?> +--EXPECTF-- +resource(5) of type (stream) + +Warning: json_encode(): type is unsupported in %s on line %d +bool(false) +int(8) + +Warning: json_encode(): type is unsupported in %s on line %d +string(4) "null" +int(8) |