1/* Test memchr 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 "memchr"
22#else
23# define TEST_NAME "wmemchr"
24#endif /* WIDE */
25
26#include "test-string.h"
27#include <stdint.h>
28
29#ifndef WIDE
30# define MEMCHR memchr
31# define CHAR char
32# define UCHAR unsigned char
33# define SIMPLE_MEMCHR simple_memchr
34# define BIG_CHAR CHAR_MAX
35# define SMALL_CHAR 127
36#else
37# include <wchar.h>
38# define MEMCHR wmemchr
39# define CHAR wchar_t
40# define UCHAR wchar_t
41# define SIMPLE_MEMCHR simple_wmemchr
42# define BIG_CHAR WCHAR_MAX
43# define SMALL_CHAR 1273
44#endif /* WIDE */
45
46typedef CHAR *(*proto_t) (const CHAR *, int, size_t);
47CHAR *SIMPLE_MEMCHR (const CHAR *, int, size_t);
48
49IMPL (SIMPLE_MEMCHR, 0)
50IMPL (MEMCHR, 1)
51
52CHAR *
53SIMPLE_MEMCHR (const CHAR *s, int c, size_t n)
54{
55 while (n--)
56 if (*s++ == (CHAR) c)
57 return (CHAR *) s - 1;
58 return NULL;
59}
60
61static void
62do_one_test (impl_t *impl, const CHAR *s, int c, size_t n, CHAR *exp_res)
63{
64 CHAR *res = CALL (impl, s, c, n);
65 if (res != exp_res)
66 {
67 error (status: 0, errnum: 0, format: "Wrong result in function %s (%p, %d, %zu) -> %p != %p",
68 impl->name, s, c, n, res, exp_res);
69 ret = 1;
70 return;
71 }
72}
73
74static void
75do_test (size_t align, size_t pos, size_t len, size_t n, int seek_char)
76{
77 size_t i;
78 CHAR *result;
79
80 if ((align + len) * sizeof (CHAR) >= page_size)
81 return;
82
83 CHAR *buf = (CHAR *) (buf1);
84
85 for (i = 0; i < len; ++i)
86 {
87 buf[align + i] = 1 + 23 * i % SMALL_CHAR;
88 if (buf[align + i] == seek_char)
89 buf[align + i] = seek_char + 1;
90 }
91 buf[align + len] = 0;
92
93 if (pos < MIN(n, len))
94 {
95 buf[align + pos] = seek_char;
96 buf[align + len] = -seek_char;
97 result = (CHAR *) (buf + align + pos);
98 }
99 else
100 {
101 result = NULL;
102 buf[align + len] = seek_char;
103 }
104
105 FOR_EACH_IMPL (impl, 0)
106 do_one_test (impl, s: (CHAR *) (buf + align), c: seek_char, n, exp_res: result);
107}
108
109static void
110do_overflow_tests (void)
111{
112 size_t i, j, len;
113 const size_t one = 1;
114 uintptr_t buf_addr = (uintptr_t) buf1;
115
116 for (i = 0; i < 750; ++i)
117 {
118 do_test (align: 0, pos: i, len: 751, SIZE_MAX - i, BIG_CHAR);
119 do_test (align: 0, pos: i, len: 751, n: i - buf_addr, BIG_CHAR);
120 do_test (align: 0, pos: i, len: 751, n: -buf_addr - i, BIG_CHAR);
121 do_test (align: 0, pos: i, len: 751, SIZE_MAX - buf_addr - i, BIG_CHAR);
122 do_test (align: 0, pos: i, len: 751, SIZE_MAX - buf_addr + i, BIG_CHAR);
123
124 len = 0;
125 for (j = 8 * sizeof(size_t) - 1; j ; --j)
126 {
127 len |= one << j;
128 do_test (align: 0, pos: i, len: 751, n: len - i, BIG_CHAR);
129 do_test (align: 0, pos: i, len: 751, n: len + i, BIG_CHAR);
130 do_test (align: 0, pos: i, len: 751, n: len - buf_addr - i, BIG_CHAR);
131 do_test (align: 0, pos: i, len: 751, n: len - buf_addr + i, BIG_CHAR);
132
133 do_test (align: 0, pos: i, len: 751, n: ~len - i, BIG_CHAR);
134 do_test (align: 0, pos: i, len: 751, n: ~len + i, BIG_CHAR);
135 do_test (align: 0, pos: i, len: 751, n: ~len - buf_addr - i, BIG_CHAR);
136 do_test (align: 0, pos: i, len: 751, n: ~len - buf_addr + i, BIG_CHAR);
137 }
138 }
139}
140
141static void
142do_random_tests (void)
143{
144 size_t i, j, n, align, pos, len;
145 int seek_char;
146 CHAR *result;
147 UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
148
149 for (n = 0; n < ITERATIONS; n++)
150 {
151 align = random () & 15;
152 pos = random () & 511;
153 if (pos + align >= 512)
154 pos = 511 - align - (random () & 7);
155 len = random () & 511;
156 if (pos >= len)
157 len = pos + (random () & 7);
158 if (len + align >= 512)
159 len = 512 - align - (random () & 7);
160 seek_char = random () & BIG_CHAR;
161 j = len + align + 64;
162 if (j > 512)
163 j = 512;
164
165 for (i = 0; i < j; i++)
166 {
167 if (i == pos + align)
168 p[i] = seek_char;
169 else
170 {
171 p[i] = random () & BIG_CHAR;
172 if (i < pos + align && p[i] == seek_char)
173 p[i] = seek_char + 13;
174 }
175 }
176
177 if (pos < len)
178 {
179 size_t r = random ();
180 if ((r & 31) == 0)
181 len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
182 result = (CHAR *) (p + pos + align);
183 }
184 else
185 result = NULL;
186
187 FOR_EACH_IMPL (impl, 1)
188 if (CALL (impl, (CHAR *) (p + align), seek_char, len) != result)
189 {
190 error (status: 0, errnum: 0, format: "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
191 n, impl->name, align, seek_char, len, pos,
192 CALL (impl, (CHAR *) (p + align), seek_char, len),
193 result, p);
194 ret = 1;
195 }
196 }
197}
198
199int
200test_main (void)
201{
202 size_t i, j;
203
204 test_init ();
205
206 printf (format: "%20s", "");
207 FOR_EACH_IMPL (impl, 0)
208 printf (format: "\t%s", impl->name);
209 putchar (c: '\n');
210
211 for (i = 1; i < 8; ++i)
212 {
213 /* Test n == 0. */
214 do_test (align: i, pos: i, len: 0, n: 0, seek_char: 23);
215 do_test (align: i, pos: i, len: 0, n: 0, seek_char: 0);
216
217 do_test (align: 0, pos: 16 << i, len: 2048, n: 2048, seek_char: 23);
218 do_test (align: i, pos: 64, len: 256, n: 256, seek_char: 23);
219 do_test (align: 0, pos: 16 << i, len: 2048, n: 2048, seek_char: 0);
220 do_test (align: i, pos: 64, len: 256, n: 256, seek_char: 0);
221
222 /* Check for large input sizes and for these cases we need to
223 make sure the byte is within the size range (that's why
224 7 << i must be smaller than 2048). */
225 do_test (align: 0, pos: 7 << i, len: 2048, SIZE_MAX, seek_char: 23);
226 do_test (align: 0, pos: 2048 - i, len: 2048, SIZE_MAX, seek_char: 23);
227 do_test (align: i, pos: 64, len: 256, SIZE_MAX, seek_char: 23);
228 do_test (align: 0, pos: 7 << i, len: 2048, SIZE_MAX, seek_char: 0);
229 do_test (align: 0, pos: 2048 - i, len: 2048, SIZE_MAX, seek_char: 0);
230 do_test (align: i, pos: 64, len: 256, SIZE_MAX, seek_char: 0);
231 }
232
233 for (i = 1; i < 64; ++i)
234 {
235 for (j = 1; j < 64; j++)
236 {
237 do_test (align: 0, pos: 64 - j, len: 64, SIZE_MAX, seek_char: 23);
238 do_test (align: i, pos: 64 - j, len: 64, SIZE_MAX, seek_char: 23);
239 }
240 }
241
242 for (i = 1; i < 32; ++i)
243 {
244 do_test (align: 0, pos: i, len: i + 1, n: i + 1, seek_char: 23);
245 do_test (align: 0, pos: i, len: i + 1, n: i + 1, seek_char: 0);
246 }
247
248 /* BZ#21182 - wrong overflow calculation for i686 implementation
249 with address near end of the page. */
250 for (i = 2; i < 16; ++i)
251 /* page_size is in fact getpagesize() * 2. */
252 do_test (align: page_size / 2 - i, pos: i, len: i, n: 1, seek_char: 0x9B);
253
254 do_random_tests ();
255 do_overflow_tests ();
256 return ret;
257}
258
259#include <support/test-driver.c>
260

source code of glibc/string/test-memchr.c