Fix wchar_t signedness problems found on x86-64.
The existing tests caught this.
Change-Id: I6269844ae4301fd2c596241a59e97eb67ef166fa
diff --git a/libc/bionic/wchar.cpp b/libc/bionic/wchar.cpp
index 8ffdb9c..b46ad49 100644
--- a/libc/bionic/wchar.cpp
+++ b/libc/bionic/wchar.cpp
@@ -198,9 +198,6 @@
}
size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) {
- unsigned char lead;
- int i, len;
-
if (s == NULL) {
// Reset to initial shift state (no-op).
return 1;
@@ -216,18 +213,20 @@
// We always output the shortest sequence possible. Also specify the
// first few bits of the first octet, which contains the information
// about the sequence length.
+ uint8_t lead;
+ size_t length;
if ((wc & ~0x7f) == 0) {
lead = 0;
- len = 1;
+ length = 1;
} else if ((wc & ~0x7ff) == 0) {
lead = 0xc0;
- len = 2;
+ length = 2;
} else if ((wc & ~0xffff) == 0) {
lead = 0xe0;
- len = 3;
+ length = 3;
} else if ((wc & ~0x1fffff) == 0) {
lead = 0xf0;
- len = 4;
+ length = 4;
} else {
errno = EILSEQ;
return ERR_ILLEGAL_SEQUENCE;
@@ -237,13 +236,13 @@
// of 6 bits, least significant last. The first octet is
// a special case because it contains the sequence length
// information.
- for (i = len - 1; i > 0; i--) {
+ for (size_t i = length - 1; i > 0; i--) {
s[i] = (wc & 0x3f) | 0x80;
wc >>= 6;
}
*s = (wc & 0xff) | lead;
- return len;
+ return length;
}
size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstate_t* ps) {
@@ -252,7 +251,7 @@
if (dst == NULL) {
for (i = o = 0; i < nwc; i++, o += r) {
wchar_t wc = (*src)[i];
- if (wc < 0x80) {
+ if (static_cast<uint32_t>(wc) < 0x80) {
// Fast path for plain ASCII characters.
if (wc == 0) {
return o;
@@ -270,7 +269,7 @@
for (i = o = 0; i < nwc && o < len; i++, o += r) {
wchar_t wc = (*src)[i];
- if (wc < 0x80) {
+ if (static_cast<uint32_t>(wc) < 0x80) {
// Fast path for plain ASCII characters.
dst[o] = wc;
if (wc == 0) {