您的位置:首页 > 移动开发 > Android开发

Android GetStringUTFRegion()

2015-08-03 15:58 369 查看
Description : Since the implementation of GetStringUTFRegion() between L release and KK is different. In KK release, the GetStringUTFRegion() will return a string including the terminating null byte (‘\0’) but L does not.

Risk : If you don’t set the ending character to be ‘\0’, it’s possible to get native crash when operate the returned string.

// reference usage
const jsize length = env->GetStringUTFLength(filePath);
char filePathChars[length + 1];
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
filePathChars[length] = '\0';


Code review

The function GetStringUTFRegion() will add ‘\0’ at the end in KK version

@/dalvik/vm/UtfString.cpp
static void convertUtf16ToUtf8(char* utf8Str, const u2* utf16Str, int len)
{
assert(len >= 0);
while (len--) {
...
}
*utf8Str = '\0';
}


The function GetStringUTFRegion() will not add ‘\0’ at the end in L release

@/art/runtime/utf.cc
void ConvertUtf16ToModifiedUtf8(char* utf8_out, const uint16_t* utf16_in, size_t char_count) {
while (char_count--) {
uint16_t ch = *utf16_in++;
if (ch > 0 && ch <= 0x7f) {
*utf8_out++ = ch;
} else {
if (ch > 0x07ff) {
*utf8_out++ = (ch >> 12) | 0xe0;
*utf8_out++ = ((ch >> 6) & 0x3f) | 0x80;
*utf8_out++ = (ch & 0x3f) | 0x80;
} else /*(ch > 0x7f || ch == 0)*/ {
*utf8_out++ = (ch >> 6) | 0xc0;
*utf8_out++ = (ch & 0x3f) | 0x80;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: