Skip to content

Commit e89c52b

Browse files
committed
Improve UtfString handling of invalid strings
First check whether string is actually utf-8 before trying to process it as it. Fixes phpmyadmin/phpmyadmin#13385 Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent 7cad6fa commit e89c52b

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44

55
* Fixed parsing SQL comment at the end of query.
6+
* Improved handing of non utf-8 strings.
67

78
## [4.1.7] - 2017-06-06
89

src/UtfString.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ public function __construct($str)
7575
$this->byteIdx = 0;
7676
$this->charIdx = 0;
7777
$this->byteLen = mb_strlen($str, '8bit');
78-
$this->charLen = mb_strlen($str, 'UTF-8');
78+
if (! mb_check_encoding($str, 'UTF-8')) {
79+
$this->charLen = 0;
80+
} else {
81+
$this->charLen = mb_strlen($str, 'UTF-8');
82+
}
7983
}
8084

8185
/**

tests/Misc/UtfStringTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,35 @@ public function testToString()
8282
$str = new UtfString(static::TEST_PHRASE);
8383
$this->assertEquals(static::TEST_PHRASE, (string) $str);
8484
}
85+
86+
/**
87+
* Test access to string
88+
*
89+
* @dataProvider utf8_strings
90+
*/
91+
public function testAccess($text, $pos10, $pos20)
92+
{
93+
$str = new UtfString($text);
94+
$this->assertEquals($pos10, $str->offsetGet(10));
95+
$this->assertEquals($pos20, $str->offsetGet(20));
96+
$this->assertEquals($pos10, $str->offsetGet(10));
97+
}
98+
99+
public function utf8_strings()
100+
{
101+
return array(
102+
'ascii' => array(
103+
'abcdefghijklmnopqrstuvwxyz', 'k', 'u'
104+
),
105+
'unicode' => array(
106+
'áéíóúýěřťǔǐǒǎšďȟǰǩľžčǚň', 'ǐ', 'č'
107+
),
108+
'emoji' => array(
109+
'😂😄😃😀😊😉😍😘😚😗😂👿😮😨😱😠😡😤😖😆😋👯', '😂', '😋'
110+
),
111+
'iso' => array(
112+
"P\xf8\xed\xb9ern\xec \xbelu\xbbou\xe8k\xfd k\xf3d \xfap\xecl \xef\xe1belsk\xe9 k\xf3dy", null, null
113+
),
114+
);
115+
}
85116
}

0 commit comments

Comments
 (0)