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

1/* Test strncpy functions.
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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#ifdef WIDE
21# include <wchar.h>
22# define CHAR wchar_t
23# define UCHAR wchar_t
24# define BIG_CHAR WCHAR_MAX
25# define SMALL_CHAR 1273
26# define MEMCMP wmemcmp
27# define MEMSET wmemset
28# define STRNLEN wcsnlen
29#else
30# define CHAR char
31# define UCHAR unsigned char
32# define BIG_CHAR CHAR_MAX
33# define SMALL_CHAR 127
34# define MEMCMP memcmp
35# define MEMSET memset
36# define STRNLEN strnlen
37#endif /* !WIDE */
38
39
40#ifndef STRNCPY_RESULT
41# define STRNCPY_RESULT(dst, len, n) dst
42# define TEST_MAIN
43# ifndef WIDE
44# define TEST_NAME "strncpy"
45# else
46# define TEST_NAME "wcsncpy"
47# endif /* WIDE */
48# include "test-string.h"
49# ifndef WIDE
50# define SIMPLE_STRNCPY simple_strncpy
51# define STUPID_STRNCPY stupid_strncpy
52# define STRNCPY strncpy
53# else
54# define SIMPLE_STRNCPY simple_wcsncpy
55# define STUPID_STRNCPY stupid_wcsncpy
56# define STRNCPY wcsncpy
57# endif /* WIDE */
58
59CHAR *SIMPLE_STRNCPY (CHAR *, const CHAR *, size_t);
60CHAR *STUPID_STRNCPY (CHAR *, const CHAR *, size_t);
61
62IMPL (STUPID_STRNCPY, 0)
63IMPL (SIMPLE_STRNCPY, 0)
64IMPL (STRNCPY, 1)
65
66CHAR *
67SIMPLE_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
68{
69 CHAR *ret = dst;
70 while (n--)
71 if ((*dst++ = *src++) == '\0')
72 {
73 while (n--)
74 *dst++ = '\0';
75 return ret;
76 }
77 return ret;
78}
79
80CHAR *
81STUPID_STRNCPY (CHAR *dst, const CHAR *src, size_t n)
82{
83 size_t nc = STRNLEN (src, n);
84 size_t i;
85
86 for (i = 0; i < nc; ++i)
87 dst[i] = src[i];
88 for (; i < n; ++i)
89 dst[i] = '\0';
90 return dst;
91}
92#endif /* !STRNCPY_RESULT */
93
94typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
95
96static void
97do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
98{
99 if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n))
100 {
101 error (0, 0, "Wrong result in function %s %p %p", impl->name,
102 CALL (impl, dst, src, n), dst);
103 ret = 1;
104 return;
105 }
106
107 if (memcmp (dst, src, (len > n ? n : len) * sizeof (CHAR)) != 0)
108 {
109 error (0, 0, "Wrong result in function %s", impl->name);
110 ret = 1;
111 return;
112 }
113
114 if (n > len)
115 {
116 size_t i;
117
118 for (i = len; i < n; ++i)
119 if (dst [i] != '\0')
120 {
121 error (0, 0, "Wrong result in function %s", impl->name);
122 ret = 1;
123 return;
124 }
125 }
126}
127
128static void
129do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
130{
131 size_t i;
132 CHAR *s1, *s2;
133
134/* For wcsncpy: align1 and align2 here mean alignment not in bytes,
135 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
136 align1 &= 7;
137 if ((align1 + len) * sizeof (CHAR) >= page_size)
138 return;
139
140 align2 &= 7;
141 if ((align2 + len) * sizeof (CHAR) >= page_size)
142 return;
143
144 s1 = (CHAR *) (buf1) + align1;
145 s2 = (CHAR *) (buf2) + align2;
146
147 for (i = 0; i < len; ++i)
148 s1[i] = 32 + 23 * i % (max_char - 32);
149 s1[len] = 0;
150 for (i = len + 1; (i + align1) * sizeof (CHAR) < page_size && i < len + 64;
151 ++i)
152 s1[i] = 32 + 32 * i % (max_char - 32);
153
154 FOR_EACH_IMPL (impl, 0)
155 do_one_test (impl, s2, s1, len, n);
156}
157
158static void
159do_random_tests (void)
160{
161 size_t i, j, n, align1, align2, len, size, mode;
162 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
163 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
164 UCHAR *res;
165
166 for (n = 0; n < ITERATIONS; n++)
167 {
168 /* For wcsncpy: align1 and align2 here mean align not in bytes,
169 but in wchar_ts, in bytes it will equal to align * (sizeof
170 (wchar_t)). */
171
172 mode = random ();
173 if (mode & 1)
174 {
175 size = random () & 255;
176 align1 = 512 - size - (random () & 15);
177 if (mode & 2)
178 align2 = align1 - (random () & 24);
179 else
180 align2 = align1 - (random () & 31);
181 if (mode & 4)
182 {
183 j = align1;
184 align1 = align2;
185 align2 = j;
186 }
187 if (mode & 8)
188 len = size - (random () & 31);
189 else
190 len = 512;
191 if (len >= 512)
192 len = random () & 511;
193 }
194 else
195 {
196 align1 = random () & 31;
197 if (mode & 2)
198 align2 = random () & 31;
199 else
200 align2 = align1 + (random () & 24);
201 len = random () & 511;
202 j = align1;
203 if (align2 > j)
204 j = align2;
205 if (mode & 4)
206 {
207 size = random () & 511;
208 if (size + j > 512)
209 size = 512 - j - (random () & 31);
210 }
211 else
212 size = 512 - j;
213 if ((mode & 8) && len + j >= 512)
214 len = 512 - j - (random () & 7);
215 }
216 j = len + align1 + 64;
217 if (j > 512)
218 j = 512;
219 for (i = 0; i < j; i++)
220 {
221 if (i == len + align1)
222 p1[i] = 0;
223 else
224 {
225 p1[i] = random () & BIG_CHAR;
226 if (i >= align1 && i < len + align1 && !p1[i])
227 p1[i] = (random () & SMALL_CHAR) + 3;
228 }
229 }
230
231 FOR_EACH_IMPL (impl, 1)
232 {
233 MEMSET (p2 - 64, '\1', 512 + 64);
234 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
235 (CHAR *) (p1 + align1), size);
236 if (res != STRNCPY_RESULT (p2 + align2, len, size))
237 {
238 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
239 n, impl->name, align1, align2, len, res,
240 STRNCPY_RESULT (p2 + align2, len, size));
241 ret = 1;
242 }
243 for (j = 0; j < align2 + 64; ++j)
244 {
245 if (p2[j - 64] != '\1')
246 {
247 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
248 n, impl->name, align1, align2, len);
249 ret = 1;
250 break;
251 }
252 }
253 j = align2 + len + 1;
254 if (size + align2 > j)
255 j = size + align2;
256 for (; j < 512; ++j)
257 {
258 if (p2[j] != '\1')
259 {
260 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
261 n, impl->name, align1, align2, len);
262 ret = 1;
263 break;
264 }
265 }
266 for (j = align2 + len + 1; j < align2 + size; ++j)
267 if (p2[j])
268 {
269 error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
270 n, impl->name, align1, align2, len);
271 ret = 1;
272 break;
273 }
274 j = len + 1;
275 if (size < j)
276 j = size;
277 if (MEMCMP (p1 + align1, p2 + align2, j))
278 {
279 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
280 n, impl->name, align1, align2, len);
281 ret = 1;
282 }
283 }
284 }
285}
286
287int
288test_main (void)
289{
290 size_t i;
291
292 test_init ();
293
294 printf ("%28s", "");
295 FOR_EACH_IMPL (impl, 0)
296 printf ("\t%s", impl->name);
297 putchar ('\n');
298
299 for (i = 1; i < 8; ++i)
300 {
301 do_test (i, i, 16, 16, SMALL_CHAR);
302 do_test (i, i, 16, 16, BIG_CHAR);
303 do_test (i, 2 * i, 16, 16, SMALL_CHAR);
304 do_test (2 * i, i, 16, 16, BIG_CHAR);
305 do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
306 do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
307 do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
308 do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
309 }
310
311 for (i = 1; i < 8; ++i)
312 {
313 do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
314 do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
315 do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
316 do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
317 }
318
319 do_random_tests ();
320 return ret;
321}
322
323#include <support/test-driver.c>
324

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