]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix strstr bug with huge needles (bug 23637)
authorWilco Dijkstra <wdijkstr@arm.com>
Wed, 19 Sep 2018 15:50:18 +0000 (16:50 +0100)
committerWilco Dijkstra <wdijkstr@arm.com>
Fri, 13 Sep 2019 15:40:59 +0000 (16:40 +0100)
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.

(cherry picked from commit 83a552b0bb9fc2a5e80a0ab3723c0a80ce1db9f2)

ChangeLog
string/strcasestr.c
string/strstr.c
string/test-strstr.c

index 12e7d27e4903aab31461e6a090ea525189b919ef..a759cf936c93055c984ed51b2ace68f0730e817f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-09-13  Wilco Dijkstra  <wdijkstr@arm.com>
+
+       [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.
+
 2019-09-13  Rajalakshmi Srinivasaraghavan  <raji@linux.vnet.ibm.com>
 
        * string/memmem.c: Use memcmp for first match.
index 772e5136265e364330d8906bf5ca422a345b9995..19ea1d4bbf1cae3915946f950e99aef828f85131 100644 (file)
@@ -37,8 +37,9 @@
 /* Two-Way algorithm.  */
 #define RETURN_TYPE char *
 #define AVAILABLE(h, h_l, j, n_l)                      \
-  (((j) + (n_l) <= (h_l)) || ((h_l) += __strnlen ((void*)((h) + (h_l)), 512), \
-                             (j) + (n_l) <= (h_l)))
+  (((j) + (n_l) <= (h_l)) \
+   || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
+       (j) + (n_l) <= (h_l)))
 #define CHECK_EOL (1)
 #define RET0_IF_0(a) if (!a) goto ret0
 #define CANON_ELEMENT(c) TOLOWER (c)
index 1eb4dc11db21d4c469a7a52b3c4f011503cc3dce..9cfcfdf77db378bf2229fafdd8d8a552ca13b69d 100644 (file)
@@ -33,8 +33,9 @@
 
 #define RETURN_TYPE char *
 #define AVAILABLE(h, h_l, j, n_l)                      \
-  (((j) + (n_l) <= (h_l)) || ((h_l) += __strnlen ((void*)((h) + (h_l)), 512), \
-                             (j) + (n_l) <= (h_l)))
+  (((j) + (n_l) <= (h_l)) \
+   || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
+       (j) + (n_l) <= (h_l)))
 #define CHECK_EOL (1)
 #define RET0_IF_0(a) if (!a) goto ret0
 #define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
index 8d04134e0b3a80c74edb0db10082a91a4ec460aa..5bce73b0bdac7cb152c9e46f3155f74409f9a7a6 100644 (file)
@@ -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);