From: Stanislav Malyshev Date: Wed, 30 Jan 2008 03:21:30 +0000 (+0000) Subject: fix #43941: invalid utf-8 not accepted X-Git-Tag: RELEASE_2_0_0a1~701 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=400c9ecf7b616913c732a0aaaf7f1cf7111e8891;p=php fix #43941: invalid utf-8 not accepted --- diff --git a/ext/json/json.c b/ext/json/json.c index 1d91e6b161..2334a3c483 100644 --- a/ext/json/json.c +++ b/ext/json/json.c @@ -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 index 0000000000..0f86d1dfad --- /dev/null +++ b/ext/json/tests/bug43941.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug #43941 (json_encode() invalid UTF-8) +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +string(5) ""abc"" +string(4) "null" +string(4) "null" +string(17) "[null,null,"abc"]" +Done + diff --git a/ext/json/utf8_to_utf16.c b/ext/json/utf8_to_utf16.c index 4a440a94c1..cb6bbec7ff 100644 --- a/ext/json/utf8_to_utf16.c +++ b/ext/json/utf8_to_utf16.c @@ -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;