diff options
author | Sergio Giro <sgiro@google.com> | 2016-07-20 20:01:33 +0100 |
---|---|---|
committer | Sergio Giro <sgiro@google.com> | 2016-08-02 18:47:53 +0000 |
commit | 1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9 (patch) | |
tree | 5e07e40ae8bc52895b05b42e9d627c4f3057a0e0 /libutils/Unicode.cpp | |
parent | de717d55c7a3f7d8051222f26ef12eb38b3777f2 (diff) | |
download | system_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.tar.gz system_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.tar.bz2 system_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.zip |
Unicode: specify destination length in utf8_to_utf16 methods
String16(const char *utf8) now returns the empty string in case
a string ends halfway throw a utf8 character.
Bug: 29267949
Change-Id: I5223caa7d42f4582a982609a898a02043265c6d3
Diffstat (limited to 'libutils/Unicode.cpp')
-rw-r--r-- | libutils/Unicode.cpp | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp index ba084f6ce..5f96efa79 100644 --- a/libutils/Unicode.cpp +++ b/libutils/Unicode.cpp @@ -17,6 +17,7 @@ #include <log/log.h> #include <utils/Unicode.h> +#include <limits.h> #include <stddef.h> #if defined(_WIN32) @@ -542,7 +543,7 @@ static inline uint32_t utf8_to_utf32_codepoint(const uint8_t *src, size_t length //printf("Char at %p: len=%d, utf-16=%p\n", src, length, (void*)result); } -ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len) +ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len, bool overreadIsFatal) { const uint8_t* const u8end = u8str + u8len; const uint8_t* u8cur = u8str; @@ -552,6 +553,20 @@ ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len) while (u8cur < u8end) { u16measuredLen++; int u8charLen = utf8_codepoint_len(*u8cur); + // Malformed utf8, some characters are beyond the end. + // Cases: + // If u8charLen == 1, this becomes u8cur >= u8end, which cannot happen as u8cur < u8end, + // then this condition fail and we continue, as expected. + // If u8charLen == 2, this becomes u8cur + 1 >= u8end, which fails only if + // u8cur == u8end - 1, that is, there was only one remaining character to read but we need + // 2 of them. This condition holds and we return -1, as expected. + if (u8cur + u8charLen - 1 >= u8end) { + if (overreadIsFatal) { + LOG_ALWAYS_FATAL("Attempt to overread computing length of utf8 string"); + } else { + return -1; + } + } uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8charLen); if (codepoint > 0xFFFF) u16measuredLen++; // this will be a surrogate pair in utf16 u8cur += u8charLen; @@ -568,38 +583,21 @@ ssize_t utf8_to_utf16_length(const uint8_t* u8str, size_t u8len) return u16measuredLen; } -char16_t* utf8_to_utf16_no_null_terminator(const uint8_t* u8str, size_t u8len, char16_t* u16str) -{ - const uint8_t* const u8end = u8str + u8len; - const uint8_t* u8cur = u8str; - char16_t* u16cur = u16str; - - while (u8cur < u8end) { - size_t u8len = utf8_codepoint_len(*u8cur); - uint32_t codepoint = utf8_to_utf32_codepoint(u8cur, u8len); - - // Convert the UTF32 codepoint to one or more UTF16 codepoints - if (codepoint <= 0xFFFF) { - // Single UTF16 character - *u16cur++ = (char16_t) codepoint; - } else { - // Multiple UTF16 characters with surrogates - codepoint = codepoint - 0x10000; - *u16cur++ = (char16_t) ((codepoint >> 10) + 0xD800); - *u16cur++ = (char16_t) ((codepoint & 0x3FF) + 0xDC00); - } - - u8cur += u8len; - } - return u16cur; -} - -void utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str) { - char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str); +char16_t* utf8_to_utf16(const uint8_t* u8str, size_t u8len, char16_t* u16str, size_t u16len) { + // A value > SSIZE_MAX is probably a negative value returned as an error and casted. + LOG_ALWAYS_FATAL_IF(u16len == 0 || u16len > SSIZE_MAX, "u16len is %zu", u16len); + char16_t* end = utf8_to_utf16_no_null_terminator(u8str, u8len, u16str, u16len - 1); *end = 0; + return end; } -char16_t* utf8_to_utf16_n(const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) { +char16_t* utf8_to_utf16_no_null_terminator( + const uint8_t* src, size_t srcLen, char16_t* dst, size_t dstLen) { + if (dstLen == 0) { + return dst; + } + // A value > SSIZE_MAX is probably a negative value returned as an error and casted. + LOG_ALWAYS_FATAL_IF(dstLen > SSIZE_MAX, "dstLen is %zu", dstLen); const uint8_t* const u8end = src + srcLen; const uint8_t* u8cur = src; const char16_t* const u16end = dst + dstLen; |