unistd: Improve fortify with clang
[glibc.git] / string / test-strcat.c
blob1accec790818705f8fbd467882a99be4e24a48f8
1 /* Test strcat functions.
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define TEST_MAIN
20 #ifndef WIDE
21 # define TEST_NAME "strcat"
22 #else
23 # define TEST_NAME "wcscat"
24 #endif /* WIDE */
25 #include "test-string.h"
27 #ifndef WIDE
28 # define STRCAT strcat
29 # define CHAR char
30 # define UCHAR unsigned char
31 # define sfmt "s"
32 # define SIMPLE_STRCAT simple_strcat
33 # define STRLEN strlen
34 # define STRCMP strcmp
35 # define MEMSET memset
36 # define MEMCPY memcpy
37 # define MEMCMP memcmp
38 # define BIG_CHAR CHAR_MAX
39 # define SMALL_CHAR 127
40 #else
41 # include <wchar.h>
42 # define STRCAT wcscat
43 # define CHAR wchar_t
44 # define UCHAR wchar_t
45 # define sfmt "ls"
46 # define SIMPLE_STRCAT simple_wcscat
47 # define STRLEN wcslen
48 # define STRCMP wcscmp
49 # define MEMSET wmemset
50 # define MEMCPY wmemcpy
51 # define MEMCMP wmemcmp
52 # define BIG_CHAR WCHAR_MAX
53 # define SMALL_CHAR 1273
54 #endif /* WIDE */
56 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
58 IMPL (STRCAT, 1)
60 /* Naive implementation to verify results. */
61 CHAR *
62 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
64 CHAR *ret = dst;
65 while (*dst++ != '\0');
66 --dst;
67 while ((*dst++ = *src++) != '\0');
68 return ret;
71 static void
72 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
74 size_t k = STRLEN (dst);
75 if (CALL (impl, dst, src) != dst)
77 error (0, 0, "Wrong result in function %s %p %p", impl->name,
78 CALL (impl, dst, src), dst);
79 ret = 1;
80 return;
83 if (STRCMP (dst + k, src) != 0)
85 error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
86 impl->name, dst, src);
87 ret = 1;
88 return;
92 static void
93 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
95 size_t i;
96 CHAR *s1, *s2;
98 align1 &= 7;
99 if ((align1 + len1) * sizeof (CHAR) >= page_size)
100 return;
102 align2 &= 7;
103 if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
104 return;
106 s1 = (CHAR *) (buf1) + align1;
107 s2 = (CHAR *) (buf2) + align2;
109 for (i = 0; i < len1; ++i)
110 s1[i] = 32 + 23 * i % (max_char - 32);
111 s1[len1] = '\0';
113 for (i = 0; i < len2; i++)
114 s2[i] = 32 + 23 * i % (max_char - 32);
116 FOR_EACH_IMPL (impl, 0)
118 s2[len2] = '\0';
119 do_one_test (impl, s2, s1);
123 static void
124 do_random_tests (void)
126 size_t i, j, n, align1, align2, len1, len2;
127 UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
128 UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
129 UCHAR *p3 = (UCHAR *) buf1;
130 UCHAR *res;
132 for (n = 0; n < ITERATIONS; n++)
134 align1 = random () & 31;
135 if (random () & 1)
136 align2 = random () & 31;
137 else
138 align2 = align1 + (random () & 24);
139 len1 = random () & 511;
140 if (len1 + align2 > 512)
141 len2 = random () & 7;
142 else
143 len2 = (512 - len1 - align2) * (random () & (1024 * 1024 - 1))
144 / (1024 * 1024);
145 j = align1;
146 if (align2 + len2 > j)
147 j = align2 + len2;
148 if (len1 + j >= 511)
149 len1 = 510 - j - (random () & 7);
150 if (len1 >= 512)
151 len1 = 0;
152 if (align1 + len1 < 512 - 8)
154 j = 510 - align1 - len1 - (random () & 31);
155 if (j > 0 && j < 512)
156 align1 += j;
158 j = len1 + align1 + 64;
159 if (j > 512)
160 j = 512;
161 for (i = 0; i < j; i++)
163 if (i == len1 + align1)
164 p1[i] = 0;
165 else
167 p1[i] = random () & BIG_CHAR;
168 if (i >= align1 && i < len1 + align1 && !p1[i])
169 p1[i] = (random () & SMALL_CHAR) + 3;
172 for (i = 0; i < len2; i++)
174 p3[i] = random () & BIG_CHAR;
175 if (!p3[i])
176 p3[i] = (random () & SMALL_CHAR) + 3;
178 p3[len2] = 0;
180 FOR_EACH_IMPL (impl, 1)
182 MEMSET (p2 - 64, '\1', align2 + 64);
183 MEMSET (p2 + align2 + len2 + 1, '\1', 512 - align2 - len2 - 1);
184 MEMCPY (p2 + align2, p3, len2 + 1);
185 res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2),
186 (CHAR *) (p1 + align1));
187 if (res != p2 + align2)
189 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd %zd) %p != %p",
190 n, impl->name, align1, align2, len1, len2, res,
191 p2 + align2);
192 ret = 1;
194 for (j = 0; j < align2 + 64; ++j)
196 if (p2[j - 64] != '\1')
198 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd, %zd)",
199 n, impl->name, align1, align2, len1, len2);
200 ret = 1;
201 break;
204 if (MEMCMP (p2 + align2, p3, len2))
206 error (0, 0, "Iteration %zd - garbage in string before, %s (%zd, %zd, %zd, %zd)",
207 n, impl->name, align1, align2, len1, len2);
208 ret = 1;
210 for (j = align2 + len1 + len2 + 1; j < 512; ++j)
212 if (p2[j] != '\1')
214 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd, %zd)",
215 n, impl->name, align1, align2, len1, len2);
216 ret = 1;
217 break;
220 if (MEMCMP (p1 + align1, p2 + align2 + len2, len1 + 1))
222 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
223 n, impl->name, align1, align2, len1, len2);
224 ret = 1;
231 test_main (void)
233 size_t i;
235 test_init ();
237 printf ("%28s", "");
238 FOR_EACH_IMPL (impl, 0)
239 printf ("\t%s", impl->name);
240 putchar ('\n');
242 for (i = 0; i < 16; ++i)
244 do_test (0, 0, i, i, SMALL_CHAR);
245 do_test (0, 0, i, i, BIG_CHAR);
246 do_test (0, i, i, i, SMALL_CHAR);
247 do_test (i, 0, i, i, BIG_CHAR);
250 for (i = 1; i < 8; ++i)
252 do_test (0, 0, 8 << i, 8 << i, SMALL_CHAR);
253 do_test (8 - i, 2 * i, 8 << i, 8 << i, SMALL_CHAR);
254 do_test (0, 0, 8 << i, 2 << i, SMALL_CHAR);
255 do_test (8 - i, 2 * i, 8 << i, 2 << i, SMALL_CHAR);
258 for (i = 1; i < 8; ++i)
260 do_test (i, 2 * i, 8 << i, 1, SMALL_CHAR);
261 do_test (2 * i, i, 8 << i, 1, BIG_CHAR);
262 do_test (i, i, 8 << i, 10, SMALL_CHAR);
263 do_test (i, i, 8 << i, 10, BIG_CHAR);
266 do_random_tests ();
267 return ret;
270 #include <support/test-driver.c>