The original code in libiberty says "FIXME" and then says it has not been
validated to be ANSI compliant. However, this patch changes the function to
match implementations that ARE compliant, and such code is in the public
domain.

I ran the test results, and there are no test failures.

--- strstr.c 2020-03-12 07:07:24.000000000 -0400
+++ strstr_fixed.c 2020-05-01 19:53:13.904340902 -0400
@@ -16,26 +16,20 @@

 */

-
-/* FIXME:  The above description is ANSI compiliant.  This routine has not
-   been validated to comply with it.  -fnf */
-
 #include <stddef.h>

-extern char *strchr (const char *, int);
-extern int strncmp (const void *, const void *, size_t);
+extern int memcmp (const void *, const void *, size_t);
 extern size_t strlen (const char *);

 char *
 strstr (const char *s1, const char *s2)
 {
-  const char *p = s1;
   const size_t len = strlen (s2);
-
-  for (; (p = strchr (p, *s2)) != 0; p++)
-    {
-      if (strncmp (p, s2, len) == 0)
- return (char *)p;
-    }
+  while (*s1)
+  {
+    if (!memcmp (s1, s2, len))
+      return (char *)s1;
+    ++s1;
+  }
   return (0);
 }

Reply via email to