Merge changes from topic 'utf'

* changes:
  Add bound checks to utf16_to_utf8
  Unicode: specify destination length in utf8_to_utf16 methods
This commit is contained in:
Martijn Coenen
2016-08-25 20:08:14 +00:00
committed by Gerrit Code Review
5 changed files with 15 additions and 9 deletions

View File

@@ -280,7 +280,7 @@ static void fillCharArrayBufferUTF(JNIEnv* env, jobject bufferObj,
if (size) { if (size) {
jchar* data = static_cast<jchar*>(env->GetPrimitiveArrayCritical(dataObj, NULL)); jchar* data = static_cast<jchar*>(env->GetPrimitiveArrayCritical(dataObj, NULL));
utf8_to_utf16_no_null_terminator(reinterpret_cast<const uint8_t*>(str), len, utf8_to_utf16_no_null_terminator(reinterpret_cast<const uint8_t*>(str), len,
reinterpret_cast<char16_t*>(data)); reinterpret_cast<char16_t*>(data), (size_t) size);
env->ReleasePrimitiveArrayCritical(dataObj, data, 0); env->ReleasePrimitiveArrayCritical(dataObj, data, 0);
} }
env->SetIntField(bufferObj, gCharArrayBufferClassInfo.sizeCopied, size); env->SetIntField(bufferObj, gCharArrayBufferClassInfo.sizeCopied, size);

View File

@@ -776,7 +776,7 @@ const char16_t* ResStringPool::stringAt(size_t idx, size_t* u16len) const
if (kDebugStringPoolNoisy) { if (kDebugStringPoolNoisy) {
ALOGI("Caching UTF8 string: %s", u8str); ALOGI("Caching UTF8 string: %s", u8str);
} }
utf8_to_utf16(u8str, u8len, u16str); utf8_to_utf16(u8str, u8len, u16str, *u16len + 1);
mCache[idx] = u16str; mCache[idx] = u16str;
return u16str; return u16str;
} else { } else {
@@ -877,7 +877,8 @@ ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
// the ordering, we need to convert strings in the pool to UTF-16. // the ordering, we need to convert strings in the pool to UTF-16.
// But we don't want to hit the cache, so instead we will have a // But we don't want to hit the cache, so instead we will have a
// local temporary allocation for the conversions. // local temporary allocation for the conversions.
char16_t* convBuffer = (char16_t*)malloc(strLen+4); size_t convBufferLen = strLen + 4;
char16_t* convBuffer = (char16_t*)calloc(convBufferLen, sizeof(char16_t));
ssize_t l = 0; ssize_t l = 0;
ssize_t h = mHeader->stringCount-1; ssize_t h = mHeader->stringCount-1;
@@ -887,8 +888,7 @@ ssize_t ResStringPool::indexOfString(const char16_t* str, size_t strLen) const
const uint8_t* s = (const uint8_t*)string8At(mid, &len); const uint8_t* s = (const uint8_t*)string8At(mid, &len);
int c; int c;
if (s != NULL) { if (s != NULL) {
char16_t* end = utf8_to_utf16_n(s, len, convBuffer, strLen+3); char16_t* end = utf8_to_utf16(s, len, convBuffer, convBufferLen);
*end = 0;
c = strzcmp16(convBuffer, end-convBuffer, str, strLen); c = strzcmp16(convBuffer, end-convBuffer, str, strLen);
} else { } else {
c = -1; c = -1;

View File

@@ -57,8 +57,8 @@ aaptTests := \
aaptHostStaticLibs := \ aaptHostStaticLibs := \
libandroidfw \ libandroidfw \
libpng \ libpng \
liblog \
libutils \ libutils \
liblog \
libcutils \ libcutils \
libexpat \ libexpat \
libziparchive-host \ libziparchive-host \

View File

@@ -430,7 +430,11 @@ std::u16string utf8ToUtf16(const StringPiece& utf8) {
std::u16string utf16; std::u16string utf16;
utf16.resize(utf16Length); utf16.resize(utf16Length);
utf8_to_utf16(reinterpret_cast<const uint8_t*>(utf8.data()), utf8.length(), &*utf16.begin()); utf8_to_utf16(
reinterpret_cast<const uint8_t*>(utf8.data()),
utf8.length(),
&*utf16.begin(),
(size_t) utf16Length + 1);
return utf16; return utf16;
} }
@@ -441,8 +445,10 @@ std::string utf16ToUtf8(const StringPiece16& utf16) {
} }
std::string utf8; std::string utf8;
// Make room for '\0' explicitly.
utf8.resize(utf8Length + 1);
utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin(), utf8Length + 1);
utf8.resize(utf8Length); utf8.resize(utf8Length);
utf16_to_utf8(utf16.data(), utf16.length(), &*utf8.begin());
return utf8; return utf8;
} }

View File

@@ -47,8 +47,8 @@ hostStaticLibs := \
libaapt \ libaapt \
libandroidfw \ libandroidfw \
libpng \ libpng \
liblog \
libutils \ libutils \
liblog \
libcutils \ libcutils \
libexpat \ libexpat \
libziparchive-host \ libziparchive-host \