summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikic@php.net>2012-06-27 12:28:55 +0200
committerNikita Popov <nikic@php.net>2012-06-27 12:28:55 +0200
commitd372b33c9b941be9a795bf3705bd22dc5f6092c3 (patch)
treec79ac04eddb9ffedf9ec3a2be57886e339f11776
parent9a86784859067eaca44cad7f8b4aea00e3ffe6b2 (diff)
parent4662151ea7d7b6920d115cf2a2d6e9d4232727a3 (diff)
downloadphp-git-d372b33c9b941be9a795bf3705bd22dc5f6092c3.tar.gz
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
-rw-r--r--ext/json/json.c42
-rw-r--r--ext/json/tests/003.phpt10
-rw-r--r--ext/json/tests/004.phpt10
-rw-r--r--ext/json/tests/bug43941.phpt8
-rw-r--r--ext/json/tests/bug53946.phpt4
-rw-r--r--ext/json/tests/bug54058.phpt17
-rw-r--r--ext/json/tests/bug61537.phpt39
-rw-r--r--ext/json/tests/bug61978.phpt6
-rw-r--r--ext/json/tests/inf_nan_error.phpt23
-rw-r--r--ext/json/tests/json_encode_basic.phpt2
-rw-r--r--ext/json/tests/unsupported_type_error.phpt10
-rw-r--r--main/php_version.h2
12 files changed, 114 insertions, 59 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index bc30251430..5ea307020a 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -55,7 +55,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
ZEND_ARG_INFO(0, options)
ZEND_END_ARG_INFO()
-ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_json_last_error, 0, 0, 0)
+ ZEND_ARG_INFO(0, as_string)
ZEND_END_ARG_INFO()
/* }}} */
@@ -236,7 +237,6 @@ 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;
}
@@ -378,7 +378,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
efree(tmp);
} else {
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');
}
}
@@ -395,7 +394,6 @@ 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;
- 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);
@@ -527,7 +525,6 @@ 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;
}
@@ -592,7 +589,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
efree(d);
} else {
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');
}
}
@@ -614,7 +610,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
default:
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;
}
@@ -754,11 +749,40 @@ static PHP_FUNCTION(json_decode)
Returns the error code of the last json_decode(). */
static PHP_FUNCTION(json_last_error)
{
- if (zend_parse_parameters_none() == FAILURE) {
+ zend_bool as_string = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &as_string) == FAILURE) {
return;
}
- RETURN_LONG(JSON_G(error_code));
+ /* return error code (JSON_ERROR_* constants) */
+ if (!as_string) {
+ RETURN_LONG(JSON_G(error_code));
+ }
+
+ /* return error message (for debugging purposes) */
+ switch(JSON_G(error_code)) {
+ case PHP_JSON_ERROR_NONE:
+ RETURN_STRING("No error", 1);
+ case PHP_JSON_ERROR_DEPTH:
+ RETURN_STRING("Maximum stack depth exceeded", 1);
+ case PHP_JSON_ERROR_STATE_MISMATCH:
+ RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
+ case PHP_JSON_ERROR_CTRL_CHAR:
+ RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
+ case PHP_JSON_ERROR_SYNTAX:
+ RETURN_STRING("Syntax error", 1);
+ case PHP_JSON_ERROR_UTF8:
+ RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
+ case PHP_JSON_ERROR_RECURSION:
+ RETURN_STRING("Recursion detected", 1);
+ case PHP_JSON_ERROR_INF_OR_NAN:
+ RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
+ case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
+ RETURN_STRING("Type is not supported", 1);
+ default:
+ RETURN_STRING("Unknown error", 1);
+ }
}
/* }}} */
diff --git a/ext/json/tests/003.phpt b/ext/json/tests/003.phpt
index ab63711008..71874525a7 100644
--- a/ext/json/tests/003.phpt
+++ b/ext/json/tests/003.phpt
@@ -10,11 +10,17 @@ $a[] = &$a;
var_dump($a);
+echo "\n";
+
var_dump(json_encode($a));
var_dump(json_last_error());
+var_dump(json_last_error(true));
+
+echo "\n";
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
var_dump(json_last_error());
+var_dump(json_last_error(true));
echo "Done\n";
?>
@@ -27,11 +33,11 @@ array(1) {
}
}
-Warning: json_encode(): recursion detected in %s on line %d
bool(false)
int(6)
+string(%d) "Recursion detected"
-Warning: json_encode(): recursion detected in %s on line %d
string(8) "[[null]]"
int(6)
+string(%d) "Recursion detected"
Done
diff --git a/ext/json/tests/004.phpt b/ext/json/tests/004.phpt
index 9f9abfe46a..49c543edca 100644
--- a/ext/json/tests/004.phpt
+++ b/ext/json/tests/004.phpt
@@ -10,11 +10,17 @@ $a->prop = $a;
var_dump($a);
+echo "\n";
+
var_dump(json_encode($a));
var_dump(json_last_error());
+var_dump(json_last_error(true));
+
+echo "\n";
var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
var_dump(json_last_error());
+var_dump(json_last_error(true));
echo "Done\n";
?>
@@ -24,11 +30,11 @@ object(stdClass)#%d (1) {
*RECURSION*
}
-Warning: json_encode(): recursion detected in %s on line %d
bool(false)
int(6)
+string(%d) "Recursion detected"
-Warning: json_encode(): recursion detected in %s on line %d
string(22) "{"prop":{"prop":null}}"
int(6)
+string(%d) "Recursion detected"
Done
diff --git a/ext/json/tests/bug43941.phpt b/ext/json/tests/bug43941.phpt
index fb59b71c96..48bd7ad524 100644
--- a/ext/json/tests/bug43941.phpt
+++ b/ext/json/tests/bug43941.phpt
@@ -14,15 +14,7 @@ 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"
-
-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 079906fa33..111438ddc4 100644
--- a/ext/json/tests/bug53946.phpt
+++ b/ext/json/tests/bug53946.phpt
@@ -12,9 +12,5 @@ var_dump(json_encode("ab\xE0", JSON_UNESCAPED_UNICODE));
--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 >𝄞<""
-
-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 e3596eb62b..2c2304578e 100644
--- a/ext/json/tests/bug54058.phpt
+++ b/ext/json/tests/bug54058.phpt
@@ -9,17 +9,20 @@ $bad_utf8 = quoted_printable_decode('=B0');
json_encode($bad_utf8);
var_dump(json_last_error());
+var_dump(json_last_error(true));
$a = new stdclass;
$a->foo = quoted_printable_decode('=B0');
json_encode($a);
var_dump(json_last_error());
+var_dump(json_last_error(true));
$b = new stdclass;
$b->foo = $bad_utf8;
$b->bar = 1;
json_encode($b);
var_dump(json_last_error());
+var_dump(json_last_error(true));
$c = array(
'foo' => $bad_utf8,
@@ -27,17 +30,15 @@ $c = array(
);
json_encode($c);
var_dump(json_last_error());
+var_dump(json_last_error(true));
+
?>
--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
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
diff --git a/ext/json/tests/bug61537.phpt b/ext/json/tests/bug61537.phpt
new file mode 100644
index 0000000000..f6bb02bae4
--- /dev/null
+++ b/ext/json/tests/bug61537.phpt
@@ -0,0 +1,39 @@
+--TEST--
+Bug #61537 (json_encode() incorrectly truncates/discards information)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+$invalid_utf8 = "\x9f";
+
+var_dump(json_encode($invalid_utf8));
+var_dump(json_last_error(), json_last_error(true));
+
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error(), json_last_error(true));
+
+echo "\n";
+
+$invalid_utf8 = "an invalid sequen\xce in the middle of a string";
+
+var_dump(json_encode($invalid_utf8));
+var_dump(json_last_error(), json_last_error(true));
+
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error(), json_last_error(true));
+
+?>
+--EXPECTF--
+bool(false)
+int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+string(4) "null"
+int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+
+bool(false)
+int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
+string(4) "null"
+int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
diff --git a/ext/json/tests/bug61978.phpt b/ext/json/tests/bug61978.phpt
index 4c863c689e..c34b03f8f7 100644
--- a/ext/json/tests/bug61978.phpt
+++ b/ext/json/tests/bug61978.phpt
@@ -31,17 +31,13 @@ class JsonTest2 implements JsonSerializable {
$obj1 = new JsonTest1();
var_dump(json_encode($obj1, JSON_PARTIAL_OUTPUT_ON_ERROR));
-echo "\n==\n";
+echo "==\n";
$obj2 = new JsonTest2();
var_dump(json_encode($obj2, JSON_PARTIAL_OUTPUT_ON_ERROR));
?>
--EXPECTF--
-Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"
-
==
-
-Warning: json_encode(): recursion detected in %s on line %d
string(44) "{"test":"123","me":{"test":"123","me":null}}"
diff --git a/ext/json/tests/inf_nan_error.phpt b/ext/json/tests/inf_nan_error.phpt
index a3ed5e7b88..f12e902d9f 100644
--- a/ext/json/tests/inf_nan_error.phpt
+++ b/ext/json/tests/inf_nan_error.phpt
@@ -8,37 +8,36 @@ $inf = INF;
var_dump($inf);
var_dump(json_encode($inf));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
+
+echo "\n";
$nan = NAN;
var_dump($nan);
var_dump(json_encode($nan));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
?>
--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(34) "Inf and NaN cannot be JSON encoded"
string(1) "0"
int(7)
-float(NAN)
+string(34) "Inf and NaN cannot be JSON encoded"
-Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
+float(NAN)
bool(false)
int(7)
-
-Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
+string(34) "Inf and NaN cannot be JSON encoded"
string(1) "0"
int(7)
+string(34) "Inf and NaN cannot be JSON encoded"
diff --git a/ext/json/tests/json_encode_basic.phpt b/ext/json/tests/json_encode_basic.phpt
index 7ee68c58ca..fc348eed81 100644
--- a/ext/json/tests/json_encode_basic.phpt
+++ b/ext/json/tests/json_encode_basic.phpt
@@ -150,8 +150,6 @@ string(4) "null"
-- Iteration 25 --
string(4) "null"
-- Iteration 26 --
-
-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"}"
diff --git a/ext/json/tests/unsupported_type_error.phpt b/ext/json/tests/unsupported_type_error.phpt
index 2564c6a3c8..f36afb44a5 100644
--- a/ext/json/tests/unsupported_type_error.phpt
+++ b/ext/json/tests/unsupported_type_error.phpt
@@ -8,19 +8,17 @@ $resource = fopen(__FILE__, "r");
var_dump($resource);
var_dump(json_encode($resource));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
?>
--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(21) "Type is not supported"
string(4) "null"
int(8)
+string(21) "Type is not supported"
diff --git a/main/php_version.h b/main/php_version.h
index 6b49f40864..4b7709c552 100644
--- a/main/php_version.h
+++ b/main/php_version.h
@@ -5,4 +5,4 @@
#define PHP_RELEASE_VERSION 5
#define PHP_EXTRA_VERSION "-dev"
#define PHP_VERSION "5.4.5-dev"
-#define PHP_VERSION_ID 50404
+#define PHP_VERSION_ID 50405