shithub: scc

Download patch

ref: 836d29c894d35835ce59a180530b330000dbfb51
parent: 41390429613c2d8ffd2abae7e88d71ef9bfda362
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Mar 16 09:31:26 EDT 2022

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.

--- 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 @@
 return_code:
 	if (pwc)
 		*pwc = wc;
-	return (*s) ? t - s : 0;
+	if (*s == '\0')
+		return 0;
+	return t - (unsigned char *) s;
 }