system/corennnnn
Révision | facd201bd2867cc98c96277d1c264290dadfa278 (tree) |
---|---|
l'heure | 2015-12-22 12:23:00 |
Auteur | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
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
@@ -134,7 +134,7 @@ size_t strnlen32(const char32_t *s, size_t maxlen) | ||
134 | 134 | |
135 | 135 | static inline int32_t utf32_at_internal(const char* cur, size_t *num_read) |
136 | 136 | { |
137 | - const char first_char = *cur; | |
137 | + const unsigned char first_char = *cur; | |
138 | 138 | if ((first_char & 0x80) == 0) { // ASCII |
139 | 139 | *num_read = 1; |
140 | 140 | return *cur; |
@@ -366,7 +366,7 @@ ssize_t utf8_length(const char *src) | ||
366 | 366 | const char *cur = src; |
367 | 367 | size_t ret = 0; |
368 | 368 | while (*cur != '\0') { |
369 | - const char first_char = *cur++; | |
369 | + const unsigned char first_char = *cur++; | |
370 | 370 | if ((first_char & 0x80) == 0) { // ASCII |
371 | 371 | ret += 1; |
372 | 372 | continue; |
@@ -457,7 +457,7 @@ size_t utf8_to_utf32_length(const char *src, size_t src_len) | ||
457 | 457 | for (cur = src, end = src + src_len, num_to_skip = 1; |
458 | 458 | cur < end; |
459 | 459 | cur += num_to_skip, ret++) { |
460 | - const char first_char = *cur; | |
460 | + const unsigned char first_char = *cur; | |
461 | 461 | num_to_skip = 1; |
462 | 462 | if ((first_char & 0x80) == 0) { // ASCII |
463 | 463 | continue; |