]> granicus.if.org Git - php/commitdiff
Fix Bug #54058, invalid utf-8 doesn't set json_encode() in all cases
authorScott MacVicar <scottmac@php.net>
Mon, 21 Feb 2011 08:09:02 +0000 (08:09 +0000)
committerScott MacVicar <scottmac@php.net>
Mon, 21 Feb 2011 08:09:02 +0000 (08:09 +0000)
ext/json/json.c
ext/json/tests/bug54058.phpt [new file with mode: 0644]

index 5f3e1a854bff1b61fa293b44ae9aa609090ea7ec..ec76c1d5f38f4e40788ccc4aea69470e240a3756 100644 (file)
@@ -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 (file)
index 0000000..3b1136b
--- /dev/null
@@ -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)