1 /* Test strlen functions.
2 Copyright (C) 1999-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/>. */
21 # define TEST_NAME "strnlen"
23 # define TEST_NAME "wcsnlen"
25 #include "test-string.h"
28 # define STRNLEN strnlen
29 # define MEMSET memset
31 # define BIG_CHAR CHAR_MAX
32 # define MIDDLE_CHAR 127
33 # define SIMPLE_STRNLEN simple_strnlen
36 # define STRNLEN wcsnlen
37 # define MEMSET wmemset
39 # define BIG_CHAR WCHAR_MAX
40 # define MIDDLE_CHAR 1121
41 # define SIMPLE_STRNLEN simple_wcsnlen
44 typedef size_t (*proto_t
) (const CHAR
*, size_t);
48 /* Naive implementation to verify results. */
50 SIMPLE_STRNLEN (const CHAR
*s
, size_t maxlen
)
54 for (i
= 0; i
< maxlen
&& s
[i
]; ++i
);
59 do_one_test (impl_t
*impl
, const CHAR
*s
, size_t maxlen
, size_t exp_len
)
61 size_t len
= CALL (impl
, s
, maxlen
);
64 error (0, 0, "Wrong result in function %s %zd %zd", impl
->name
,
72 do_test (size_t align
, size_t len
, size_t maxlen
, int max_char
)
76 align
&= (getpagesize () / sizeof (CHAR
) - 1);
77 if ((align
+ len
) * sizeof (CHAR
) >= page_size
)
80 CHAR
*buf
= (CHAR
*) (buf1
);
82 for (i
= 0; i
< len
; ++i
)
83 buf
[align
+ i
] = 1 + 11111 * i
% max_char
;
86 FOR_EACH_IMPL (impl
, 0)
87 do_one_test (impl
, (CHAR
*) (buf
+ align
), maxlen
, MIN (len
, maxlen
));
91 do_overflow_tests (void)
93 size_t i
, j
, al_idx
, repeats
, len
;
95 uintptr_t buf_addr
= (uintptr_t) buf1
;
96 const size_t alignments
[] = { 0, 1, 7, 9, 31, 33, 63, 65, 95, 97, 127, 129 };
98 for (al_idx
= 0; al_idx
< sizeof (alignments
) / sizeof (alignments
[0]);
101 for (repeats
= 0; repeats
< 2; ++repeats
)
103 size_t align
= repeats
? (getpagesize () - alignments
[al_idx
])
104 : alignments
[al_idx
];
105 align
/= sizeof (CHAR
);
106 for (i
= 0; i
< 750; ++i
)
108 do_test (align
, i
, SIZE_MAX
, BIG_CHAR
);
110 do_test (align
, i
, SIZE_MAX
- i
, BIG_CHAR
);
111 do_test (align
, i
, i
- buf_addr
, BIG_CHAR
);
112 do_test (align
, i
, -buf_addr
- i
, BIG_CHAR
);
113 do_test (align
, i
, SIZE_MAX
- buf_addr
- i
, BIG_CHAR
);
114 do_test (align
, i
, SIZE_MAX
- buf_addr
+ i
, BIG_CHAR
);
117 for (j
= 8 * sizeof (size_t) - 1; j
; --j
)
120 do_test (align
, i
, len
, BIG_CHAR
);
121 do_test (align
, i
, len
- i
, BIG_CHAR
);
122 do_test (align
, i
, len
+ i
, BIG_CHAR
);
123 do_test (align
, i
, len
- buf_addr
- i
, BIG_CHAR
);
124 do_test (align
, i
, len
- buf_addr
+ i
, BIG_CHAR
);
126 do_test (align
, i
, ~len
- i
, BIG_CHAR
);
127 do_test (align
, i
, ~len
+ i
, BIG_CHAR
);
128 do_test (align
, i
, ~len
- buf_addr
- i
, BIG_CHAR
);
129 do_test (align
, i
, ~len
- buf_addr
+ i
, BIG_CHAR
);
131 do_test (align
, i
, -buf_addr
, BIG_CHAR
);
132 do_test (align
, i
, j
- buf_addr
, BIG_CHAR
);
133 do_test (align
, i
, -buf_addr
- j
, BIG_CHAR
);
141 do_random_tests (void)
143 size_t i
, j
, n
, align
, len
;
144 CHAR
*p
= (CHAR
*) (buf1
+ page_size
- 512 * sizeof (CHAR
));
146 for (n
= 0; n
< ITERATIONS
; n
++)
148 align
= random () & 15;
149 len
= random () & 511;
150 if (len
+ align
> 510)
151 len
= 511 - align
- (random () & 7);
152 j
= len
+ align
+ 64;
156 for (i
= 0; i
< j
; i
++)
158 if (i
== len
+ align
)
162 p
[i
] = random () & 255;
163 if (i
>= align
&& i
< len
+ align
&& !p
[i
])
164 p
[i
] = (random () & 127) + 1;
168 FOR_EACH_IMPL (impl
, 1)
171 && CALL (impl
, (CHAR
*) (p
+ align
), len
- 1) != len
- 1)
173 error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
174 n
, impl
->name
, align
,
175 CALL (impl
, (CHAR
*) (p
+ align
), len
- 1), len
- 1, p
);
178 if (CALL (impl
, (CHAR
*) (p
+ align
), len
) != len
)
180 error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
181 n
, impl
->name
, align
,
182 CALL (impl
, (CHAR
*) (p
+ align
), len
), len
, p
);
185 if (CALL (impl
, (CHAR
*) (p
+ align
), len
+ 1) != len
)
187 error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
188 n
, impl
->name
, align
,
189 CALL (impl
, (CHAR
*) (p
+ align
), len
+ 1), len
, p
);
196 /* Tests meant to unveil fail on implementation that does not access bytes
197 around the page boundary accordingly. */
201 size_t i
, exp_len
, start_offset
, offset
;
202 /* Calculate the null character offset. */
203 size_t last_offset
= (page_size
/ sizeof (CHAR
)) - 1;
205 CHAR
*s
= (CHAR
*) buf2
;
206 MEMSET (s
, 65, (last_offset
- 1));
209 /* Place short strings ending at page boundary. */
210 offset
= last_offset
;
211 for (i
= 0; i
< 128; i
++)
213 /* Decrease offset to stress several sizes and alignments. */
215 exp_len
= last_offset
- offset
;
216 FOR_EACH_IMPL (impl
, 0)
218 /* Varies maxlen value to cover the cases where it is:
219 - larger than length;
220 - slightly greater than length;
222 - slightly less than length. */
223 do_one_test (impl
, (CHAR
*) (s
+ offset
), page_size
, exp_len
);
224 do_one_test (impl
, (CHAR
*) (s
+ offset
), exp_len
+ 1, exp_len
);
225 do_one_test (impl
, (CHAR
*) (s
+ offset
), exp_len
, exp_len
);
227 do_one_test (impl
, (CHAR
*) (s
+ offset
), exp_len
- 1, exp_len
- 1);
231 /* Place long strings ending at page boundary. */
232 start_offset
= (last_offset
+ 1) / 2;
233 for (i
= 0; i
< 64; ++i
)
235 /* Increase offset to stress several alignments. */
236 offset
= start_offset
+ i
;
237 if (offset
>= (last_offset
+ 1))
239 exp_len
= last_offset
- offset
;
240 FOR_EACH_IMPL (impl
, 0)
242 /* Checks only for maxlen much larger than length because smaller
243 values are already covered in do_random_tests function. */
244 do_one_test (impl
, (CHAR
*) (s
+ offset
), page_size
, exp_len
);
249 /* Tests meant to unveil fail on implementations that access bytes
250 beyond the maxium length. */
253 do_page_2_tests (void)
255 size_t i
, exp_len
, offset
;
256 size_t last_offset
= page_size
/ sizeof (CHAR
);
258 CHAR
*s
= (CHAR
*) buf2
;
259 MEMSET (s
, 65, last_offset
);
261 /* Place short strings ending at page boundary without the null
263 offset
= last_offset
;
264 for (i
= 0; i
< 128; i
++)
266 /* Decrease offset to stress several sizes and alignments. */
268 exp_len
= last_offset
- offset
;
269 FOR_EACH_IMPL (impl
, 0)
271 /* If an implementation goes beyond EXP_LEN, it will trigger
273 do_one_test (impl
, (CHAR
*) (s
+ offset
), exp_len
, exp_len
);
281 size_t i
, length
, char_per_page
;
286 FOR_EACH_IMPL (impl
, 0)
287 printf ("\t%s", impl
->name
);
290 for (i
= 1; i
< 8; ++i
)
292 do_test (0, i
, i
- 1, MIDDLE_CHAR
);
293 do_test (0, i
, i
, MIDDLE_CHAR
);
294 do_test (0, i
, i
+ 1, MIDDLE_CHAR
);
297 for (i
= 1; i
< 8; ++i
)
299 do_test (i
, i
, i
- 1, MIDDLE_CHAR
);
300 do_test (i
, i
, i
, MIDDLE_CHAR
);
301 do_test (i
, i
, i
+ 1, MIDDLE_CHAR
);
304 for (i
= 2; i
<= 10; ++i
)
306 do_test (0, 1 << i
, 5000, MIDDLE_CHAR
);
307 do_test (1, 1 << i
, 5000, MIDDLE_CHAR
);
310 for (i
= 1; i
< 8; ++i
)
311 do_test (0, i
, 5000, BIG_CHAR
);
313 for (i
= 1; i
< 8; ++i
)
314 do_test (i
, i
, 5000, BIG_CHAR
);
316 for (i
= 2; i
<= 10; ++i
)
318 do_test (0, 1 << i
, 5000, BIG_CHAR
);
319 do_test (1, 1 << i
, 5000, BIG_CHAR
);
322 char_per_page
= getpagesize () / sizeof (CHAR
);
324 for (i
= 0; i
<= 127; i
++)
325 for (length
= i
; length
<= 512; length
++)
327 do_test (i
, length
, 512, BIG_CHAR
);
328 do_test (char_per_page
- i
, length
, 512, BIG_CHAR
);
334 do_overflow_tests ();
338 #include <support/test-driver.c>