From cc90ac54beb7359e5a3210261ce09159bbc43e92 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 23 Jun 2012 15:21:20 -0300 Subject: [PATCH 1/3] - BFN --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index 79b7325626f..b9bb0091c03 100644 --- a/NEWS +++ b/NEWS @@ -50,6 +50,8 @@ PHP NEWS . Fixed bug #62227 (Invalid phar stream path causes crash). (Felipe) - Reflection: + . Fixed bug #62384 (Attempting to invoke a Closure more than once causes + segfault). (Felipe) . Fixed bug #62202 (ReflectionParameter::getDefaultValue() memory leaks with constant). (Laruence) From e8862725770c3d5ee27c6c8c8a01b226b610fa08 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 23 Jun 2012 15:21:32 -0300 Subject: [PATCH 2/3] - BFN --- NEWS | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index dfad253f5a2..e93d717c9e1 100644 --- a/NEWS +++ b/NEWS @@ -52,8 +52,10 @@ PHP NEWS return a value). (Johannes) - Reflection: - . Fixed bug #62202 (ReflectionParameter::getDefaultValue() memory leaks - with constant). (Laruence) + . Fixed bug #62384 (Attempting to invoke a Closure more than once causes + segfault). (Felipe) + . Fixed bug #62202 (ReflectionParameter::getDefaultValue() memory leaks + with constant). (Laruence) - Sockets: . Fixed bug #62025 (__ss_family was changed on AIX 5.3). (Felipe) From 84fe2cc890e49f40bac7c3ba74b3cfc6dc4cef2f Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 23 Jun 2012 20:46:27 +0200 Subject: [PATCH 3/3] Improve json_encode error handling json_encode() now returns bool(false) for all possible errors, throws the respective warning and also sets the respective json_last_error() error code. Three new error codes have been added: * JSON_ERROR_RECURSION * JSON_ERROR_INF_OR_NAN * JSON_ERROR_UNSUPPORTED_TYPE To get a partial JSON output instead of bool(false) the option JSON_PARTIAL_OUTPUT_ON_ERROR can be specified. In this case the invalid segments will be replaced either by null (for recursion, unsupported type and invalid JSON) or 0 (for Inf and NaN). The warning for invalid UTF-8 stays intact and is thrown also with display_errors = On. If this behavior is undesired this can be remedied later. --- ext/json/JSON_parser.h | 5 ++- ext/json/json.c | 15 ++++++-- ext/json/tests/003.phpt | 13 +++++-- ext/json/tests/004.phpt | 10 +++++ ext/json/tests/inf_nan_error.phpt | 44 ++++++++++++++++++++++ ext/json/tests/json_encode_basic.phpt | 4 +- ext/json/tests/pass001.1.phpt | 4 +- ext/json/tests/pass001.phpt | 4 +- ext/json/tests/unsupported_type_error.phpt | 26 +++++++++++++ 9 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 ext/json/tests/inf_nan_error.phpt create mode 100644 ext/json/tests/unsupported_type_error.phpt diff --git a/ext/json/JSON_parser.h b/ext/json/JSON_parser.h index 746190bb355..5037344890b 100644 --- a/ext/json/JSON_parser.h +++ b/ext/json/JSON_parser.h @@ -24,7 +24,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 ce2cf43fcc9..a90476530e8 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -81,6 +81,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); return SUCCESS; } @@ -181,6 +184,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; @@ -303,7 +307,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'); } } @@ -460,7 +465,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'); } } @@ -476,7 +482,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; } @@ -570,7 +577,7 @@ static PHP_FUNCTION(json_encode) php_json_encode(&buf, parameter, options TSRMLS_CC); - if (JSON_G(error_code) != PHP_JSON_ERROR_NONE && options ^ PHP_JSON_PARTIAL_OUTPUT_ON_ERROR) { + 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); diff --git a/ext/json/tests/003.phpt b/ext/json/tests/003.phpt index 3b52fb08841..ab637110087 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)); -/* Break circular data structure to prevent memory leaks */ -unset($a[0]); +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"; ?> @@ -25,6 +27,11 @@ 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 1d282f9a961..9f9abfe46a6 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"; ?> @@ -19,6 +24,11 @@ object(stdClass)#%d (1) { *RECURSION* } +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/inf_nan_error.phpt b/ext/json/tests/inf_nan_error.phpt new file mode 100644 index 00000000000..a3ed5e7b882 --- /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-- + +--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 003fcd44c6a..7ee68c58ca8 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=== diff --git a/ext/json/tests/pass001.1.phpt b/ext/json/tests/pass001.1.phpt index 7e15a7622ac..a51f885780d 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 43be11e2b0f..1fd05fcdd87 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 00000000000..2564c6a3c8c --- /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-- + +--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)