Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#define _GNU_SOURCE 1
21
22/* Make sure we don't test the optimized inline functions if we want to
23 test the real implementation. */
24#undef __USE_STRING_INLINES
25
26#include <errno.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/mman.h>
31#include <sys/param.h>
32
33#ifndef CHAR
34# define L(c) c
35# define CHAR char
36# define MEMSET memset
37# define STRLEN strlen
38# define STRNLEN strnlen
39# define STRCHR strchr
40# define STRRCHR strrchr
41# define STRCPY strcpy
42# define STRNCPY strncpy
43# define MEMCMP memcmp
44# define STPCPY stpcpy
45# define STPNCPY stpncpy
46# define MEMCPY memcpy
47# define MEMPCPY mempcpy
48# define MEMCHR memchr
49# define STRCMP strcmp
50# define STRNCMP strncmp
51#endif
52
53
54#define STRINGIFY(s) STRINGIFY2 (s)
55#define STRINGIFY2(s) #s
56
57
58int
59do_test (void)
60{
61 size_t size = sysconf (_SC_PAGESIZE);
62 size_t nchars = size / sizeof (CHAR);
63 CHAR *adr;
64 CHAR *dest;
65 int result = 0;
66
67 adr = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
68 MAP_PRIVATE | MAP_ANON, -1, 0);
69 dest = (CHAR *) mmap (NULL, 3 * size, PROT_READ | PROT_WRITE,
70 MAP_PRIVATE | MAP_ANON, -1, 0);
71 if (adr == MAP_FAILED || dest == MAP_FAILED)
72 {
73 if (errno == ENOSYS)
74 puts ("No test, mmap not available.");
75 else
76 {
77 printf ("mmap failed: %m");
78 result = 1;
79 }
80 }
81 else
82 {
83 size_t inner, middle, outer, nchars64, max128;
84
85 if (nchars > 64)
86 nchars64 = nchars - 64;
87 else
88 nchars64 = 0;
89
90 if (nchars > 128)
91 max128 = nchars - 128;
92 else
93 max128 = 0;
94
95 mprotect (adr, size, PROT_NONE);
96 mprotect (adr + 2 * nchars, size, PROT_NONE);
97 adr += nchars;
98
99 mprotect (dest, size, PROT_NONE);
100 mprotect (dest + 2 * nchars, size, PROT_NONE);
101 dest += nchars;
102
103 MEMSET (adr, L('T'), nchars);
104
105 /* strlen/wcslen test */
106 for (outer = nchars - 1; outer >= max128; --outer)
107 {
108 for (inner = MAX (outer, nchars64); inner < nchars; ++inner)
109 {
110 adr[inner] = L('\0');
111
112 if (STRLEN (&adr[outer]) != (size_t) (inner - outer))
113 {
114 printf ("%s flunked for outer = %zu, inner = %zu\n",
115 STRINGIFY (STRLEN), outer, inner);
116 result = 1;
117 }
118
119 adr[inner] = L('T');
120 }
121 if (outer == 0)
122 break;
123 }
124
125 /* strnlen/wcsnlen test */
126 for (outer = nchars; outer >= max128; --outer)
127 {
128 for (inner = MAX (outer, nchars64); inner < nchars; ++inner)
129 {
130 adr[inner] = L('\0');
131
132 if (STRNLEN (&adr[outer], inner - outer + 1)
133 != (size_t) (inner - outer))
134 {
135 printf ("%s flunked for outer = %zu, inner = %zu\n",
136 STRINGIFY (STRNLEN), outer, inner);
137 result = 1;
138 }
139
140 adr[inner] = L('T');
141 }
142 if (outer == 0)
143 break;
144 }
145 for (outer = nchars; outer >= max128; --outer)
146 {
147 for (inner = MAX (outer, nchars64); inner <= nchars; ++inner)
148 {
149 if (STRNLEN (&adr[outer], inner - outer)
150 != (size_t) (inner - outer))
151 {
152 printf ("%s flunked bounded for outer = %zu, inner = %zu\n",
153 STRINGIFY (STRNLEN), outer, inner);
154 result = 1;
155 }
156 }
157 if (outer == 0)
158 break;
159 }
160
161 /* strchr/wcschr test */
162 for (outer = nchars - 1; outer >= max128; --outer)
163 {
164 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
165 {
166 for (inner = middle; inner < nchars; ++inner)
167 {
168 adr[middle] = L('V');
169 adr[inner] = L('\0');
170
171 CHAR *cp = STRCHR (&adr[outer], L('V'));
172
173 if ((inner == middle && cp != NULL)
174 || (inner != middle
175 && (cp - &adr[outer]) != middle - outer))
176 {
177 printf ("%s flunked for outer = %zu, middle = %zu, "
178 "inner = %zu\n",
179 STRINGIFY (STRCHR), outer, middle, inner);
180 result = 1;
181 }
182
183 adr[inner] = L('T');
184 adr[middle] = L('T');
185 }
186 }
187 if (outer == 0)
188 break;
189 }
190
191 /* Special test. */
192 adr[nchars - 1] = L('\0');
193 if (STRCHR (&adr[nchars - 1], L('\n')) != NULL)
194 {
195 printf ("%s flunked test of empty string at end of page\n",
196 STRINGIFY (STRCHR));
197 result = 1;
198 }
199
200 /* strrchr/wcsrchr test */
201 for (outer = nchars - 1; outer >= max128; --outer)
202 {
203 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
204 {
205 for (inner = middle; inner < nchars; ++inner)
206 {
207 adr[middle] = L('V');
208 adr[inner] = L('\0');
209
210 CHAR *cp = STRRCHR (&adr[outer], L('V'));
211
212 if ((inner == middle && cp != NULL)
213 || (inner != middle
214 && (cp - &adr[outer]) != middle - outer))
215 {
216 printf ("%s flunked for outer = %zu, middle = %zu, "
217 "inner = %zu\n",
218 STRINGIFY (STRRCHR), outer, middle, inner);
219 result = 1;
220 }
221
222 adr[inner] = L('T');
223 adr[middle] = L('T');
224 }
225 }
226 if (outer == 0)
227 break;
228 }
229
230 /* memchr test */
231 for (outer = nchars - 1; outer >= max128; --outer)
232 {
233 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
234 {
235 adr[middle] = L('V');
236
237 CHAR *cp = MEMCHR (&adr[outer], L('V'), 3 * size);
238
239 if (cp - &adr[outer] != middle - outer)
240 {
241 printf ("%s flunked for outer = %zu, middle = %zu\n",
242 STRINGIFY (MEMCHR), outer, middle);
243 result = 1;
244 }
245
246 adr[middle] = L('T');
247 }
248 if (outer == 0)
249 break;
250 }
251 for (outer = nchars; outer >= max128; --outer)
252 {
253 CHAR *cp = MEMCHR (&adr[outer], L('V'), nchars - outer);
254
255 if (cp != NULL)
256 {
257 printf ("%s flunked for outer = %zu\n",
258 STRINGIFY (MEMCHR), outer);
259 result = 1;
260 }
261 if (outer == 0)
262 break;
263 }
264
265 /* These functions only exist for single-byte characters. */
266#ifndef WCSTEST
267 /* rawmemchr test */
268 for (outer = nchars - 1; outer >= max128; --outer)
269 {
270 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
271 {
272 adr[middle] = L('V');
273
274 CHAR *cp = rawmemchr (&adr[outer], L('V'));
275
276 if (cp - &adr[outer] != middle - outer)
277 {
278 printf ("%s flunked for outer = %zu, middle = %zu\n",
279 STRINGIFY (rawmemchr), outer, middle);
280 result = 1;
281 }
282
283 adr[middle] = L('T');
284 }
285 if (outer == 0)
286 break;
287 }
288
289 /* memrchr test */
290 for (outer = nchars - 1; outer >= max128; --outer)
291 {
292 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
293 {
294 adr[middle] = L('V');
295
296 CHAR *cp = memrchr (&adr[outer], L('V'), nchars - outer);
297
298 if (cp - &adr[outer] != middle - outer)
299 {
300 printf ("%s flunked for outer = %zu, middle = %zu\n",
301 STRINGIFY (memrchr), outer, middle);
302 result = 1;
303 }
304
305 adr[middle] = L('T');
306 }
307 if (outer == 0)
308 break;
309 }
310 for (outer = nchars; outer >= max128; --outer)
311 {
312 CHAR *cp = memrchr (&adr[outer], L('V'), nchars - outer);
313
314 if (cp != NULL)
315 {
316 printf ("%s flunked for outer = %zu\n",
317 STRINGIFY (memrchr), outer);
318 result = 1;
319 }
320 if (outer == 0)
321 break;
322 }
323#endif
324
325 /* strcpy/wcscpy test */
326 for (outer = nchars - 1; outer >= max128; --outer)
327 {
328 for (inner = MAX (outer, nchars64); inner < nchars; ++inner)
329 {
330 adr[inner] = L('\0');
331
332 if (STRCPY (dest, &adr[outer]) != dest
333 || STRLEN (dest) != (size_t) (inner - outer))
334 {
335 printf ("%s flunked for outer = %zu, inner = %zu\n",
336 STRINGIFY (STRCPY), outer, inner);
337 result = 1;
338 }
339
340 adr[inner] = L('T');
341 }
342 if (outer == 0)
343 break;
344 }
345
346 /* strcmp/wcscmp tests */
347 for (outer = 1; outer < 32; ++outer)
348 for (middle = 0; middle < 16; ++middle)
349 {
350 MEMSET (adr + middle, L('T'), 256);
351 adr[256] = L('\0');
352 MEMSET (dest + nchars - outer, L('T'), outer - 1);
353 dest[nchars - 1] = L('\0');
354
355 if (STRCMP (adr + middle, dest + nchars - outer) <= 0)
356 {
357 printf ("%s 1 flunked for outer = %zu, middle = %zu\n",
358 STRINGIFY (STRCMP), outer, middle);
359 result = 1;
360 }
361
362 if (STRCMP (dest + nchars - outer, adr + middle) >= 0)
363 {
364 printf ("%s 2 flunked for outer = %zu, middle = %zu\n",
365 STRINGIFY (STRCMP), outer, middle);
366 result = 1;
367 }
368 }
369
370 /* strncmp/wcsncmp tests */
371 for (outer = 1; outer < 32; ++outer)
372 for (middle = 0; middle < 16; ++middle)
373 {
374 MEMSET (adr + middle, L('T'), 256);
375 adr[256] = L('\0');
376 MEMSET (dest + nchars - outer, L('T'), outer - 1);
377 dest[nchars - 1] = L('U');
378
379 for (inner = 0; inner < outer; ++inner)
380 {
381 if (STRNCMP (adr + middle, dest + nchars - outer, inner) != 0)
382 {
383 printf ("%s 1 flunked for outer = %zu, middle = %zu, "
384 "inner = %zu\n",
385 STRINGIFY (STRNCMP), outer, middle, inner);
386 result = 1;
387 }
388
389 if (STRNCMP (dest + nchars - outer, adr + middle, inner) != 0)
390 {
391 printf ("%s 2 flunked for outer = %zu, middle = %zu, "
392 "inner = %zu\n",
393 STRINGIFY (STRNCMP), outer, middle, inner);
394 result = 1;
395 }
396 }
397
398 if (STRNCMP (adr + middle, dest + nchars - outer, outer) >= 0)
399 {
400 printf ("%s 1 flunked for outer = %zu, middle = %zu, full\n",
401 STRINGIFY (STRNCMP), outer, middle);
402 result = 1;
403 }
404
405 if (STRNCMP (dest + nchars - outer, adr + middle, outer) <= 0)
406 {
407 printf ("%s 2 flunked for outer = %zu, middle = %zu, full\n",
408 STRINGIFY (STRNCMP), outer, middle);
409 result = 1;
410 }
411 }
412
413 /* strncpy/wcsncpy tests */
414 adr[nchars - 1] = L('T');
415 for (outer = nchars; outer >= max128; --outer)
416 {
417 size_t len;
418
419 for (len = 0; len < nchars - outer; ++len)
420 {
421 if (STRNCPY (dest, &adr[outer], len) != dest
422 || MEMCMP (dest, &adr[outer], len) != 0)
423 {
424 printf ("outer %s flunked for outer = %zu, len = %zu\n",
425 STRINGIFY (STRNCPY), outer, len);
426 result = 1;
427 }
428 }
429 if (outer == 0)
430 break;
431 }
432 adr[nchars - 1] = L('\0');
433
434 for (outer = nchars - 1; outer >= max128; --outer)
435 {
436 for (inner = MAX (outer, nchars64); inner < nchars; ++inner)
437 {
438 size_t len;
439
440 adr[inner] = L('\0');
441
442 for (len = 0; len < nchars - outer + 64; ++len)
443 {
444 if (STRNCPY (dest, &adr[outer], len) != dest
445 || MEMCMP (dest, &adr[outer],
446 MIN (inner - outer, len)) != 0
447 || (inner - outer < len
448 && STRLEN (dest) != (inner - outer)))
449 {
450 printf ("%s flunked for outer = %zu, inner = %zu, "
451 "len = %zu\n",
452 STRINGIFY (STRNCPY), outer, inner, len);
453 result = 1;
454 }
455 if (STRNCPY (dest + 1, &adr[outer], len) != dest + 1
456 || MEMCMP (dest + 1, &adr[outer],
457 MIN (inner - outer, len)) != 0
458 || (inner - outer < len
459 && STRLEN (dest + 1) != (inner - outer)))
460 {
461 printf ("%s+1 flunked for outer = %zu, inner = %zu, "
462 "len = %zu\n",
463 STRINGIFY (STRNCPY), outer, inner, len);
464 result = 1;
465 }
466 }
467
468 adr[inner] = L('T');
469 }
470 if (outer == 0)
471 break;
472 }
473
474 /* stpcpy/wcpcpy test */
475 for (outer = nchars - 1; outer >= max128; --outer)
476 {
477 for (inner = MAX (outer, nchars64); inner < nchars; ++inner)
478 {
479 adr[inner] = L('\0');
480
481 if ((STPCPY (dest, &adr[outer]) - dest) != inner - outer)
482 {
483 printf ("%s flunked for outer = %zu, inner = %zu\n",
484 STRINGIFY (STPCPY), outer, inner);
485 result = 1;
486 }
487
488 adr[inner] = L('T');
489 }
490 if (outer == 0)
491 break;
492 }
493
494 /* stpncpy/wcpncpy test */
495 adr[nchars - 1] = L('T');
496 for (outer = nchars; outer >= max128; --outer)
497 {
498 size_t len;
499
500 for (len = 0; len < nchars - outer; ++len)
501 {
502 if (STPNCPY (dest, &adr[outer], len) != dest + len
503 || MEMCMP (dest, &adr[outer], len) != 0)
504 {
505 printf ("outer %s flunked for outer = %zu, len = %zu\n",
506 STRINGIFY (STPNCPY), outer, len);
507 result = 1;
508 }
509 }
510 if (outer == 0)
511 break;
512 }
513 adr[nchars - 1] = L('\0');
514
515 for (outer = nchars - 1; outer >= max128; --outer)
516 {
517 for (middle = MAX (outer, nchars64); middle < nchars; ++middle)
518 {
519 adr[middle] = L('\0');
520
521 for (inner = 0; inner < nchars - outer; ++ inner)
522 {
523 if ((STPNCPY (dest, &adr[outer], inner) - dest)
524 != MIN (inner, middle - outer))
525 {
526 printf ("%s flunked for outer = %zu, middle = %zu, "
527 "inner = %zu\n",
528 STRINGIFY (STPNCPY), outer, middle, inner);
529 result = 1;
530 }
531 }
532
533 adr[middle] = L('T');
534 }
535 if (outer == 0)
536 break;
537 }
538
539 /* memcpy/wmemcpy test */
540 for (outer = nchars; outer >= max128; --outer)
541 {
542 for (inner = 0; inner < nchars - outer; ++inner)
543 if (MEMCPY (dest, &adr[outer], inner) != dest)
544 {
545 printf ("%s flunked for outer = %zu, inner = %zu\n",
546 STRINGIFY (MEMCPY), outer, inner);
547 result = 1;
548 }
549 if (outer == 0)
550 break;
551 }
552
553 /* mempcpy/wmempcpy test */
554 for (outer = nchars; outer >= max128; --outer)
555 {
556 for (inner = 0; inner < nchars - outer; ++inner)
557 if (MEMPCPY (dest, &adr[outer], inner) != dest + inner)
558 {
559 printf ("%s flunked for outer = %zu, inner = %zu\n",
560 STRINGIFY (MEMPCPY), outer, inner);
561 result = 1;
562 }
563 if (outer == 0)
564 break;
565 }
566
567 /* This function only exists for single-byte characters. */
568#ifndef WCSTEST
569 /* memccpy test */
570 memset (adr, '\0', nchars);
571 for (outer = nchars; outer >= max128; --outer)
572 {
573 for (inner = 0; inner < nchars - outer; ++inner)
574 if (memccpy (dest, &adr[outer], L('\1'), inner) != NULL)
575 {
576 printf ("memccpy flunked full copy for outer = %zu, inner = %zu\n",
577 outer, inner);
578 result = 1;
579 }
580 if (outer == 0)
581 break;
582 }
583 for (outer = nchars - 1; outer >= max128; --outer)
584 {
585 for (middle = 0; middle < nchars - outer; ++middle)
586 {
587 memset (dest, L('\2'), middle + 1);
588 for (inner = 0; inner < middle; ++inner)
589 {
590 adr[outer + inner] = L('\1');
591
592 if (memccpy (dest, &adr[outer], '\1', middle + 128)
593 != dest + inner + 1)
594 {
595 printf ("\
596 memccpy flunked partial copy for outer = %zu, middle = %zu, inner = %zu\n",
597 outer, middle, inner);
598 result = 1;
599 }
600 else if (dest[inner + 1] != L('\2'))
601 {
602 printf ("\
603 memccpy copied too much for outer = %zu, middle = %zu, inner = %zu\n",
604 outer, middle, inner);
605 result = 1;
606 }
607 adr[outer + inner] = L('\0');
608 }
609 }
610 if (outer == 0)
611 break;
612 }
613#endif
614 }
615
616 return result;
617}
618
619#include <support/test-driver.c>
620

Warning: That file was not part of the compilation database. It may have many parsing errors.