commit 6f13ed360e15b5daa1df2c2de39dc93422db4255
parent abc7bb9f69d648be0457170d1f491594cbb2f7e7
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date: Thu, 20 Mar 2025 20:07:17 +0100
libc/wchar: Use hidden state in mblen and mbrlen
These functions have a hidden state and they should
behave as if they don't call mbrtowc (not using the
internal hidden state of mbrtowc) and the return value
of mblen() has to be adjusted because it cannot
return -2 in the same way that mbrtowc() does.
Diffstat:
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/libc/stdlib/mblen.c b/src/libc/stdlib/mblen.c
@@ -1,9 +1,16 @@
#include <stdlib.h>
+#include <wchar.h>
#undef mblen
int
mblen(const char *s, size_t n)
{
- return mbtowc(NULL, s, n);
+ int ret;
+ static mbstate_t st;
+
+ ret = mbrtowc(NULL, s, n, &st);
+ if (ret < 0)
+ ret = -1;
+ return ret;
}
diff --git a/src/libc/wchar/mbrlen.c b/src/libc/wchar/mbrlen.c
@@ -5,5 +5,9 @@
size_t
mbrlen(const char *restrict s, size_t n, mbstate_t *restrict ps)
{
+ static mbstate_t st;
+
+ if (!ps)
+ ps = &st;
return mbrtowc(NULL, s, n, ps);
}