1 /* Test strcat functions.
2    Copyright (C) 1999-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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.
9 
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.
14 
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/>.  */
18 
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"
26 
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 */
55 
56 typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
57 
58 IMPL (STRCAT, 1)
59 
60 /* Naive implementation to verify results.  */
61 CHAR *
SIMPLE_STRCAT(CHAR * dst,const CHAR * src)62 SIMPLE_STRCAT (CHAR *dst, const CHAR *src)
63 {
64   CHAR *ret = dst;
65   while (*dst++ != '\0');
66   --dst;
67   while ((*dst++ = *src++) != '\0');
68   return ret;
69 }
70 
71 static void
do_one_test(impl_t * impl,CHAR * dst,const CHAR * src)72 do_one_test (impl_t *impl, CHAR *dst, const CHAR *src)
73 {
74   size_t k = STRLEN (dst);
75   if (CALL (impl, dst, src) != dst)
76     {
77       error (0, 0, "Wrong result in function %s %p %p", impl->name,
78 	     CALL (impl, dst, src), dst);
79       ret = 1;
80       return;
81     }
82 
83   if (STRCMP (dst + k, src) != 0)
84     {
85       error (0, 0, "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
86 	     impl->name, dst, src);
87       ret = 1;
88       return;
89     }
90 }
91 
92 static void
do_test(size_t align1,size_t align2,size_t len1,size_t len2,int max_char)93 do_test (size_t align1, size_t align2, size_t len1, size_t len2, int max_char)
94 {
95   size_t i;
96   CHAR *s1, *s2;
97 
98   align1 &= 7;
99   if ((align1 + len1) * sizeof (CHAR) >= page_size)
100     return;
101 
102   align2 &= 7;
103   if ((align2 + len1 + len2) * sizeof (CHAR) >= page_size)
104     return;
105 
106   s1 = (CHAR *) (buf1) + align1;
107   s2 = (CHAR *) (buf2) + align2;
108 
109   for (i = 0; i < len1; ++i)
110     s1[i] = 32 + 23 * i % (max_char - 32);
111   s1[len1] = '\0';
112 
113   for (i = 0; i < len2; i++)
114     s2[i] = 32 + 23 * i % (max_char - 32);
115 
116   FOR_EACH_IMPL (impl, 0)
117     {
118       s2[len2] = '\0';
119       do_one_test (impl, s2, s1);
120     }
121 }
122 
123 static void
do_random_tests(void)124 do_random_tests (void)
125 {
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;
131 
132   for (n = 0; n < ITERATIONS; n++)
133     {
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)
153 	{
154 	  j = 510 - align1 - len1 - (random () & 31);
155 	  if (j > 0 && j < 512)
156 	    align1 += j;
157 	}
158       j = len1 + align1 + 64;
159       if (j > 512)
160 	j = 512;
161       for (i = 0; i < j; i++)
162 	{
163 	  if (i == len1 + align1)
164 	    p1[i] = 0;
165 	  else
166 	    {
167 	      p1[i] = random () & BIG_CHAR;
168 	      if (i >= align1 && i < len1 + align1 && !p1[i])
169 		p1[i] = (random () & SMALL_CHAR) + 3;
170 	    }
171 	}
172       for (i = 0; i < len2; i++)
173 	{
174 	  p3[i] = random () & BIG_CHAR;
175 	  if (!p3[i])
176 	    p3[i] = (random () & SMALL_CHAR) + 3;
177 	}
178       p3[len2] = 0;
179 
180       FOR_EACH_IMPL (impl, 1)
181 	{
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)
188 	    {
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;
193 	    }
194 	  for (j = 0; j < align2 + 64; ++j)
195 	    {
196 	      if (p2[j - 64] != '\1')
197 		{
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;
202 		}
203 	    }
204 	  if (MEMCMP (p2 + align2, p3, len2))
205 	    {
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;
209 	    }
210 	  for (j = align2 + len1 + len2 + 1; j < 512; ++j)
211 	    {
212 	      if (p2[j] != '\1')
213 		{
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;
218 		}
219 	    }
220 	  if (MEMCMP (p1 + align1, p2 + align2 + len2, len1 + 1))
221 	    {
222 	      error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd, %zd)",
223 		     n, impl->name, align1, align2, len1, len2);
224 	      ret = 1;
225 	    }
226 	}
227     }
228 }
229 
230 int
test_main(void)231 test_main (void)
232 {
233   size_t i;
234 
235   test_init ();
236 
237   printf ("%28s", "");
238   FOR_EACH_IMPL (impl, 0)
239     printf ("\t%s", impl->name);
240   putchar ('\n');
241 
242   for (i = 0; i < 16; ++i)
243     {
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);
248     }
249 
250   for (i = 1; i < 8; ++i)
251     {
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);
256     }
257 
258   for (i = 1; i < 8; ++i)
259     {
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);
264     }
265 
266   do_random_tests ();
267   return ret;
268 }
269 
270 #include <support/test-driver.c>
271