strstr() and strcasestr() are only broken pre-SSE4, but memmem() is broken even on SSE4 machines.
On the other hand, on SSE4 machines, strstr() and strcasestr() are quadratic in behavior; in other words, the use of an assembly implementation has actually caused a performance regression over the fix for http://sourceware.org/bugzilla/show_bug.cgi?id=5514
Interestingly enough:
strstr() and strcasestr() are only broken pre-SSE4, but memmem() is broken even on SSE4 machines.
On the other hand, on SSE4 machines, strstr() and strcasestr() are quadratic in behavior; in other words, the use of an assembly implementation has actually caused a performance regression over the fix for sourceware. org/bugzilla/ show_bug. cgi?id= 5514
http://
$ cat foo.c
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define P ":012345678-"
static void quit (int sig) { exit (sig + 128); }
int main(int argc, char **argv)
{
const char *hay = ";" ":013245678-" P P ":012345678." P ":012345678." P;
const char *needle = P P P;
size_t m = 1000000;
char *largehay = malloc (2 * m + 2);
char *largeneedle = malloc (m + 2);
signal (SIGALRM, quit);
alarm (5);
if (!largehay || !largeneedle)
return 2;
memset (largehay, 'A', 2 * m);
largehay[2 * m] = 'B';
largehay[2 * m + 1] = 0;
memset (largeneedle, 'A', m);
largeneedle[m] = 'B';
largeneedle[m + 1] = 0;
switch (argc > 1 ? atoi (argv[1]) : 0)
{
/* Demonstrate str-two-way.h bug. */
case 1:
return !!memmem (hay, strlen (hay), needle, strlen (needle));
case 2:
return !!strstr (hay, needle);
case 3:
return !!strcasestr (hay, needle);
/* Demonstrate quadratic behavior. */
largeneedle, strlen (largeneedle));
case 4:
return !memmem (largehay, strlen (largehay),
case 5:
return !strstr (largehay, largeneedle);
case 6:
return !strcasestr (largehay, largeneedle);
/* Usage error. */
default:
return 2;
}
}
$ for i in $(seq 6); do ./foo $i; echo $?; done
1
0
0
0
142
142