about summary refs log tree commit diff
path: root/string/test-strstr.c
diff options
context:
space:
mode:
authorWilco Dijkstra <wdijkstr@arm.com>2018-09-19 16:50:18 +0100
committerWilco Dijkstra <wdijkstr@arm.com>2018-09-19 16:50:18 +0100
commit83a552b0bb9fc2a5e80a0ab3723c0a80ce1db9f2 (patch)
tree4861e51f5b6524e080ca62aefceb6a1d052fc652 /string/test-strstr.c
parentd734727837b5135c4c4c540d8c53e5a06aa7556a (diff)
downloadglibc-83a552b0bb9fc2a5e80a0ab3723c0a80ce1db9f2.tar.gz
glibc-83a552b0bb9fc2a5e80a0ab3723c0a80ce1db9f2.tar.xz
glibc-83a552b0bb9fc2a5e80a0ab3723c0a80ce1db9f2.zip
Fix strstr bug with huge needles (bug 23637)
The generic strstr in GLIBC 2.28 fails to match huge needles.  The optimized
AVAILABLE macro reads ahead a large fixed amount to reduce the overhead of
repeatedly checking for the end of the string.  However if the needle length
is larger than this, two_way_long_needle may confuse this as meaning the end
of the string and return NULL.  This is fixed by adding the needle length to
the amount to read ahead.

	[BZ #23637]
	* string/test-strstr.c (pr23637): New function.
	(test_main): Add tests with longer needles.
	* string/strcasestr.c (AVAILABLE): Fix readahead distance.
	* string/strstr.c (AVAILABLE): Likewise.
Diffstat (limited to 'string/test-strstr.c')
-rw-r--r--string/test-strstr.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/string/test-strstr.c b/string/test-strstr.c
index 8d99716ff3..5861b01b73 100644
--- a/string/test-strstr.c
+++ b/string/test-strstr.c
@@ -151,6 +151,32 @@ check2 (void)
     }
 }
 
+#define N 1024
+
+static void
+pr23637 (void)
+{
+  char *h = (char*) buf1;
+  char *n = (char*) buf2;
+
+  for (int i = 0; i < N; i++)
+    {
+      n[i] = 'x';
+      h[i] = ' ';
+      h[i + N] = 'x';
+    }
+
+  n[N] = '\0';
+  h[N * 2] = '\0';
+
+  /* Ensure we don't match at the first 'x'.  */
+  h[0] = 'x';
+
+  char *exp_result = stupid_strstr (h, n);
+  FOR_EACH_IMPL (impl, 0)
+    check_result (impl, h, n, exp_result);
+}
+
 static int
 test_main (void)
 {
@@ -158,6 +184,7 @@ test_main (void)
 
   check1 ();
   check2 ();
+  pr23637 ();
 
   printf ("%23s", "");
   FOR_EACH_IMPL (impl, 0)
@@ -202,6 +229,9 @@ test_main (void)
 	do_test (15, 9, hlen, klen, 1);
 	do_test (15, 15, hlen, klen, 0);
 	do_test (15, 15, hlen, klen, 1);
+
+	do_test (15, 15, hlen + klen * 4, klen * 4, 0);
+	do_test (15, 15, hlen + klen * 4, klen * 4, 1);
       }
 
   do_test (0, 0, page_size - 1, 16, 0);