elf: Fix GL(dl_phdr) and GL(dl_phnum) for static builds [BZ #29864]
[glibc.git] / string / test-strstr.c
blob26e983bee0568de4e565e9dd423a128ada9c8466
1 /* Test and measure strstr functions.
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #define TEST_NAME "strstr"
21 #include "test-string.h"
24 #define STRSTR c_strstr
25 #define libc_hidden_builtin_def(arg) /* nothing */
26 #define __strnlen strnlen
27 #include "strstr.c"
29 /* Naive implementation to verify results. */
30 static char *
31 simple_strstr (const char *s1, const char *s2)
33 ssize_t s1len = strlen (s1);
34 ssize_t s2len = strlen (s2);
36 if (s2len > s1len)
37 return NULL;
39 for (ssize_t i = 0; i <= s1len - s2len; ++i)
41 size_t j;
42 for (j = 0; j < s2len; ++j)
43 if (s1[i + j] != s2[j])
44 break;
45 if (j == s2len)
46 return (char *) s1 + i;
49 return NULL;
53 typedef char *(*proto_t) (const char *, const char *);
55 IMPL (c_strstr, 0)
56 IMPL (strstr, 1)
59 static int
60 check_result (impl_t *impl, const char *s1, const char *s2,
61 char *exp_result)
63 char *result = CALL (impl, s1, s2);
64 if (result != exp_result)
66 error (0, 0, "Wrong result in function %s %s %s", impl->name,
67 (result == NULL) ? "(null)" : result,
68 (exp_result == NULL) ? "(null)" : exp_result);
69 ret = 1;
70 return -1;
73 return 0;
76 static void
77 do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
79 if (check_result (impl, s1, s2, exp_result) < 0)
80 return;
84 static void
85 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
86 int fail)
88 char *s1 = (char *) (buf1 + align1);
89 char *s2 = (char *) (buf2 + align2);
91 static const char d[] = "1234567890abcdef";
92 #define dl (sizeof (d) - 1)
93 char *ss2 = s2;
94 for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
96 size_t t = l > dl ? dl : l;
97 ss2 = mempcpy (ss2, d, t);
99 s2[len2] = '\0';
101 if (fail)
103 char *ss1 = s1;
104 for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
106 size_t t = l > dl ? dl : l;
107 memcpy (ss1, d, t);
108 ++ss1[len2 > 7 ? 7 : len2 - 1];
109 ss1 += t;
112 else
114 memset (s1, '0', len1);
115 memcpy (s1 + len1 - len2, s2, len2);
117 s1[len1] = '\0';
119 FOR_EACH_IMPL (impl, 0)
120 do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
124 static void
125 check1 (void)
127 const char s1[] =
128 "F_BD_CE_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_88_20_EF_BF_BD_EF_BF_BD_EF_BF_BD_C3_A7_20_EF_BF_BD";
129 const char s2[] = "_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD_EF_BF_BD";
130 char *exp_result;
132 exp_result = simple_strstr (s1, s2);
133 FOR_EACH_IMPL (impl, 0)
134 check_result (impl, s1, s2, exp_result);
137 static void
138 check2 (void)
140 const char s1_stack[] = ", enable_static, \0, enable_shared, ";
141 const size_t s1_byte_count = 18;
142 const char *s2_stack = &(s1_stack[s1_byte_count]);
143 const size_t s2_byte_count = 18;
144 char *exp_result;
145 const size_t page_size_real = getpagesize ();
147 /* Haystack at end of page. The following page is protected. */
148 char *s1_page_end = (void *) buf1 + page_size - s1_byte_count;
149 strcpy (s1_page_end, s1_stack);
151 /* Haystack which crosses a page boundary.
152 Note: page_size is at least 2 * getpagesize. See test_init. */
153 char *s1_page_cross = (void *) buf1 + page_size_real - 8;
154 strcpy (s1_page_cross, s1_stack);
156 /* Needle at end of page. The following page is protected. */
157 char *s2_page_end = (void *) buf2 + page_size - s2_byte_count;
158 strcpy (s2_page_end, s2_stack);
160 /* Needle which crosses a page boundary.
161 Note: page_size is at least 2 * getpagesize. See test_init. */
162 char *s2_page_cross = (void *) buf2 + page_size_real - 8;
163 strcpy (s2_page_cross, s2_stack);
165 exp_result = simple_strstr (s1_stack, s2_stack);
166 FOR_EACH_IMPL (impl, 0)
168 check_result (impl, s1_stack, s2_stack, exp_result);
169 check_result (impl, s1_stack, s2_page_end, exp_result);
170 check_result (impl, s1_stack, s2_page_cross, exp_result);
172 check_result (impl, s1_page_end, s2_stack, exp_result);
173 check_result (impl, s1_page_end, s2_page_end, exp_result);
174 check_result (impl, s1_page_end, s2_page_cross, exp_result);
176 check_result (impl, s1_page_cross, s2_stack, exp_result);
177 check_result (impl, s1_page_cross, s2_page_end, exp_result);
178 check_result (impl, s1_page_cross, s2_page_cross, exp_result);
182 #define N 1024
184 static void
185 pr23637 (void)
187 char *h = (char*) buf1;
188 char *n = (char*) buf2;
190 for (int i = 0; i < N; i++)
192 n[i] = 'x';
193 h[i] = ' ';
194 h[i + N] = 'x';
197 n[N] = '\0';
198 h[N * 2] = '\0';
200 /* Ensure we don't match at the first 'x'. */
201 h[0] = 'x';
203 char *exp_result = simple_strstr (h, n);
204 FOR_EACH_IMPL (impl, 0)
205 check_result (impl, h, n, exp_result);
208 static int
209 test_main (void)
211 test_init ();
213 check1 ();
214 check2 ();
215 pr23637 ();
217 printf ("%23s", "");
218 FOR_EACH_IMPL (impl, 0)
219 printf ("\t%s", impl->name);
220 putchar ('\n');
222 for (size_t klen = 2; klen < 32; ++klen)
223 for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
225 do_test (0, 0, hlen, klen, 0);
226 do_test (0, 0, hlen, klen, 1);
227 do_test (0, 3, hlen, klen, 0);
228 do_test (0, 3, hlen, klen, 1);
229 do_test (0, 9, hlen, klen, 0);
230 do_test (0, 9, hlen, klen, 1);
231 do_test (0, 15, hlen, klen, 0);
232 do_test (0, 15, hlen, klen, 1);
234 do_test (3, 0, hlen, klen, 0);
235 do_test (3, 0, hlen, klen, 1);
236 do_test (3, 3, hlen, klen, 0);
237 do_test (3, 3, hlen, klen, 1);
238 do_test (3, 9, hlen, klen, 0);
239 do_test (3, 9, hlen, klen, 1);
240 do_test (3, 15, hlen, klen, 0);
241 do_test (3, 15, hlen, klen, 1);
243 do_test (9, 0, hlen, klen, 0);
244 do_test (9, 0, hlen, klen, 1);
245 do_test (9, 3, hlen, klen, 0);
246 do_test (9, 3, hlen, klen, 1);
247 do_test (9, 9, hlen, klen, 0);
248 do_test (9, 9, hlen, klen, 1);
249 do_test (9, 15, hlen, klen, 0);
250 do_test (9, 15, hlen, klen, 1);
252 do_test (15, 0, hlen, klen, 0);
253 do_test (15, 0, hlen, klen, 1);
254 do_test (15, 3, hlen, klen, 0);
255 do_test (15, 3, hlen, klen, 1);
256 do_test (15, 9, hlen, klen, 0);
257 do_test (15, 9, hlen, klen, 1);
258 do_test (15, 15, hlen, klen, 0);
259 do_test (15, 15, hlen, klen, 1);
261 do_test (15, 15, hlen + klen * 4, klen * 4, 0);
262 do_test (15, 15, hlen + klen * 4, klen * 4, 1);
265 do_test (0, 0, page_size - 1, 16, 0);
266 do_test (0, 0, page_size - 1, 16, 1);
268 return ret;
271 #include <support/test-driver.c>