From: Scott MacVicar Date: Mon, 21 Feb 2011 08:09:02 +0000 (+0000) Subject: Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases X-Git-Tag: php-5.3.6RC2~20 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3242016f283138486242e4c4f4c0b1a5ce2e000f;p=php Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases --- 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-- + +--FILE-- +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)