commit dfdf53df99c8f59e5e3a4296c455041bee96a88d
parent f9239e2c0f0be9856322727887a45333683940a6
Author: jvoisin <julien.voisin@dustri.org>
Date: Thu, 30 Apr 2026 17:50:27 +0200
Improve coverage for wmemcpy and wmemmove
Like it's already done for memcpy and memmove. Add tests as well,
to prove that nothing broke.
Diffstat:
6 files changed, 78 insertions(+), 6 deletions(-)
diff --git a/include/wchar.h b/include/wchar.h
@@ -221,21 +221,25 @@ _FORTIFY_FN(wcstombs) size_t wcstombs(char * _FORTIFY_POS0 __s,
}
_FORTIFY_FN(wmemcpy) wchar_t *wmemcpy(wchar_t * _FORTIFY_POS0 __d,
- const wchar_t *__s, size_t __n)
+ const wchar_t * _FORTIFY_POS0 __s,
+ size_t __n)
{
- size_t __b = __bos(__d, 0);
+ size_t __bd = __bos(__d, 0);
+ size_t __bs = __bos(__s, 0);
- if (__n > __b / sizeof(wchar_t))
+ if (__n > __bd / sizeof(wchar_t) || __n > __bs / sizeof(wchar_t))
__builtin_trap();
return __orig_wmemcpy(__d, __s, __n);
}
_FORTIFY_FN(wmemmove) wchar_t *wmemmove(wchar_t * _FORTIFY_POS0 __d,
- const wchar_t *__s, size_t __n)
+ const wchar_t * _FORTIFY_POS0 __s,
+ size_t __n)
{
- size_t __b = __bos(__d, 0);
+ size_t __bd = __bos(__d, 0);
+ size_t __bs = __bos(__s, 0);
- if (__n > __b / sizeof(wchar_t))
+ if (__n > __bd / sizeof(wchar_t) || __n > __bs / sizeof(wchar_t))
__builtin_trap();
return __orig_wmemmove(__d, __s, __n);
}
diff --git a/tests/Makefile b/tests/Makefile
@@ -100,9 +100,13 @@ RUNTIME_TARGETS= \
test_wcscpy_static_write \
test_wcsncat_static_write \
test_wcsncpy_static_write \
+ test_wmemcpy_dynamic_read \
test_wmemcpy_dynamic_write \
+ test_wmemcpy_static_read \
test_wmemcpy_static_write \
+ test_wmemmove_dynamic_read \
test_wmemmove_dynamic_write \
+ test_wmemmove_static_read \
test_wmemmove_static_write \
test_wmemset_dynamic \
test_wmemset_static \
diff --git a/tests/test_wmemcpy_dynamic_read.c b/tests/test_wmemcpy_dynamic_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include <wchar.h>
+
+int main(int argc, char** argv) {
+ wchar_t buffer[12] = {0};
+ wmemcpy(buffer, L"αβγδεζηθικ", sizeof(buffer) / sizeof(wchar_t) - 1);
+ printf("%ls\n", buffer);
+
+ CHK_FAIL_START
+ wmemcpy(buffer, L"αβγδεζ", argc);
+ CHK_FAIL_END
+
+ printf("%ls\n", buffer);
+ return ret;
+}
diff --git a/tests/test_wmemcpy_static_read.c b/tests/test_wmemcpy_static_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include <wchar.h>
+
+int main(int argc, char** argv) {
+ wchar_t buffer[8] = {0};
+ wmemcpy(buffer, L"αβγδεζ", 4);
+ printf("%ls\n", buffer);
+
+ CHK_FAIL_START
+ wmemcpy(buffer, L"αβγδεζ", sizeof(buffer) / sizeof(wchar_t));
+ CHK_FAIL_END
+
+ printf("%ls\n", buffer);
+ return ret;
+}
diff --git a/tests/test_wmemmove_dynamic_read.c b/tests/test_wmemmove_dynamic_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include <wchar.h>
+
+int main(int argc, char** argv) {
+ wchar_t buffer[12] = {0};
+ wmemmove(buffer, L"αβγδεζηθικ", sizeof(buffer) / sizeof(wchar_t) - 1);
+ printf("%ls\n", buffer);
+
+ CHK_FAIL_START
+ wmemmove(buffer, L"αβγδεζ", argc);
+ CHK_FAIL_END
+
+ printf("%ls\n", buffer);
+ return ret;
+}
diff --git a/tests/test_wmemmove_static_read.c b/tests/test_wmemmove_static_read.c
@@ -0,0 +1,16 @@
+#include "common.h"
+
+#include <wchar.h>
+
+int main(int argc, char** argv) {
+ wchar_t buffer[8] = {0};
+ wmemmove(buffer, L"αβγδεζ", 4);
+ printf("%ls\n", buffer);
+
+ CHK_FAIL_START
+ wmemmove(buffer, L"αβγδεζ", sizeof(buffer) / sizeof(wchar_t));
+ CHK_FAIL_END
+
+ printf("%ls\n", buffer);
+ return ret;
+}