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.
Code review
The function GetStringUTFRegion() will add ‘\0’ at the end in KK version
The function GetStringUTFRegion() will not add ‘\0’ at the end in L release
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; } } } }
相关文章推荐
- Android中的日期选择器
- Android开发小技巧整理(持续更新)
- Android屏幕适配全攻略(最权威的官方适配指导)
- 关于Android 构建
- Android Do not do binder operation in destructor
- 【Android应用开发技术:媒体开发】拍照
- AndroidTV/机顶盒 ListView获取焦点与点击事件问题处理方案
- Android 5.0内核和源代码学习(3)——SystemServer启动了什么服务?
- 我是如何自学Android,资料分享(2015 版)
- android 检测sqlite数据表中字段(列)是否存在
- Android屏幕适配,百分比布局
- android里的客户端请求服务端的3种方式
- android开发中handler的总结
- android 锁屏音乐控制
- android 不能试用switch
- Android的下拉列表
- 【Android进阶学习】监听EditText的变化
- Android 之自定义控件样式在drawable文件夹下的XML实现
- Android--操作图片Exif信息
- Android Studio 运行真机出现中文乱码