• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

system/core


Commit MetaInfo

Revision72eaa064ac2518d0648584f6021c845ce8c096f3 (tree)
Time2019-12-17 13:15:46
AuthorChih-Wei Huang <cwhuang@linu...>
CommiterChih-Wei Huang

Log Message

libutils: fix incorrect calculation in utf8_length() method

The first character of utf-8 could be larger than 128. If use signed char
variable to hold it, it would be treated as negative. That may result in
some unexpected errors.

For example, without this patch, suppose the code is 0xE88888, then
first_char is 0xE8 and converted to int32_t type (0xFFFFFFE8) and
masked with (~to_ignore_mask). The result utf32 is FFF08208
which is incorrect.

Change-Id: I72b355f380865bc375251eb287fc225fd585a115

Change Summary

Incremental Difference

--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -135,7 +135,7 @@ size_t strnlen32(const char32_t *s, size_t maxlen)
135135
136136 static inline int32_t utf32_at_internal(const char* cur, size_t *num_read)
137137 {
138- const char first_char = *cur;
138+ const unsigned char first_char = *cur;
139139 if ((first_char & 0x80) == 0) { // ASCII
140140 *num_read = 1;
141141 return *cur;
@@ -364,7 +364,7 @@ ssize_t utf8_length(const char *src)
364364 const char *cur = src;
365365 size_t ret = 0;
366366 while (*cur != '\0') {
367- const char first_char = *cur++;
367+ const unsigned char first_char = *cur++;
368368 if ((first_char & 0x80) == 0) { // ASCII
369369 ret += 1;
370370 continue;
@@ -464,7 +464,7 @@ size_t utf8_to_utf32_length(const char *src, size_t src_len)
464464 for (cur = src, end = src + src_len, num_to_skip = 1;
465465 cur < end;
466466 cur += num_to_skip, ret++) {
467- const char first_char = *cur;
467+ const unsigned char first_char = *cur;
468468 num_to_skip = 1;
469469 if ((first_char & 0x80) == 0) { // ASCII
470470 continue;