Skip to content

Commit 3b88c07

Browse files
jason-simmonsSkia Commit-Bot
authored andcommitted
Handle surrogates in SkUTF::UTF16ToUTF8
Change-Id: Ib2e34aaa49520328660cf5df1998dd50f6f07fbb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/319176 Reviewed-by: Julia Lavrova <[email protected]> Commit-Queue: Jason Simmons <[email protected]>
1 parent 1748c6a commit 3b88c07

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/utils/SkUTF.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,26 @@ int SkUTF::UTF8ToUTF16(uint16_t dst[], int dstCapacity, const char src[], size_t
285285
}
286286

287287
int SkUTF::UTF16ToUTF8(char dst[], int dstCapacity, const uint16_t src[], size_t srcLength) {
288-
289288
if (!dst) {
290289
dstCapacity = 0;
291290
}
292291

293292
int dstLength = 0;
294293
const char* endDst = dst + dstCapacity;
295-
for (size_t i = 0; i < srcLength; ++i) {
294+
const uint16_t* endSrc = src + srcLength;
295+
while (src < endSrc) {
296+
SkUnichar uni = NextUTF16(&src, endSrc);
297+
if (uni < 0) {
298+
return -1;
299+
}
300+
296301
char utf8[SkUTF::kMaxBytesInUTF8Sequence];
297-
size_t count = ToUTF8(src[i], utf8);
302+
size_t count = ToUTF8(uni, utf8);
298303
if (count == 0) {
299304
return -1;
300305
}
301306
dstLength += count;
307+
302308
if (dst) {
303309
const char* elems = utf8;
304310
while (dst < endDst && count > 0) {

0 commit comments

Comments
 (0)