Skip to content

Commit 8b245e5

Browse files
author
Ilia Alshanetsky
committed
Fixed bug #41067 (json_encode() problem with UTF-16 input).
1 parent 8f845b7 commit 8b245e5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? Apr 2007, PHP 5.2.2RC2
4+
- Fixed bug #41093 (magic_quotes_gpc ignores first arrays keys). (Arpad, Ilia)
45
- Fixed bug #41083 (mysql_ping() requires MYSQL_OPT_RECONNECT to be set since
56
MySQL 5.0.13). (xiaojb at gmail dot com, Tony)
67
- Fixed bug #41075 (memleak when creating default object caused exception).
78
(Dmitry)
9+
- Fixed bug #41067 (json_encode() problem with UTF-16 input). (jp at df5ea
10+
dot net. Ilia)
811
- Fixed bug #41063 (chdir doesn't like root paths). (Dmitry)
912
- Fixed bug #41061 ("visibility error" in ReflectionFunction::export()).
1013
(Johannes)

ext/json/JSON_parser.c

+19
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,25 @@ static void utf16_to_utf8(smart_str *buf, unsigned short utf16)
316316
smart_str_appendc(buf, 0xc0 | (utf16 >> 6));
317317
smart_str_appendc(buf, 0x80 | (utf16 & 0x3f));
318318
}
319+
else if ((utf16 & 0xfc00) == 0xdc00
320+
&& buf->len >= 3
321+
&& ((unsigned char) buf->c[buf->len - 3]) == 0xed
322+
&& ((unsigned char) buf->c[buf->len - 2] & 0xf0) == 0xa0
323+
&& ((unsigned char) buf->c[buf->len - 1] & 0xc0) == 0x80)
324+
{
325+
/* found surrogate pair */
326+
unsigned long utf32;
327+
328+
utf32 = (((buf->c[buf->len - 2] & 0xf) << 16)
329+
| ((buf->c[buf->len - 1] & 0x3f) << 10)
330+
| (utf16 & 0x3ff)) + 0x10000;
331+
buf->len -= 3;
332+
333+
smart_str_appendc(buf, 0xf0 | (utf32 >> 18));
334+
smart_str_appendc(buf, 0x80 | ((utf32 >> 12) & 0x3f));
335+
smart_str_appendc(buf, 0x80 | ((utf32 >> 6) & 0x3f));
336+
smart_str_appendc(buf, 0x80 | (utf32 & 0x3f));
337+
}
319338
else
320339
{
321340
smart_str_appendc(buf, 0xe0 | (utf16 >> 12));

ext/json/tests/bug41067.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #41067 (json_encode() problem with UTF-16 input)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("json")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$single_barline = "\360\235\204\200";
8+
$array = array($single_barline);
9+
print bin2hex($single_barline) . "\n";
10+
// print $single_barline . "\n\n";
11+
$json = json_encode($array);
12+
print $json . "\n\n";
13+
$json_decoded = json_decode($json, true);
14+
// print $json_decoded[0] . "\n";
15+
print bin2hex($json_decoded[0]) . "\n";
16+
print "END\n";
17+
?>
18+
--EXPECT--
19+
f09d8480
20+
["\ud834\udd00"]
21+
22+
f09d8480
23+
END

0 commit comments

Comments
 (0)