1 /* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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.
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.
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/>. */
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
31 #include <sys/param.h>
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
54 #define STRINGIFY(s) STRINGIFY2 (s)
55 #define STRINGIFY2(s) #s
61 int size
= sysconf (_SC_PAGESIZE
);
62 int nchars
= size
/ sizeof (CHAR
);
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
)
74 puts ("No test, mmap not available.");
77 printf ("mmap failed: %m");
83 int inner
, middle
, outer
;
85 mprotect (adr
, size
, PROT_NONE
);
86 mprotect (adr
+ 2 * nchars
, size
, PROT_NONE
);
89 mprotect (dest
, size
, PROT_NONE
);
90 mprotect (dest
+ 2 * nchars
, size
, PROT_NONE
);
93 MEMSET (adr
, L('T'), nchars
);
95 /* strlen/wcslen test */
96 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
98 for (inner
= MAX (outer
, nchars
- 64); inner
< nchars
; ++inner
)
100 adr
[inner
] = L('\0');
102 if (STRLEN (&adr
[outer
]) != (size_t) (inner
- outer
))
104 printf ("%s flunked for outer = %d, inner = %d\n",
105 STRINGIFY (STRLEN
), outer
, inner
);
113 /* strnlen/wcsnlen test */
114 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
116 for (inner
= MAX (outer
, nchars
- 64); inner
< nchars
; ++inner
)
118 adr
[inner
] = L('\0');
120 if (STRNLEN (&adr
[outer
], inner
- outer
+ 1)
121 != (size_t) (inner
- outer
))
123 printf ("%s flunked for outer = %d, inner = %d\n",
124 STRINGIFY (STRNLEN
), outer
, inner
);
131 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
133 for (inner
= MAX (outer
, nchars
- 64); inner
<= nchars
; ++inner
)
135 if (STRNLEN (&adr
[outer
], inner
- outer
)
136 != (size_t) (inner
- outer
))
138 printf ("%s flunked bounded for outer = %d, inner = %d\n",
139 STRINGIFY (STRNLEN
), outer
, inner
);
145 /* strchr/wcschr test */
146 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
148 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
150 for (inner
= middle
; inner
< nchars
; ++inner
)
152 adr
[middle
] = L('V');
153 adr
[inner
] = L('\0');
155 CHAR
*cp
= STRCHR (&adr
[outer
], L('V'));
157 if ((inner
== middle
&& cp
!= NULL
)
159 && (cp
- &adr
[outer
]) != middle
- outer
))
161 printf ("%s flunked for outer = %d, middle = %d, "
163 STRINGIFY (STRCHR
), outer
, middle
, inner
);
168 adr
[middle
] = L('T');
174 adr
[nchars
- 1] = L('\0');
175 if (STRCHR (&adr
[nchars
- 1], L('\n')) != NULL
)
177 printf ("%s flunked test of empty string at end of page\n",
182 /* strrchr/wcsrchr test */
183 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
185 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
187 for (inner
= middle
; inner
< nchars
; ++inner
)
189 adr
[middle
] = L('V');
190 adr
[inner
] = L('\0');
192 CHAR
*cp
= STRRCHR (&adr
[outer
], L('V'));
194 if ((inner
== middle
&& cp
!= NULL
)
196 && (cp
- &adr
[outer
]) != middle
- outer
))
198 printf ("%s flunked for outer = %d, middle = %d, "
200 STRINGIFY (STRRCHR
), outer
, middle
, inner
);
205 adr
[middle
] = L('T');
211 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
213 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
215 adr
[middle
] = L('V');
217 CHAR
*cp
= MEMCHR (&adr
[outer
], L('V'), 3 * size
);
219 if (cp
- &adr
[outer
] != middle
- outer
)
221 printf ("%s flunked for outer = %d, middle = %d\n",
222 STRINGIFY (MEMCHR
), outer
, middle
);
226 adr
[middle
] = L('T');
229 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
231 CHAR
*cp
= MEMCHR (&adr
[outer
], L('V'), nchars
- outer
);
235 printf ("%s flunked for outer = %d\n",
236 STRINGIFY (MEMCHR
), outer
);
241 /* These functions only exist for single-byte characters. */
244 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
246 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
248 adr
[middle
] = L('V');
250 CHAR
*cp
= rawmemchr (&adr
[outer
], L('V'));
252 if (cp
- &adr
[outer
] != middle
- outer
)
254 printf ("%s flunked for outer = %d, middle = %d\n",
255 STRINGIFY (rawmemchr
), outer
, middle
);
259 adr
[middle
] = L('T');
264 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
266 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
268 adr
[middle
] = L('V');
270 CHAR
*cp
= memrchr (&adr
[outer
], L('V'), nchars
- outer
);
272 if (cp
- &adr
[outer
] != middle
- outer
)
274 printf ("%s flunked for outer = %d, middle = %d\n",
275 STRINGIFY (memrchr
), outer
, middle
);
279 adr
[middle
] = L('T');
282 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
284 CHAR
*cp
= memrchr (&adr
[outer
], L('V'), nchars
- outer
);
288 printf ("%s flunked for outer = %d\n",
289 STRINGIFY (memrchr
), outer
);
295 /* strcpy/wcscpy test */
296 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
298 for (inner
= MAX (outer
, nchars
- 64); inner
< nchars
; ++inner
)
300 adr
[inner
] = L('\0');
302 if (STRCPY (dest
, &adr
[outer
]) != dest
303 || STRLEN (dest
) != (size_t) (inner
- outer
))
305 printf ("%s flunked for outer = %d, inner = %d\n",
306 STRINGIFY (STRCPY
), outer
, inner
);
314 /* strcmp/wcscmp tests */
315 for (outer
= 1; outer
< 32; ++outer
)
316 for (middle
= 0; middle
< 16; ++middle
)
318 MEMSET (adr
+ middle
, L('T'), 256);
320 MEMSET (dest
+ nchars
- outer
, L('T'), outer
- 1);
321 dest
[nchars
- 1] = L('\0');
323 if (STRCMP (adr
+ middle
, dest
+ nchars
- outer
) <= 0)
325 printf ("%s 1 flunked for outer = %d, middle = %d\n",
326 STRINGIFY (STRCMP
), outer
, middle
);
330 if (STRCMP (dest
+ nchars
- outer
, adr
+ middle
) >= 0)
332 printf ("%s 2 flunked for outer = %d, middle = %d\n",
333 STRINGIFY (STRCMP
), outer
, middle
);
338 /* strncmp/wcsncmp tests */
339 for (outer
= 1; outer
< 32; ++outer
)
340 for (middle
= 0; middle
< 16; ++middle
)
342 MEMSET (adr
+ middle
, L('T'), 256);
344 MEMSET (dest
+ nchars
- outer
, L('T'), outer
- 1);
345 dest
[nchars
- 1] = L('U');
347 for (inner
= 0; inner
< outer
; ++inner
)
349 if (STRNCMP (adr
+ middle
, dest
+ nchars
- outer
, inner
) != 0)
351 printf ("%s 1 flunked for outer = %d, middle = %d, "
353 STRINGIFY (STRNCMP
), outer
, middle
, inner
);
357 if (STRNCMP (dest
+ nchars
- outer
, adr
+ middle
, inner
) != 0)
359 printf ("%s 2 flunked for outer = %d, middle = %d, "
361 STRINGIFY (STRNCMP
), outer
, middle
, inner
);
366 if (STRNCMP (adr
+ middle
, dest
+ nchars
- outer
, outer
) >= 0)
368 printf ("%s 1 flunked for outer = %d, middle = %d, full\n",
369 STRINGIFY (STRNCMP
), outer
, middle
);
373 if (STRNCMP (dest
+ nchars
- outer
, adr
+ middle
, outer
) <= 0)
375 printf ("%s 2 flunked for outer = %d, middle = %d, full\n",
376 STRINGIFY (STRNCMP
), outer
, middle
);
381 /* strncpy/wcsncpy tests */
382 adr
[nchars
- 1] = L('T');
383 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
387 for (len
= 0; len
< nchars
- outer
; ++len
)
389 if (STRNCPY (dest
, &adr
[outer
], len
) != dest
390 || MEMCMP (dest
, &adr
[outer
], len
) != 0)
392 printf ("outer %s flunked for outer = %d, len = %Zd\n",
393 STRINGIFY (STRNCPY
), outer
, len
);
398 adr
[nchars
- 1] = L('\0');
400 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
402 for (inner
= MAX (outer
, nchars
- 64); inner
< nchars
; ++inner
)
406 adr
[inner
] = L('\0');
408 for (len
= 0; len
< nchars
- outer
+ 64; ++len
)
410 if (STRNCPY (dest
, &adr
[outer
], len
) != dest
411 || MEMCMP (dest
, &adr
[outer
],
412 MIN (inner
- outer
, len
)) != 0
413 || (inner
- outer
< len
414 && STRLEN (dest
) != (inner
- outer
)))
416 printf ("%s flunked for outer = %d, inner = %d, "
418 STRINGIFY (STRNCPY
), outer
, inner
, len
);
421 if (STRNCPY (dest
+ 1, &adr
[outer
], len
) != dest
+ 1
422 || MEMCMP (dest
+ 1, &adr
[outer
],
423 MIN (inner
- outer
, len
)) != 0
424 || (inner
- outer
< len
425 && STRLEN (dest
+ 1) != (inner
- outer
)))
427 printf ("%s+1 flunked for outer = %d, inner = %d, "
429 STRINGIFY (STRNCPY
), outer
, inner
, len
);
438 /* stpcpy/wcpcpy test */
439 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
441 for (inner
= MAX (outer
, nchars
- 64); inner
< nchars
; ++inner
)
443 adr
[inner
] = L('\0');
445 if ((STPCPY (dest
, &adr
[outer
]) - dest
) != inner
- outer
)
447 printf ("%s flunked for outer = %d, inner = %d\n",
448 STRINGIFY (STPCPY
), outer
, inner
);
456 /* stpncpy/wcpncpy test */
457 adr
[nchars
- 1] = L('T');
458 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
462 for (len
= 0; len
< nchars
- outer
; ++len
)
464 if (STPNCPY (dest
, &adr
[outer
], len
) != dest
+ len
465 || MEMCMP (dest
, &adr
[outer
], len
) != 0)
467 printf ("outer %s flunked for outer = %d, len = %Zd\n",
468 STRINGIFY (STPNCPY
), outer
, len
);
473 adr
[nchars
- 1] = L('\0');
475 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
477 for (middle
= MAX (outer
, nchars
- 64); middle
< nchars
; ++middle
)
479 adr
[middle
] = L('\0');
481 for (inner
= 0; inner
< nchars
- outer
; ++ inner
)
483 if ((STPNCPY (dest
, &adr
[outer
], inner
) - dest
)
484 != MIN (inner
, middle
- outer
))
486 printf ("%s flunked for outer = %d, middle = %d, "
488 STRINGIFY (STPNCPY
), outer
, middle
, inner
);
493 adr
[middle
] = L('T');
497 /* memcpy/wmemcpy test */
498 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
499 for (inner
= 0; inner
< nchars
- outer
; ++inner
)
500 if (MEMCPY (dest
, &adr
[outer
], inner
) != dest
)
502 printf ("%s flunked for outer = %d, inner = %d\n",
503 STRINGIFY (MEMCPY
), outer
, inner
);
507 /* mempcpy/wmempcpy test */
508 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
509 for (inner
= 0; inner
< nchars
- outer
; ++inner
)
510 if (MEMPCPY (dest
, &adr
[outer
], inner
) != dest
+ inner
)
512 printf ("%s flunked for outer = %d, inner = %d\n",
513 STRINGIFY (MEMPCPY
), outer
, inner
);
517 /* This function only exists for single-byte characters. */
520 memset (adr
, '\0', nchars
);
521 for (outer
= nchars
; outer
>= MAX (0, nchars
- 128); --outer
)
522 for (inner
= 0; inner
< nchars
- outer
; ++inner
)
523 if (memccpy (dest
, &adr
[outer
], L('\1'), inner
) != NULL
)
525 printf ("memccpy flunked full copy for outer = %d, inner = %d\n",
529 for (outer
= nchars
- 1; outer
>= MAX (0, nchars
- 128); --outer
)
530 for (middle
= 0; middle
< nchars
- outer
; ++middle
)
532 memset (dest
, L('\2'), middle
+ 1);
533 for (inner
= 0; inner
< middle
; ++inner
)
535 adr
[outer
+ inner
] = L('\1');
537 if (memccpy (dest
, &adr
[outer
], '\1', middle
+ 128)
541 memccpy flunked partial copy for outer = %d, middle = %d, inner = %d\n",
542 outer
, middle
, inner
);
545 else if (dest
[inner
+ 1] != L('\2'))
548 memccpy copied too much for outer = %d, middle = %d, inner = %d\n",
549 outer
, middle
, inner
);
552 adr
[outer
+ inner
] = L('\0');
561 #define TEST_FUNCTION do_test ()
562 #include "../test-skeleton.c"