gettimeofday: port recent C++ fix to Emacs
[gnulib.git] / tests / test-strstr.c
index 718ead7..80e8093 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004, 2007-2011 Free Software Foundation, Inc.
+ * Copyright (C) 2004, 2007-2013 Free Software Foundation, Inc.
  * Written by Bruno Haible and Eric Blake
  *
  * This program is free software: you can redistribute it and/or modify
@@ -37,8 +37,9 @@ main (int argc, char *argv[])
      caused by SIGALRM.  All known platforms that lack alarm also have
      a quadratic strstr, and the replacement strstr is known to not
      take too long.  */
+  int alarm_value = 50;
   signal (SIGALRM, SIG_DFL);
-  alarm (50);
+  alarm (alarm_value);
 #endif
 
   {
@@ -232,6 +233,14 @@ main (int argc, char *argv[])
   }
 
   {
+    /* Same bug, shorter trigger.  */
+    const char *haystack = "..wi.d.";
+    const char *needle = ".d.";
+    const char* p = strstr (haystack, needle);
+    ASSERT (p - haystack == 4);
+  }
+
+  {
     /* Like the above, but trigger the flaw in two_way_long_needle
        by using a needle of length LONG_NEEDLE_THRESHOLD (32) or greater.
        Rather than trying to find the right alignment manually, I've
@@ -248,16 +257,18 @@ main (int argc, char *argv[])
       "with_e_\n"
       "..............................\n"
       "with_FGHIJKLMNOPQRSTUVWXYZ\n"
-      "with_567890123456789\n" "with_multilib_list\n";
+      "with_567890123456789\n"
+      "with_multilib_list\n";
     size_t h_len = strlen (h);
     char *haystack = malloc (h_len + 1);
-    ASSERT (haystack);
     size_t i;
+    ASSERT (haystack);
     for (i = 0; i < h_len - strlen (needle); i++)
       {
+        const char *p;
         memcpy (haystack, h, h_len + 1);
         memcpy (haystack + i, needle, strlen (needle) + 1);
-        const char *p = strstr (haystack, needle);
+        p = strstr (haystack, needle);
         ASSERT (p);
         ASSERT (p - haystack == i);
       }