commit 836d29c894d35835ce59a180530b330000dbfb51
parent 41390429613c2d8ffd2abae7e88d71ef9bfda362
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 16 Mar 2022 13:31:26 +0100
libc: Fix signess of pointer in mbtowc()
We have to access the elements of the char array using
an unsigned pointer, because otherwise we can have
sign extension that can modify the behaviour of the
function.
Diffstat:
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/libc/stdlib/mbtowc.c b/src/libc/stdlib/mbtowc.c
@@ -5,7 +5,7 @@
int
mbtowc(wchar_t *restrict pwc, const char *restrict s, size_t n)
{
- const char *t = s;
+ unsigned char *t = (unsigned char *) s;
unsigned long wc;
unsigned c;
size_t len;
@@ -30,5 +30,7 @@ mbtowc(wchar_t *restrict pwc, const char *restrict s, size_t n)
return_code:
if (pwc)
*pwc = wc;
- return (*s) ? t - s : 0;
+ if (*s == '\0')
+ return 0;
+ return t - (unsigned char *) s;
}