Mercurial > gnulib
annotate tests/test-strstr.c @ 17356:639f60219717
tests: make it easier to bypass alarm time in debugger
While auditing alarm usage, I noticed that test-regex had a nice
idiom that made it easier to disable an alarm under glibc. Use
it elsewhere, so future copy-and-paste will preserve the idiom.
* tests/test-file-has-acl.c (main): Allow gdb to override alarm.
* tests/test-memmem.c (main): Likewise.
* tests/test-passfd.c (main): Likewise.
* tests/test-ptsname.c (main): Likewise.
* tests/test-ptsname_r.c (main): Likewise.
* tests/test-strcasestr.c (main): Likewise.
* tests/test-strstr.c (main): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Mon, 11 Mar 2013 15:40:32 -0600 |
parents | e542fd46ad6f |
children | 344018b6e5d7 |
rev | line source |
---|---|
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
1 /* |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 * Copyright (C) 2004, 2007-2013 Free Software Foundation, Inc. |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
3 * Written by Bruno Haible and Eric Blake |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
4 * |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
5 * This program is free software: you can redistribute it and/or modify |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
6 * it under the terms of the GNU General Public License as published by |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
7 * the Free Software Foundation; either version 3 of the License, or |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
8 * (at your option) any later version. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
9 * |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
10 * This program is distributed in the hope that it will be useful, |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
13 * GNU General Public License for more details. |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
14 * |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
17 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
18 #include <config.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
19 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
20 #include <string.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
21 |
12489 | 22 #include "signature.h" |
23 SIGNATURE_CHECK (strstr, char *, (char const *, char const *)); | |
24 | |
10195 | 25 #include <signal.h> |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
26 #include <stdlib.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
27 #include <unistd.h> |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
28 |
11611
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
29 #include "zerosize-ptr.h" |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
30 #include "macros.h" |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
31 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
32 int |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
33 main (int argc, char *argv[]) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
34 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
35 #if HAVE_DECL_ALARM |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
36 /* Declare failure if test takes too long, by using default abort |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
37 caused by SIGALRM. All known platforms that lack alarm also have |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
38 a quadratic strstr, and the replacement strstr is known to not |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
39 take too long. */ |
17356
639f60219717
tests: make it easier to bypass alarm time in debugger
Eric Blake <eblake@redhat.com>
parents:
17249
diff
changeset
|
40 int alarm_value = 50; |
10192
0553e4d56bcc
Work around environments that (stupidly) ignore SIGALRM.
Bruno Haible <bruno@clisp.org>
parents:
9889
diff
changeset
|
41 signal (SIGALRM, SIG_DFL); |
17356
639f60219717
tests: make it easier to bypass alarm time in debugger
Eric Blake <eblake@redhat.com>
parents:
17249
diff
changeset
|
42 alarm (alarm_value); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
43 #endif |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
44 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
45 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
46 const char input[] = "foo"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
47 const char *result = strstr (input, ""); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
48 ASSERT (result == input); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
49 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
50 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
51 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
52 const char input[] = "foo"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
53 const char *result = strstr (input, "o"); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
54 ASSERT (result == input + 1); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
55 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
56 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
57 { |
11598
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
58 /* On some platforms, the memchr() functions reads past the first |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
59 occurrence of the byte to be searched, leading to an out-of-bounds |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
60 read access for strstr(). |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
61 See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>. |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
62 This is a bug in memchr(), see the Austin Group's clarification |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11591
diff
changeset
|
63 <http://www.opengroup.org/austin/docs/austin_454.txt>. */ |
11591
8864003db422
tests/test-strstr.c: Rewrite to use malloc/strcpy instead of strdup.
Simon Josefsson <simon@josefsson.org>
parents:
11590
diff
changeset
|
64 const char *fix = "aBaaaaaaaaaaax"; |
11611
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
65 char *page_boundary = (char *) zerosize_ptr (); |
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
66 size_t len = strlen (fix) + 1; |
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
67 char *input = page_boundary ? page_boundary - len : malloc (len); |
11591
8864003db422
tests/test-strstr.c: Rewrite to use malloc/strcpy instead of strdup.
Simon Josefsson <simon@josefsson.org>
parents:
11590
diff
changeset
|
68 const char *result; |
8864003db422
tests/test-strstr.c: Rewrite to use malloc/strcpy instead of strdup.
Simon Josefsson <simon@josefsson.org>
parents:
11590
diff
changeset
|
69 |
8864003db422
tests/test-strstr.c: Rewrite to use malloc/strcpy instead of strdup.
Simon Josefsson <simon@josefsson.org>
parents:
11590
diff
changeset
|
70 strcpy (input, fix); |
8864003db422
tests/test-strstr.c: Rewrite to use malloc/strcpy instead of strdup.
Simon Josefsson <simon@josefsson.org>
parents:
11590
diff
changeset
|
71 result = strstr (input, "B1x"); |
11590
3a0825087a5c
tests/test-strstr.c: Add another self-test.
Simon Josefsson <simon@josefsson.org>
parents:
10195
diff
changeset
|
72 ASSERT (result == NULL); |
11611
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
73 if (!page_boundary) |
ae5773856cba
test-strstr: use memory fence, when possible
Eric Blake <ebb9@byu.net>
parents:
11598
diff
changeset
|
74 free (input); |
11590
3a0825087a5c
tests/test-strstr.c: Add another self-test.
Simon Josefsson <simon@josefsson.org>
parents:
10195
diff
changeset
|
75 } |
3a0825087a5c
tests/test-strstr.c: Add another self-test.
Simon Josefsson <simon@josefsson.org>
parents:
10195
diff
changeset
|
76 |
3a0825087a5c
tests/test-strstr.c: Add another self-test.
Simon Josefsson <simon@josefsson.org>
parents:
10195
diff
changeset
|
77 { |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
78 const char input[] = "ABC ABCDAB ABCDABCDABDE"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
79 const char *result = strstr (input, "ABCDABD"); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
80 ASSERT (result == input + 15); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
81 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
82 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
83 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
84 const char input[] = "ABC ABCDAB ABCDABCDABDE"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
85 const char *result = strstr (input, "ABCDABE"); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
86 ASSERT (result == NULL); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
87 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
88 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
89 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
90 const char input[] = "ABC ABCDAB ABCDABCDABDE"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
91 const char *result = strstr (input, "ABCDABCD"); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
92 ASSERT (result == input + 11); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
93 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
94 |
13772
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
95 /* Check that a long periodic needle does not cause false positives. */ |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
96 { |
13810
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
97 const char input[] = "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD" |
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
98 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD" |
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
99 "_C3_A7_20_EF_BF_BD"; |
13772
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
100 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"; |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
101 const char *result = strstr (input, need); |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
102 ASSERT (result == NULL); |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
103 } |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
104 { |
13810
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
105 const char input[] = "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD" |
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
106 "_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD" |
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
107 "_C3_A7_20_EF_BF_BD_DA_B5_C2_A6_20" |
3ad36edfec7f
tests: Make them compile with TinyCC.
Bruno Haible <bruno@clisp.org>
parents:
13772
diff
changeset
|
108 "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"; |
13772
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
109 const char need[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD"; |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
110 const char *result = strstr (input, need); |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
111 ASSERT (result == input + 115); |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
112 } |
1d8a613275c6
memmem, strstr, strcasestr: fix bug with long periodic needle
Eric Blake <eblake@redhat.com>
parents:
13415
diff
changeset
|
113 |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
114 /* Check that a very long haystack is handled quickly if the needle is |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
115 short and occurs near the beginning. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
116 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
117 size_t repeat = 10000; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
118 size_t m = 1000000; |
13415
a884db63982b
Avoid some more warnings from "gcc -Wwrite-strings".
Bruno Haible <bruno@clisp.org>
parents:
12825
diff
changeset
|
119 const char *needle = |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
120 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
121 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
122 char *haystack = (char *) malloc (m + 1); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
123 if (haystack != NULL) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
124 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
125 memset (haystack, 'A', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
126 haystack[0] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
127 haystack[m] = '\0'; |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
128 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
129 for (; repeat > 0; repeat--) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
130 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
131 ASSERT (strstr (haystack, needle) == haystack + 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
132 } |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
133 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
134 free (haystack); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
135 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
136 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
137 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
138 /* Check that a very long needle is discarded quickly if the haystack is |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
139 short. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
140 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
141 size_t repeat = 10000; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
142 size_t m = 1000000; |
13415
a884db63982b
Avoid some more warnings from "gcc -Wwrite-strings".
Bruno Haible <bruno@clisp.org>
parents:
12825
diff
changeset
|
143 const char *haystack = |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
144 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
145 "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB"; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
146 char *needle = (char *) malloc (m + 1); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
147 if (needle != NULL) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
148 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
149 memset (needle, 'A', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
150 needle[m] = '\0'; |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
151 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
152 for (; repeat > 0; repeat--) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
153 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
154 ASSERT (strstr (haystack, needle) == NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
155 } |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
156 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
157 free (needle); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
158 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
159 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
160 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
161 /* Check that the asymptotic worst-case complexity is not quadratic. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
162 { |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
163 size_t m = 1000000; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
164 char *haystack = (char *) malloc (2 * m + 2); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
165 char *needle = (char *) malloc (m + 2); |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
166 if (haystack != NULL && needle != NULL) |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
167 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
168 const char *result; |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
169 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
170 memset (haystack, 'A', 2 * m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
171 haystack[2 * m] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
172 haystack[2 * m + 1] = '\0'; |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
173 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
174 memset (needle, 'A', m); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
175 needle[m] = 'B'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
176 needle[m + 1] = '\0'; |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
177 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
178 result = strstr (haystack, needle); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11611
diff
changeset
|
179 ASSERT (result == haystack + m); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
180 } |
9749
daf5ba9fe2d9
Remove useless "if" tests before free. Deprecate "free" module.
Jim Meyering <meyering@redhat.com>
parents:
9608
diff
changeset
|
181 free (needle); |
daf5ba9fe2d9
Remove useless "if" tests before free. Deprecate "free" module.
Jim Meyering <meyering@redhat.com>
parents:
9608
diff
changeset
|
182 free (haystack); |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
183 } |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
184 |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
185 /* Sublinear speed is only possible in memmem; strstr must examine |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
186 every character of haystack to find its length. */ |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
187 |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
188 |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
189 { |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
190 /* Ensure that with a barely periodic "short" needle, strstr's |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
191 search does not mistakenly skip just past the match point. |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
192 This use of strstr would mistakenly return NULL before |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
193 gnulib v0.0-4927. */ |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
194 const char *haystack = |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
195 "\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
196 "with_build_libsubdir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
197 "with_local_prefix\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
198 "with_gxx_include_dir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
199 "with_cpp_install_dir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
200 "enable_generated_files_in_srcdir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
201 "with_gnu_ld\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
202 "with_ld\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
203 "with_demangler_in_ld\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
204 "with_gnu_as\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
205 "with_as\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
206 "enable_largefile\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
207 "enable_werror_always\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
208 "enable_checking\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
209 "enable_coverage\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
210 "enable_gather_detailed_mem_stats\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
211 "enable_build_with_cxx\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
212 "with_stabs\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
213 "enable_multilib\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
214 "enable___cxa_atexit\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
215 "enable_decimal_float\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
216 "enable_fixed_point\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
217 "enable_threads\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
218 "enable_tls\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
219 "enable_objc_gc\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
220 "with_dwarf2\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
221 "enable_shared\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
222 "with_build_sysroot\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
223 "with_sysroot\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
224 "with_specs\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
225 "with_pkgversion\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
226 "with_bugurl\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
227 "enable_languages\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
228 "with_multilib_list\n"; |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
229 const char *needle = "\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
230 "with_gnu_ld\n"; |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
231 const char* p = strstr (haystack, needle); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
232 ASSERT (p - haystack == 114); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
233 } |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
234 |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
235 { |
14395
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
236 /* Same bug, shorter trigger. */ |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
237 const char *haystack = "..wi.d."; |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
238 const char *needle = ".d."; |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
239 const char* p = strstr (haystack, needle); |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
240 ASSERT (p - haystack == 4); |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
241 } |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
242 |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
243 { |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
244 /* Like the above, but trigger the flaw in two_way_long_needle |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
245 by using a needle of length LONG_NEEDLE_THRESHOLD (32) or greater. |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
246 Rather than trying to find the right alignment manually, I've |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
247 arbitrarily chosen the following needle and template for the |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
248 haystack, and ensure that for each placement of the needle in |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
249 that haystack, strstr finds it. */ |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
250 const char *needle = "\nwith_gnu_ld-extend-to-len-32-b\n"; |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
251 const char *h = |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
252 "\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
253 "with_build_libsubdir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
254 "with_local_prefix\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
255 "with_gxx_include_dir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
256 "with_cpp_install_dir\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
257 "with_e_\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
258 "..............................\n" |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
259 "with_FGHIJKLMNOPQRSTUVWXYZ\n" |
14395
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
260 "with_567890123456789\n" |
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
261 "with_multilib_list\n"; |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
262 size_t h_len = strlen (h); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
263 char *haystack = malloc (h_len + 1); |
14395
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
264 size_t i; |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
265 ASSERT (haystack); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
266 for (i = 0; i < h_len - strlen (needle); i++) |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
267 { |
14395
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
268 const char *p; |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
269 memcpy (haystack, h, h_len + 1); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
270 memcpy (haystack + i, needle, strlen (needle) + 1); |
14395
bf7bdef206dd
strstr: expand test coverage
Eric Blake <eblake@redhat.com>
parents:
14386
diff
changeset
|
271 p = strstr (haystack, needle); |
14386
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
272 ASSERT (p); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
273 ASSERT (p - haystack == i); |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
274 } |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
275 } |
8d52f50f5946
strstr: fix a bug whereby strstr would mistakenly return NULL
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
276 |
9600
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
277 return 0; |
99268e709d31
Avoid quadratic strstr implementations.
Eric Blake <ebb9@byu.net>
parents:
diff
changeset
|
278 } |