sunrpc: Test case for clnt_create "unix" buffer overflow (bug 22542)
[glibc.git] / string / test-memccpy.c
blobee2cf8b8af1dc1ca1776ea2905a80b6d738d9073
1 /* Test and measure memccpy functions.
2 Copyright (C) 1999-2022 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 #define TEST_NAME "memccpy"
21 #include "test-string.h"
23 void *simple_memccpy (void *, const void *, int, size_t);
24 void *stupid_memccpy (void *, const void *, int, size_t);
26 IMPL (stupid_memccpy, 0)
27 IMPL (simple_memccpy, 0)
28 IMPL (memccpy, 1)
30 void *
31 simple_memccpy (void *dst, const void *src, int c, size_t n)
33 const char *s = src;
34 char *d = dst;
36 while (n-- > 0)
37 if ((*d++ = *s++) == (char) c)
38 return d;
40 return NULL;
43 void *
44 stupid_memccpy (void *dst, const void *src, int c, size_t n)
46 void *p = memchr (src, c, n);
48 if (p != NULL)
49 return mempcpy (dst, src, p - src + 1);
51 memcpy (dst, src, n);
52 return NULL;
55 typedef void *(*proto_t) (void *, const void *, int c, size_t);
57 static void
58 do_one_test (impl_t *impl, void *dst, const void *src, int c, size_t len,
59 size_t n)
61 void *expect = len > n ? NULL : (char *) dst + len;
62 if (CALL (impl, dst, src, c, n) != expect)
64 error (0, 0, "Wrong result in function %s %p %p", impl->name,
65 CALL (impl, dst, src, c, n), expect);
66 ret = 1;
67 return;
70 if (memcmp (dst, src, len > n ? n : len) != 0)
72 error (0, 0, "Wrong result in function %s", impl->name);
73 ret = 1;
74 return;
78 static void
79 do_test (size_t align1, size_t align2, int c, size_t len, size_t n,
80 int max_char)
82 size_t i;
83 char *s1, *s2;
85 align1 &= 7;
86 if (align1 + len >= page_size)
87 return;
89 align2 &= 7;
90 if (align2 + len >= page_size)
91 return;
93 s1 = (char *) (buf1 + align1);
94 s2 = (char *) (buf2 + align2);
96 for (i = 0; i < len - 1; ++i)
98 s1[i] = 32 + 23 * i % (max_char - 32);
99 if (s1[i] == (char) c)
100 --s1[i];
102 s1[len - 1] = c;
103 for (i = len; i + align1 < page_size && i < len + 64; ++i)
104 s1[i] = 32 + 32 * i % (max_char - 32);
106 FOR_EACH_IMPL (impl, 0)
107 do_one_test (impl, s2, s1, c, len, n);
110 static void
111 do_random_tests (void)
113 size_t i, j, n, align1, align2, len, size, mode;
114 unsigned char *p1 = buf1 + page_size - 512;
115 unsigned char *p2 = buf2 + page_size - 512;
116 unsigned char *res, c;
118 for (n = 0; n < ITERATIONS; n++)
120 mode = random ();
121 c = random ();
122 if (mode & 1)
124 size = random () & 255;
125 align1 = 512 - size - (random () & 15);
126 if (mode & 2)
127 align2 = align1 - (random () & 24);
128 else
129 align2 = align1 - (random () & 31);
130 if (mode & 4)
132 j = align1;
133 align1 = align2;
134 align2 = j;
136 if (mode & 8)
137 len = size - (random () & 31);
138 else
139 len = 512;
140 if (len >= 512)
141 len = random () & 511;
143 else
145 align1 = random () & 31;
146 if (mode & 2)
147 align2 = random () & 31;
148 else
149 align2 = align1 + (random () & 24);
150 len = random () & 511;
151 j = align1;
152 if (align2 > j)
153 j = align2;
154 if (mode & 4)
156 size = random () & 511;
157 if (size + j > 512)
158 size = 512 - j - (random() & 31);
160 else
161 size = 512 - j;
162 if ((mode & 8) && len + j >= 512)
163 len = 512 - j - (random () & 7);
165 j = len + align1 + 64;
166 if (j > 512)
167 j = 512;
168 for (i = 0; i < j; i++)
170 if (i == len + align1)
171 p1[i] = c;
172 else
174 p1[i] = random () & 255;
175 if (i >= align1 && i < len + align1 && p1[i] == c)
176 p1[i] = (random () & 127) + 3 + c;
180 FOR_EACH_IMPL (impl, 1)
182 unsigned char *expect;
183 memset (p2 - 64, '\1', 512 + 64);
184 res = CALL (impl, p2 + align2, p1 + align1, (char) c, size);
185 if (len >= size)
186 expect = NULL;
187 else
188 expect = p2 + align2 + len + 1;
190 if (res != expect)
192 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %d) %p != %p",
193 n, impl->name, align1, align2, len, size, c, res, expect);
194 ret = 1;
196 for (j = 0; j < align2 + 64; ++j)
198 if (p2[j - 64] != '\1')
200 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
201 n, impl->name, align1, align2, len);
202 ret = 1;
203 break;
206 j = align2 + len + 1;
207 if (size + align2 < j)
208 j = size + align2;
209 for (; j < 512; ++j)
211 if (p2[j] != '\1')
213 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
214 n, impl->name, align1, align2, len);
215 ret = 1;
216 break;
219 j = len + 1;
220 if (size < j)
221 j = size;
222 if (memcmp (p1 + align1, p2 + align2, j))
224 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
225 n, impl->name, align1, align2, len);
226 ret = 1;
233 test_main (void)
235 size_t i;
237 test_init ();
239 printf ("%28s", "");
240 FOR_EACH_IMPL (impl, 0)
241 printf ("\t%s", impl->name);
242 putchar ('\n');
244 for (i = 1; i < 8; ++i)
246 do_test (i, i, 12, 16, 16, 127);
247 do_test (i, i, 23, 16, 16, 255);
248 do_test (i, 2 * i, 28, 16, 16, 127);
249 do_test (2 * i, i, 31, 16, 16, 255);
250 do_test (8 - i, 2 * i, 1, 1 << i, 2 << i, 127);
251 do_test (2 * i, 8 - i, 17, 2 << i, 1 << i, 127);
252 do_test (8 - i, 2 * i, 0, 1 << i, 2 << i, 255);
253 do_test (2 * i, 8 - i, i, 2 << i, 1 << i, 255);
256 for (i = 1; i < 8; ++i)
258 do_test (0, 0, i, 4 << i, 8 << i, 127);
259 do_test (0, 0, i, 16 << i, 8 << i, 127);
260 do_test (8 - i, 2 * i, i, 4 << i, 8 << i, 127);
261 do_test (8 - i, 2 * i, i, 16 << i, 8 << i, 127);
264 do_random_tests ();
265 return ret;
268 #include <support/test-driver.c>