aboutsummaryrefslogtreecommitdiff
path: root/string/test-strnlen.c
diff options
context:
space:
mode:
authorNoah Goldstein <goldstein.w.n@gmail.com>2021-06-09 16:17:14 -0400
committerNoah Goldstein <goldstein.w.n@gmail.com>2021-06-23 14:13:00 -0400
commitda5a6fba0febbfc90896ce1b2eb75c6d8a88a72d (patch)
treeb0b24fa0d148725804d45f3204c985243d38d526 /string/test-strnlen.c
parent6f573a27b6c8b4236445810a44660612323f5a73 (diff)
downloadglibc-da5a6fba0febbfc90896ce1b2eb75c6d8a88a72d.zip
glibc-da5a6fba0febbfc90896ce1b2eb75c6d8a88a72d.tar.gz
glibc-da5a6fba0febbfc90896ce1b2eb75c6d8a88a72d.tar.bz2
String: Add overflow tests for strnlen, memchr, and strncat [BZ #27974]
This commit adds tests for a bug in the wide char variant of the functions where the implementation may assume that maxlen for wcsnlen or n for wmemchr/strncat will not overflow when multiplied by sizeof(wchar_t). These tests show the following implementations failing on x86_64: wcsnlen-sse4_1 wcsnlen-avx2 wmemchr-sse2 wmemchr-avx2 strncat would fail as well if it where on a system that prefered either of the wcsnlen implementations that failed as it relies on wcsnlen. Signed-off-by: Noah Goldstein <goldstein.w.n@gmail.com> Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
Diffstat (limited to 'string/test-strnlen.c')
-rw-r--r--string/test-strnlen.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/string/test-strnlen.c b/string/test-strnlen.c
index 920f58e..f53e092 100644
--- a/string/test-strnlen.c
+++ b/string/test-strnlen.c
@@ -90,6 +90,38 @@ do_test (size_t align, size_t len, size_t maxlen, int max_char)
}
static void
+do_overflow_tests (void)
+{
+ size_t i, j, len;
+ const size_t one = 1;
+ uintptr_t buf_addr = (uintptr_t) buf1;
+
+ for (i = 0; i < 750; ++i)
+ {
+ do_test (0, i, SIZE_MAX - i, BIG_CHAR);
+ do_test (0, i, i - buf_addr, BIG_CHAR);
+ do_test (0, i, -buf_addr - i, BIG_CHAR);
+ do_test (0, i, SIZE_MAX - buf_addr - i, BIG_CHAR);
+ do_test (0, i, SIZE_MAX - buf_addr + i, BIG_CHAR);
+
+ len = 0;
+ for (j = 8 * sizeof(size_t) - 1; j ; --j)
+ {
+ len |= one << j;
+ do_test (0, i, len - i, BIG_CHAR);
+ do_test (0, i, len + i, BIG_CHAR);
+ do_test (0, i, len - buf_addr - i, BIG_CHAR);
+ do_test (0, i, len - buf_addr + i, BIG_CHAR);
+
+ do_test (0, i, ~len - i, BIG_CHAR);
+ do_test (0, i, ~len + i, BIG_CHAR);
+ do_test (0, i, ~len - buf_addr - i, BIG_CHAR);
+ do_test (0, i, ~len - buf_addr + i, BIG_CHAR);
+ }
+ }
+}
+
+static void
do_random_tests (void)
{
size_t i, j, n, align, len;
@@ -283,6 +315,7 @@ test_main (void)
do_random_tests ();
do_page_tests ();
do_page_2_tests ();
+ do_overflow_tests ();
return ret;
}