Android GetStringUTFRegion()

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;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值