]> granicus.if.org Git - php/commitdiff
fix #43941: invalid utf-8 not accepted
authorStanislav Malyshev <stas@php.net>
Wed, 30 Jan 2008 03:21:30 +0000 (03:21 +0000)
committerStanislav Malyshev <stas@php.net>
Wed, 30 Jan 2008 03:21:30 +0000 (03:21 +0000)
ext/json/json.c
ext/json/tests/bug43941.phpt [new file with mode: 0644]
ext/json/utf8_to_utf16.c

index 1d91e6b16179845e268f938e0d401377d5caf407..2334a3c48333e4f201fe9597f45e53b9d8c81f34 100644 (file)
@@ -265,12 +265,17 @@ static void json_escape_string(smart_str *buf, zstr s, int len, zend_uchar type,
                len = utf8_to_utf16(utf16, s.s, len);
                if (len <= 0)
                {
-                       if (utf16)
-                       {
+                       if (utf16) {
                                efree(utf16);
                        }
-
-                       smart_str_appendl(buf, "\"\"", 2);
+                       if(len < 0) {
+                               if(!PG(display_errors)) {
+                                       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);
+                       }
                        return;
                }
        }
diff --git a/ext/json/tests/bug43941.phpt b/ext/json/tests/bug43941.phpt
new file mode 100644 (file)
index 0000000..0f86d1d
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+Bug #43941 (json_encode() invalid UTF-8)
+--SKIPIF--
+<?php if (!extension_loaded("json")) print "skip"; ?>
+--FILE--
+<?php
+
+var_dump(json_encode("abc"));
+var_dump(json_encode("ab\xE0"));
+var_dump(json_encode("ab\xE0c"));
+var_dump(json_encode(array("ab\xE0", "ab\xE0c", "abc")));
+
+echo "Done\n";
+?>
+--EXPECTF--
+string(5) ""abc""
+string(4) "null"
+string(4) "null"
+string(17) "[null,null,"abc"]"
+Done
+
index 4a440a94c12dadbe7e817f60dab64d4cffebb626..cb6bbec7ffb2c563ac88147389627e0193999b9d 100644 (file)
@@ -39,7 +39,7 @@ int utf8_to_utf16(unsigned short w[], char p[], int length)  /* {{{ */
        for (;;) {
                c = utf8_decode_next(&utf8);
                if (c < 0) {
-                       return UTF8_END ? the_index : UTF8_ERROR;
+                       return (c == UTF8_END) ? the_index : UTF8_ERROR;
                }
                if (c < 0x10000) {
                        w[the_index] = (unsigned short)c;