Skip to content

Commit 715311f

Browse files
author
Jeff Brown
committed
Fix regression in CursorWindow.getString()
Bug: 5332296 NewStringUTF expects modified UTF-8, so it barfs on UTF-8 strings that contain high codepoints. Even though it results in an extra copy being performed, first convert to UTF-16, then call NewString. Change-Id: Idbfeb3cc2c4b731834e4482848dcac2fa33ec2d0
1 parent 7ce7452 commit 715311f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

core/jni/android_database_CursorWindow.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,14 @@ static jstring nativeGetString(JNIEnv* env, jclass clazz, jint windowPtr,
205205
if (type == FIELD_TYPE_STRING) {
206206
uint32_t size = fieldSlot->data.buffer.size;
207207
#if WINDOW_STORAGE_UTF8
208-
return size > 1 ? env->NewStringUTF(window->getFieldSlotValueString(fieldSlot))
209-
: gEmptyString;
208+
if (size <= 1) {
209+
return gEmptyString;
210+
}
211+
// Convert to UTF-16 here instead of calling NewStringUTF. NewStringUTF
212+
// doesn't like UTF-8 strings with high codepoints. It actually expects
213+
// Modified UTF-8 with encoded surrogate pairs.
214+
String16 utf16(window->getFieldSlotValueString(fieldSlot), size - 1);
215+
return env->NewString(reinterpret_cast<const jchar*>(utf16.string()), utf16.size());
210216
#else
211217
size_t chars = size / sizeof(char16_t);
212218
return chars ? env->NewString(reinterpret_cast<jchar*>(

0 commit comments

Comments
 (0)