From 7f2d3d5a82db83a34d5947d6bd195d81e1fc2b9b Mon Sep 17 00:00:00 2001 From: Lassebq Date: Fri, 10 May 2024 22:19:35 +0300 Subject: wchar: add __wcscat_chk, __wcsncpy_chk --- libgcompat/wchar.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/libgcompat/wchar.c b/libgcompat/wchar.c index 78f7e02..4af686d 100644 --- a/libgcompat/wchar.c +++ b/libgcompat/wchar.c @@ -109,8 +109,8 @@ long int __wcstol_internal(const wchar_t *nptr, wchar_t **endptr, int base, * * Some day, when musl supports LC_NUMERIC, we can probably remove this. */ -unsigned long int wcstoul_l(const wchar_t *nptr, wchar_t **endptr, - int base, locale_t loc) +unsigned long int wcstoul_l(const wchar_t *nptr, wchar_t **endptr, int base, + locale_t loc) { return wcstoul(nptr, endptr, base); } @@ -120,8 +120,7 @@ unsigned long int wcstoul_l(const wchar_t *nptr, wchar_t **endptr, * * Some day, when musl supports LC_NUMERIC, we can probably remove this. */ -long int wcstol_l(const wchar_t *nptr, wchar_t **endptr, int base, - locale_t loc) +long int wcstol_l(const wchar_t *nptr, wchar_t **endptr, int base, locale_t loc) { return wcstol(nptr, endptr, base); } @@ -136,7 +135,33 @@ double wcstod_l(const wchar_t *nptr, wchar_t **endptr, locale_t loc) return wcstod(nptr, endptr); } +size_t __mbrlen(const char *restrict s, size_t n, mbstate_t *restrict st) +{ + return mbrlen(s, n, st); +} + +wchar_t *__wcsncpy_chk(wchar_t *dest, const wchar_t *src, size_t n, + size_t destlen) +{ + assert(dest != NULL); + assert(src != NULL); -size_t __mbrlen(const char *restrict s, size_t n, mbstate_t *restrict st) { - return mbrlen(s, n, st); + assert(destlen >= n); + + return wcsncpy(dest, src, n); +} + +wchar_t *__wcscat_chk(wchar_t *dest, const wchar_t *src, size_t n) +{ + wchar_t *a = dest; + size_t destlen = wcslen(dest); + assert(destlen + wcslen(src) + 1 <= n); + + dest += destlen; + while (n && *src) { + n--; + *dest++ = *src++; + } + *dest++ = 0; + return a; } -- cgit v1.2.3-70-g09d2