From dfdf53df99c8f59e5e3a4296c455041bee96a88d Mon Sep 17 00:00:00 2001 From: jvoisin Date: Thu, 30 Apr 2026 17:50:27 +0200 Subject: Improve coverage for wmemcpy and wmemmove Like it's already done for memcpy and memmove. Add tests as well, to prove that nothing broke. --- include/wchar.h | 16 ++++++++++------ tests/Makefile | 4 ++++ tests/test_wmemcpy_dynamic_read.c | 16 ++++++++++++++++ tests/test_wmemcpy_static_read.c | 16 ++++++++++++++++ tests/test_wmemmove_dynamic_read.c | 16 ++++++++++++++++ tests/test_wmemmove_static_read.c | 16 ++++++++++++++++ 6 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 tests/test_wmemcpy_dynamic_read.c create mode 100644 tests/test_wmemcpy_static_read.c create mode 100644 tests/test_wmemmove_dynamic_read.c create mode 100644 tests/test_wmemmove_static_read.c diff --git a/include/wchar.h b/include/wchar.h index 0842115..9e32720 100644 --- 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 index adea381..fbfd178 100644 --- 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 new file mode 100644 index 0000000..20ba0ad --- /dev/null +++ b/tests/test_wmemcpy_dynamic_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +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 new file mode 100644 index 0000000..c87d378 --- /dev/null +++ b/tests/test_wmemcpy_static_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +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 new file mode 100644 index 0000000..05e315b --- /dev/null +++ b/tests/test_wmemmove_dynamic_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +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 new file mode 100644 index 0000000..6e4a929 --- /dev/null +++ b/tests/test_wmemmove_static_read.c @@ -0,0 +1,16 @@ +#include "common.h" + +#include + +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; +} -- cgit v1.3