1/* Test strlen functions.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#define TEST_MAIN
20#ifndef WIDE
21# define TEST_NAME "strnlen"
22#else
23# define TEST_NAME "wcsnlen"
24#endif /* !WIDE */
25#include "test-string.h"
26
27#ifndef WIDE
28# define STRNLEN strnlen
29# define MEMSET memset
30# define CHAR char
31# define BIG_CHAR CHAR_MAX
32# define MIDDLE_CHAR 127
33# define SIMPLE_STRNLEN simple_strnlen
34#else
35# include <wchar.h>
36# define STRNLEN wcsnlen
37# define MEMSET wmemset
38# define CHAR wchar_t
39# define BIG_CHAR WCHAR_MAX
40# define MIDDLE_CHAR 1121
41# define SIMPLE_STRNLEN simple_wcsnlen
42#endif /* !WIDE */
43
44typedef size_t (*proto_t) (const CHAR *, size_t);
45
46/* Also check the default implementation. */
47#undef STRNLEN
48#ifndef WIDE
49# define MEMCHR __memchr_default
50# define weak_alias(a, b)
51# define libc_hidden_def(a)
52# define libc_hidden_builtin_def(a)
53# include "string/memchr.c"
54# undef STRNLEN
55# define STRNLEN __strnlen_default
56# define memchr __memchr_default
57# include "string/strnlen.c"
58IMPL (__strnlen_default, 1)
59#else
60# define WMEMCHR __wmemchr_default
61# define weak_alias(a, b)
62# define libc_hidden_def(a)
63# define libc_hidden_weak(a)
64# include "wcsmbs/wmemchr.c"
65# define WCSNLEN __wcsnlen_default
66# define wmemchr __wmemchr_default
67# include "wcsmbs/wcsnlen.c"
68IMPL (__wcsnlen_default, 1)
69#endif
70
71static void
72do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
73{
74 size_t len = CALL (impl, s, maxlen);
75 if (len != exp_len)
76 {
77 error (status: 0, errnum: 0, format: "Wrong result in function %s %zd %zd", impl->name,
78 len, exp_len);
79 ret = 1;
80 return;
81 }
82}
83
84static void
85do_test (size_t align, size_t len, size_t maxlen, int max_char)
86{
87 size_t i;
88
89 align &= (getpagesize () / sizeof (CHAR) - 1);
90 if ((align + len) * sizeof (CHAR) >= page_size)
91 return;
92
93 CHAR *buf = (CHAR *) (buf1);
94
95 for (i = 0; i < len; ++i)
96 buf[align + i] = 1 + 11111 * i % max_char;
97 buf[align + len] = 0;
98
99 FOR_EACH_IMPL (impl, 0)
100 do_one_test (impl, s: (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
101}
102
103static void
104do_overflow_tests (void)
105{
106 size_t i, j, al_idx, repeats, len;
107 const size_t one = 1;
108 uintptr_t buf_addr = (uintptr_t) buf1;
109 const size_t alignments[] = { 0, 1, 7, 9, 31, 33, 63, 65, 95, 97, 127, 129 };
110
111 for (al_idx = 0; al_idx < sizeof (alignments) / sizeof (alignments[0]);
112 al_idx++)
113 {
114 for (repeats = 0; repeats < 2; ++repeats)
115 {
116 size_t align = repeats ? (getpagesize () - alignments[al_idx])
117 : alignments[al_idx];
118 align /= sizeof (CHAR);
119 for (i = 0; i < 750; ++i)
120 {
121 do_test (align, len: i, SIZE_MAX, BIG_CHAR);
122
123 do_test (align, len: i, SIZE_MAX - i, BIG_CHAR);
124 do_test (align, len: i, maxlen: i - buf_addr, BIG_CHAR);
125 do_test (align, len: i, maxlen: -buf_addr - i, BIG_CHAR);
126 do_test (align, len: i, SIZE_MAX - buf_addr - i, BIG_CHAR);
127 do_test (align, len: i, SIZE_MAX - buf_addr + i, BIG_CHAR);
128
129 len = 0;
130 for (j = 8 * sizeof (size_t) - 1; j; --j)
131 {
132 len |= one << j;
133 do_test (align, len: i, maxlen: len, BIG_CHAR);
134 do_test (align, len: i, maxlen: len - i, BIG_CHAR);
135 do_test (align, len: i, maxlen: len + i, BIG_CHAR);
136 do_test (align, len: i, maxlen: len - buf_addr - i, BIG_CHAR);
137 do_test (align, len: i, maxlen: len - buf_addr + i, BIG_CHAR);
138
139 do_test (align, len: i, maxlen: ~len - i, BIG_CHAR);
140 do_test (align, len: i, maxlen: ~len + i, BIG_CHAR);
141 do_test (align, len: i, maxlen: ~len - buf_addr - i, BIG_CHAR);
142 do_test (align, len: i, maxlen: ~len - buf_addr + i, BIG_CHAR);
143
144 do_test (align, len: i, maxlen: -buf_addr, BIG_CHAR);
145 do_test (align, len: i, maxlen: j - buf_addr, BIG_CHAR);
146 do_test (align, len: i, maxlen: -buf_addr - j, BIG_CHAR);
147 }
148 }
149 }
150 }
151}
152
153static void
154do_random_tests (void)
155{
156 size_t i, j, n, align, len;
157 CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
158
159 for (n = 0; n < ITERATIONS; n++)
160 {
161 align = random () & 15;
162 len = random () & 511;
163 if (len + align > 510)
164 len = 511 - align - (random () & 7);
165 j = len + align + 64;
166 if (j > 512)
167 j = 512;
168
169 for (i = 0; i < j; i++)
170 {
171 if (i == len + align)
172 p[i] = 0;
173 else
174 {
175 p[i] = random () & 255;
176 if (i >= align && i < len + align && !p[i])
177 p[i] = (random () & 127) + 1;
178 }
179 }
180
181 FOR_EACH_IMPL (impl, 1)
182 {
183 if (len > 0
184 && CALL (impl, (CHAR *) (p + align), len - 1) != len - 1)
185 {
186 error (status: 0, errnum: 0, format: "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
187 n, impl->name, align,
188 CALL (impl, (CHAR *) (p + align), len - 1), len - 1, p);
189 ret = 1;
190 }
191 if (CALL (impl, (CHAR *) (p + align), len) != len)
192 {
193 error (status: 0, errnum: 0, format: "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
194 n, impl->name, align,
195 CALL (impl, (CHAR *) (p + align), len), len, p);
196 ret = 1;
197 }
198 if (CALL (impl, (CHAR *) (p + align), len + 1) != len)
199 {
200 error (status: 0, errnum: 0, format: "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
201 n, impl->name, align,
202 CALL (impl, (CHAR *) (p + align), len + 1), len, p);
203 ret = 1;
204 }
205 }
206 }
207}
208
209/* Tests meant to unveil fail on implementation that does not access bytes
210 around the page boundary accordingly. */
211static void
212do_page_tests (void)
213{
214 size_t i, exp_len, start_offset, offset;
215 /* Calculate the null character offset. */
216 size_t last_offset = (page_size / sizeof (CHAR)) - 1;
217
218 CHAR *s = (CHAR *) buf2;
219 MEMSET (s, 65, (last_offset - 1));
220 s[last_offset] = 0;
221
222 /* Place short strings ending at page boundary. */
223 offset = last_offset;
224 for (i = 0; i < 128; i++)
225 {
226 /* Decrease offset to stress several sizes and alignments. */
227 offset--;
228 exp_len = last_offset - offset;
229 FOR_EACH_IMPL (impl, 0)
230 {
231 /* Varies maxlen value to cover the cases where it is:
232 - larger than length;
233 - slightly greater than length;
234 - equal to length;
235 - slightly less than length. */
236 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: page_size, exp_len);
237 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len + 1, exp_len);
238 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len, exp_len);
239 if (exp_len > 0)
240 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len - 1, exp_len: exp_len - 1);
241 }
242 }
243
244 /* Place long strings ending at page boundary. */
245 start_offset = (last_offset + 1) / 2;
246 for (i = 0; i < 64; ++i)
247 {
248 /* Increase offset to stress several alignments. */
249 offset = start_offset + i;
250 if (offset >= (last_offset + 1))
251 break;
252 exp_len = last_offset - offset;
253 FOR_EACH_IMPL (impl, 0)
254 {
255 /* Checks only for maxlen much larger than length because smaller
256 values are already covered in do_random_tests function. */
257 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: page_size, exp_len);
258 }
259 }
260}
261
262/* Tests meant to unveil fail on implementations that access bytes
263 beyond the maximum length. */
264
265static void
266do_page_2_tests (void)
267{
268 size_t i, exp_len, offset;
269 size_t last_offset = page_size / sizeof (CHAR);
270
271 CHAR *s = (CHAR *) buf2;
272 MEMSET (s, 65, last_offset);
273
274 /* Place short strings ending at page boundary without the null
275 byte. */
276 offset = last_offset;
277 for (i = 0; i < 128; i++)
278 {
279 /* Decrease offset to stress several sizes and alignments. */
280 offset--;
281 exp_len = last_offset - offset;
282 FOR_EACH_IMPL (impl, 0)
283 {
284 /* If an implementation goes beyond EXP_LEN, it will trigger
285 the segfault. */
286 do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len, exp_len);
287 }
288 }
289}
290
291int
292test_main (void)
293{
294 size_t i, length, char_per_page;
295
296 test_init ();
297
298 printf (format: "%20s", "");
299 FOR_EACH_IMPL (impl, 0)
300 printf (format: "\t%s", impl->name);
301 putchar (c: '\n');
302
303 for (i = 1; i < 8; ++i)
304 {
305 do_test (align: 0, len: i, maxlen: i - 1, MIDDLE_CHAR);
306 do_test (align: 0, len: i, maxlen: i, MIDDLE_CHAR);
307 do_test (align: 0, len: i, maxlen: i + 1, MIDDLE_CHAR);
308 }
309
310 for (i = 1; i < 8; ++i)
311 {
312 do_test (align: i, len: i, maxlen: i - 1, MIDDLE_CHAR);
313 do_test (align: i, len: i, maxlen: i, MIDDLE_CHAR);
314 do_test (align: i, len: i, maxlen: i + 1, MIDDLE_CHAR);
315 }
316
317 for (i = 2; i <= 10; ++i)
318 {
319 do_test (align: 0, len: 1 << i, maxlen: 5000, MIDDLE_CHAR);
320 do_test (align: 1, len: 1 << i, maxlen: 5000, MIDDLE_CHAR);
321 }
322
323 for (i = 1; i < 8; ++i)
324 do_test (align: 0, len: i, maxlen: 5000, BIG_CHAR);
325
326 for (i = 1; i < 8; ++i)
327 do_test (align: i, len: i, maxlen: 5000, BIG_CHAR);
328
329 for (i = 2; i <= 10; ++i)
330 {
331 do_test (align: 0, len: 1 << i, maxlen: 5000, BIG_CHAR);
332 do_test (align: 1, len: 1 << i, maxlen: 5000, BIG_CHAR);
333 }
334
335 char_per_page = getpagesize () / sizeof (CHAR);
336
337 for (i = 0; i <= 127; i++)
338 for (length = i; length <= 512; length++)
339 {
340 do_test (align: i, len: length, maxlen: 512, BIG_CHAR);
341 do_test (align: char_per_page - i, len: length, maxlen: 512, BIG_CHAR);
342 }
343
344 do_random_tests ();
345 do_page_tests ();
346 do_page_2_tests ();
347 do_overflow_tests ();
348 return ret;
349}
350
351#include <support/test-driver.c>
352

source code of glibc/string/test-strnlen.c