elf: Fix use-after-free in ldconfig [BZ #26779]
[glibc.git] / string / test-strncat.c
blob86ec101258f89fbb600816de206de19899c745fb
1 /* Test strncat functions.
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #define TEST_MAIN
19 #ifndef WIDE
20 # define TEST_NAME "strncat"
21 #else
22 # define TEST_NAME "wcsncat"
23 #endif /* WIDE */
24 #include "test-string.h"
26 #ifndef WIDE
27 # define STRNCAT strncat
28 # define CHAR char
29 # define UCHAR unsigned char
30 # define SIMPLE_STRNCAT simple_strncat
31 # define STUPID_STRNCAT stupid_strncat
32 # define STRLEN strlen
33 # define MEMSET memset
34 # define MEMCPY memcpy
35 # define MEMCMP memcmp
36 # define BIG_CHAR CHAR_MAX
37 # define SMALL_CHAR 127
38 #else
39 # include <wchar.h>
40 # define STRNCAT wcsncat
41 # define CHAR wchar_t
42 # define UCHAR wchar_t
43 # define SIMPLE_STRNCAT simple_wcsncat
44 # define STUPID_STRNCAT stupid_wcsncat
45 # define STRLEN wcslen
46 # define MEMSET wmemset
47 # define MEMCPY wmemcpy
48 # define MEMCMP wmemcmp
49 # define BIG_CHAR WCHAR_MAX
50 # define SMALL_CHAR 1273
51 #endif /* WIDE */
53 typedef CHAR *(*proto_t) (CHAR *, const CHAR *, size_t);
54 CHAR *STUPID_STRNCAT (CHAR *, const CHAR *, size_t);
55 CHAR *SIMPLE_STRNCAT (CHAR *, const CHAR *, size_t);
57 IMPL (STUPID_STRNCAT, 0)
58 IMPL (STRNCAT, 2)
60 CHAR *
61 STUPID_STRNCAT (CHAR *dst, const CHAR *src, size_t n)
63 CHAR *ret = dst;
64 while (*dst++ != '\0');
65 --dst;
66 while (n--)
67 if ((*dst++ = *src++) == '\0')
68 return ret;
69 *dst = '\0';
70 return ret;
73 static void
74 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t n)
76 size_t k = STRLEN (dst);
77 if (CALL (impl, dst, src, n) != dst)
79 error (0, 0, "Wrong result in function %s %p != %p", impl->name,
80 CALL (impl, dst, src, n), dst);
81 ret = 1;
82 return;
85 size_t len = STRLEN (src);
86 if (MEMCMP (dst + k, src, len + 1 > n ? n : len + 1) != 0)
88 error (0, 0, "Incorrect concatenation in function %s",
89 impl->name);
90 ret = 1;
91 return;
93 if (n < len && dst[k + n] != '\0')
95 error (0, 0, "There is no zero in the end of output string in %s",
96 impl->name);
97 ret = 1;
98 return;
102 static void
103 do_test (size_t align1, size_t align2, size_t len1, size_t len2,
104 size_t n, int max_char)
106 size_t i;
107 CHAR *s1, *s2;
109 align1 &= 7;
110 if ((align1 + len1) * sizeof (CHAR) >= page_size)
111 return;
112 if ((align1 + n) * sizeof (CHAR) > page_size)
113 return;
114 align2 &= 7;
115 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
116 return;
117 if ((align2 + len1 + n) * sizeof (CHAR) > page_size)
118 return;
119 s1 = (CHAR *) (buf1) + align1;
120 s2 = (CHAR *) (buf2) + align2;
122 for (i = 0; i < len1; ++i)
123 s1[i] = 32 + 23 * i % (max_char - 32);
124 s1[len1] = '\0';
126 for (i = 0; i < len2; i++)
127 s2[i] = 32 + 23 * i % (max_char - 32);
129 FOR_EACH_IMPL (impl, 0)
131 s2[len2] = '\0';
132 do_one_test (impl, s2, s1, n);
136 static void
137 do_overflow_tests (void)
139 size_t i, j, len;
140 const size_t one = 1;
141 CHAR *s1, *s2;
142 uintptr_t s1_addr;
143 s1 = (CHAR *) buf1;
144 s2 = (CHAR *) buf2;
145 s1_addr = (uintptr_t)s1;
146 for (j = 0; j < 200; ++j)
147 s2[j] = 32 + 23 * j % (BIG_CHAR - 32);
148 s2[200] = 0;
149 for (i = 0; i < 750; ++i) {
150 for (j = 0; j < i; ++j)
151 s1[j] = 32 + 23 * j % (BIG_CHAR - 32);
152 s1[i] = '\0';
154 FOR_EACH_IMPL (impl, 0)
156 s2[200] = '\0';
157 do_one_test (impl, s2, s1, SIZE_MAX - i);
158 s2[200] = '\0';
159 do_one_test (impl, s2, s1, i - s1_addr);
160 s2[200] = '\0';
161 do_one_test (impl, s2, s1, -s1_addr - i);
162 s2[200] = '\0';
163 do_one_test (impl, s2, s1, SIZE_MAX - s1_addr - i);
164 s2[200] = '\0';
165 do_one_test (impl, s2, s1, SIZE_MAX - s1_addr + i);
168 len = 0;
169 for (j = 8 * sizeof(size_t) - 1; j ; --j)
171 len |= one << j;
172 FOR_EACH_IMPL (impl, 0)
174 s2[200] = '\0';
175 do_one_test (impl, s2, s1, len - i);
176 s2[200] = '\0';
177 do_one_test (impl, s2, s1, len + i);
178 s2[200] = '\0';
179 do_one_test (impl, s2, s1, len - s1_addr - i);
180 s2[200] = '\0';
181 do_one_test (impl, s2, s1, len - s1_addr + i);
183 s2[200] = '\0';
184 do_one_test (impl, s2, s1, ~len - i);
185 s2[200] = '\0';
186 do_one_test (impl, s2, s1, ~len + i);
187 s2[200] = '\0';
188 do_one_test (impl, s2, s1, ~len - s1_addr - i);
189 s2[200] = '\0';
190 do_one_test (impl, s2, s1, ~len - s1_addr + i);
196 static void
197 do_random_tests (void)
199 size_t i, j, n, align1, align2, len1, len2, N;
200 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
201 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
202 UCHAR *p3 = (UCHAR *) buf1;
203 UCHAR *res;
204 fprintf (stdout, "Number of iterations in random test = %zd\n",
205 ITERATIONS);
206 for (n = 0; n < ITERATIONS; n++)
208 N = random () & 255;
209 align1 = random () & 31;
210 if (random () & 1)
211 align2 = random () & 31;
212 else
213 align2 = align1 + (random () & 24);
214 len1 = random () & 511;
215 if (len1 + align2 > 512)
216 len2 = random () & 7;
217 else
218 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
219 / (1024 * 1024);
220 j = align1;
221 if (align2 + len2 > j)
222 j = align2 + len2;
223 if (len1 + j >= 511)
224 len1 = 510 - j - (random () & 7);
225 if (len1 >= 512)
226 len1 = 0;
227 if (align1 + len1 < 512 - 8)
229 j = 510 - align1 - len1 - (random () & 31);
230 if (j > 0 && j < 512)
231 align1 += j;
233 j = len1 + align1 + 64;
234 if (j > 512)
235 j = 512;
236 for (i = 0; i < j; i++)
238 if (i == len1 + align1)
239 p1[i] = 0;
240 else
242 p1[i] = random () & BIG_CHAR;
243 if (i >= align1 && i < len1 + align1 && !p1[i])
244 p1[i] = (random () & SMALL_CHAR) + 3;
247 for (i = 0; i < len2; i++)
249 p3[i] = random () & BIG_CHAR;
250 if (!p3[i])
251 p3[i] = (random () & SMALL_CHAR) + 3;
253 p3[len2] = 0;
255 FOR_EACH_IMPL (impl, 1)
257 MEMSET (p2 - 64, '\1', align2 + 64);
258 MEMSET (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
259 MEMCPY (p2 + align2, p3, len2 + 1);
260 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
261 (CHAR *) (p1 + align1), N);
262 if (res != p2 + align2)
264 error (0, 0, "Iteration %zd - wrong result in function %s "
265 "(%zd, %zd, %zd, %zd, %zd) %p != %p",
266 n, impl->name, align1, align2, len1, len2, N,
267 res, p2 + align2);
268 ret = 1;
270 for (j = 0; j < align2 + 64; ++j)
272 if (p2[j - 64] != '\1')
274 error (0, 0, "Iteration %zd - garbage before dst, %s "
275 "%zd, %zd, %zd, %zd, %zd)",
276 n, impl->name, align1, align2, len1, len2, N);
277 ret = 1;
278 break;
281 if (MEMCMP (p2 + align2, p3, len2))
283 error (0, 0, "Iteration %zd - garbage in string before, %s "
284 "(%zd, %zd, %zd, %zd, %zd)",
285 n, impl->name, align1, align2, len1, len2, N);
286 ret = 1;
289 if ((len1 + 1) > N)
290 j = align2 + N + 1 + len2;
291 else
292 j = align2 + len1 + 1 + len2;
293 for (; j < 512; ++j)
295 if (p2[j] != '\1')
297 error (0, 0, "Iteration %zd - garbage after, %s "
298 "(%zd, %zd, %zd, %zd, %zd)",
299 n, impl->name, align1, align2, len1, len2, N);
300 ret = 1;
301 break;
304 if (len1 + 1 > N)
306 if (p2[align2 + N + len2] != '\0')
308 error (0, 0, "Iteration %zd - there is no zero at the "
309 "end of output string, %s (%zd, %zd, %zd, %zd, %zd)",
310 n, impl->name, align1, align2, len1, len2, N);
311 ret = 1;
314 if (MEMCMP (p1 + align1, p2 + align2 + len2,
315 (len1 + 1) > N ? N : len1 + 1))
317 error (0, 0, "Iteration %zd - different strings, %s "
318 "(%zd, %zd, %zd, %zd, %zd)",
319 n, impl->name, align1, align2, len1, len2, N);
320 ret = 1;
327 test_main (void)
329 size_t i, n;
331 test_init ();
333 printf ("%28s", "");
334 FOR_EACH_IMPL (impl, 0)
335 printf ("\t%s", impl->name);
336 putchar ('\n');
338 for (n = 2; n <= 2048; n*=4)
340 do_test (0, 2, 2, 2, n, SMALL_CHAR);
341 do_test (0, 0, 4, 4, n, SMALL_CHAR);
342 do_test (4, 0, 4, 4, n, BIG_CHAR);
343 do_test (0, 0, 8, 8, n, SMALL_CHAR);
344 do_test (0, 8, 8, 8, n, SMALL_CHAR);
346 do_test (0, 2, 2, 2, SIZE_MAX, SMALL_CHAR);
347 do_test (0, 0, 4, 4, SIZE_MAX, SMALL_CHAR);
348 do_test (4, 0, 4, 4, SIZE_MAX, BIG_CHAR);
349 do_test (0, 0, 8, 8, SIZE_MAX, SMALL_CHAR);
350 do_test (0, 8, 8, 8, SIZE_MAX, SMALL_CHAR);
352 for (i = 1; i < 8; ++i)
354 do_test (0, 0, 8 << i, 8 << i, n, SMALL_CHAR);
355 do_test (8 - i, 2 * i, 8 << i, 8 << i, n, SMALL_CHAR);
356 do_test (0, 0, 8 << i, 2 << i, n, SMALL_CHAR);
357 do_test (8 - i, 2 * i, 8 << i, 2 << i, n, SMALL_CHAR);
359 do_test (0, 0, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR);
360 do_test (8 - i, 2 * i, 8 << i, 8 << i, SIZE_MAX, SMALL_CHAR);
361 do_test (0, 0, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR);
362 do_test (8 - i, 2 * i, 8 << i, 2 << i, SIZE_MAX, SMALL_CHAR);
365 for (i = 1; i < 8; ++i)
367 do_test (i, 2 * i, 8 << i, 1, n, SMALL_CHAR);
368 do_test (2 * i, i, 8 << i, 1, n, BIG_CHAR);
369 do_test (i, i, 8 << i, 10, n, SMALL_CHAR);
371 do_test (i, 2 * i, 8 << i, 1, SIZE_MAX, SMALL_CHAR);
372 do_test (2 * i, i, 8 << i, 1, SIZE_MAX, BIG_CHAR);
373 do_test (i, i, 8 << i, 10, SIZE_MAX, SMALL_CHAR);
377 do_random_tests ();
378 do_overflow_tests ();
379 return ret;
382 #include <support/test-driver.c>