1 /* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996,1997,1999,2000,2001,2002 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* Make sure we don't test the optimized inline functions if we want to
24 test the real implementation. */
25 #undef __USE_STRING_INLINES
32 #include <sys/param.h>
35 #define MAX(a, b) ((a) > (b) ? (a) : (b))
39 main (int argc
, char *argv
[])
41 int size
= sysconf (_SC_PAGESIZE
);
45 adr
= (char *) mmap (NULL
, 3 * size
, PROT_READ
| PROT_WRITE
,
46 MAP_PRIVATE
| MAP_ANON
, -1, 0);
47 dest
= (char *) mmap (NULL
, 3 * size
, PROT_READ
| PROT_WRITE
,
48 MAP_PRIVATE
| MAP_ANON
, -1, 0);
49 if (adr
== MAP_FAILED
|| dest
== MAP_FAILED
)
52 puts ("No test, mmap not available.");
55 printf ("mmap failed: %m");
61 int inner
, middle
, outer
;
63 mprotect(adr
, size
, PROT_NONE
);
64 mprotect(adr
+ 2 * size
, size
, PROT_NONE
);
67 mprotect(dest
, size
, PROT_NONE
);
68 mprotect(dest
+ 2 * size
, size
, PROT_NONE
);
71 memset (adr
, 'T', size
);
74 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
76 for (inner
= MAX (outer
, size
- 64); inner
< size
; ++inner
)
80 if (strlen (&adr
[outer
]) != (size_t) (inner
- outer
))
82 printf ("strlen flunked for outer = %d, inner = %d\n",
92 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
94 for (middle
= MAX (outer
, size
- 64); middle
< size
; ++middle
)
96 for (inner
= middle
; inner
< size
; ++inner
)
102 cp
= strchr (&adr
[outer
], 'V');
104 if ((inner
== middle
&& cp
!= NULL
)
106 && (cp
- &adr
[outer
]) != middle
- outer
))
108 printf ("strchr flunked for outer = %d, middle = %d, "
109 "inner = %d\n", outer
, middle
, inner
);
120 adr
[size
- 1] = '\0';
121 if (strchr (&adr
[size
- 1], '\n') != NULL
)
123 puts ("strchr flunked for test of empty string at end of page");
128 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
130 for (middle
= MAX (outer
, size
- 64); middle
< size
; ++middle
)
132 for (inner
= middle
; inner
< size
; ++inner
)
138 cp
= strrchr (&adr
[outer
], 'V');
140 if ((inner
== middle
&& cp
!= NULL
)
142 && (cp
- &adr
[outer
]) != middle
- outer
))
144 printf ("strrchr flunked for outer = %d, middle = %d, "
145 "inner = %d\n", outer
, middle
, inner
);
156 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
158 for (middle
= MAX (outer
, size
- 64); middle
< size
; ++middle
)
163 cp
= rawmemchr (&adr
[outer
], 'V');
165 if (cp
- &adr
[outer
] != middle
- outer
)
167 printf ("rawmemchr flunked for outer = %d, middle = %d\n",
177 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
179 for (inner
= MAX (outer
, size
- 64); inner
< size
; ++inner
)
183 if (strcpy (dest
, &adr
[outer
]) != dest
184 || strlen (dest
) != (size_t) (inner
- outer
))
186 printf ("strcpy flunked for outer = %d, inner = %d\n",
197 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
201 for (len
= 0; len
< size
- outer
; ++len
)
203 if (strncpy (dest
, &adr
[outer
], len
) != dest
204 || memcmp (dest
, &adr
[outer
], len
) != 0)
206 printf ("outer strncpy flunked for outer = %d, len = %Zd\n",
214 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
216 for (inner
= MAX (outer
, size
- 64); inner
< size
; ++inner
)
222 for (len
= 0; len
< size
- outer
+ 64; ++len
)
224 if (strncpy (dest
, &adr
[outer
], len
) != dest
225 || memcmp (dest
, &adr
[outer
],
226 MIN (inner
- outer
, len
)) != 0
227 || (inner
- outer
< len
228 && strlen (dest
) != (inner
- outer
)))
230 printf ("strncpy flunked for outer = %d, inner = %d, len = %Zd\n",
234 if (strncpy (dest
+ 1, &adr
[outer
], len
) != dest
+ 1
235 || memcmp (dest
+ 1, &adr
[outer
],
236 MIN (inner
- outer
, len
)) != 0
237 || (inner
- outer
< len
238 && strlen (dest
+ 1) != (inner
- outer
)))
240 printf ("strncpy+1 flunked for outer = %d, inner = %d, len = %Zd\n",
251 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
253 for (inner
= MAX (outer
, size
- 64); inner
< size
; ++inner
)
257 if ((stpcpy (dest
, &adr
[outer
]) - dest
) != inner
- outer
)
259 printf ("stpcpy flunked for outer = %d, inner = %d\n",
269 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
271 for (middle
= MAX (outer
, size
- 64); middle
< size
; ++middle
)
275 for (inner
= 0; inner
< size
- outer
; ++ inner
)
277 if ((stpncpy (dest
, &adr
[outer
], inner
) - dest
)
278 != MIN (inner
, middle
- outer
))
280 printf ("stpncpy flunked for outer = %d, middle = %d, "
281 "inner = %d\n", outer
, middle
, inner
);
291 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
292 for (inner
= 0; inner
< size
- outer
; ++inner
)
293 if (memcpy (dest
, &adr
[outer
], inner
) != dest
)
295 printf ("memcpy flunked for outer = %d, inner = %d\n",
301 for (outer
= size
- 1; outer
>= MAX (0, size
- 128); --outer
)
302 for (inner
= 0; inner
< size
- outer
; ++inner
)
303 if (mempcpy (dest
, &adr
[outer
], inner
) != dest
+ inner
)
305 printf ("mempcpy flunked for outer = %d, inner = %d\n",