summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorScott MacVicar <scottmac@php.net>2011-02-21 08:09:02 +0000
committerScott MacVicar <scottmac@php.net>2011-02-21 08:09:02 +0000
commit3242016f283138486242e4c4f4c0b1a5ce2e000f (patch)
treefdf4ddbf946b8d6ac23cf1c0b9c311229c7d9d31
parent1b2d14c5e10cc024f97a257a00fbefdb3a906501 (diff)
downloadphp-git-3242016f283138486242e4c4f4c0b1a5ce2e000f.tar.gz
Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases
-rw-r--r--ext/json/json.c3
-rw-r--r--ext/json/tests/bug54058.phpt35
2 files changed, 37 insertions, 1 deletions
diff --git a/ext/json/json.c b/ext/json/json.c
index 5f3e1a854b..ec76c1d5f3 100644
--- a/ext/json/json.c
+++ b/ext/json/json.c
@@ -431,7 +431,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_DC) /* {{{ */
{
- JSON_G(error_code) = PHP_JSON_ERROR_NONE;
switch (Z_TYPE_P(val))
{
case IS_NULL:
@@ -567,6 +566,8 @@ static PHP_FUNCTION(json_encode)
return;
}
+ JSON_G(error_code) = PHP_JSON_ERROR_NONE;
+
php_json_encode(&buf, parameter, options TSRMLS_CC);
ZVAL_STRINGL(return_value, buf.c, buf.len, 1);
diff --git a/ext/json/tests/bug54058.phpt b/ext/json/tests/bug54058.phpt
new file mode 100644
index 0000000000..3b1136bdd9
--- /dev/null
+++ b/ext/json/tests/bug54058.phpt
@@ -0,0 +1,35 @@
+--TEST--
+Bug #54058 (json_last_error() invalid UTF-8 produces wrong error)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+
+$bad_utf8 = quoted_printable_decode('=B0');
+
+json_encode($bad_utf8);
+var_dump(json_last_error());
+
+$a = new stdclass;
+$a->foo = quoted_printable_decode('=B0');
+json_encode($a);
+var_dump(json_last_error());
+
+$b = new stdclass;
+$b->foo = $bad_utf8;
+$b->bar = 1;
+json_encode($b);
+var_dump(json_last_error());
+
+$c = array(
+ 'foo' => $bad_utf8,
+ 'bar' => 1
+);
+json_encode($c);
+var_dump(json_last_error());
+?>
+--EXPECTF--
+int(5)
+int(5)
+int(5)
+int(5)