1 /* Test strncpy 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/>. */
22 # define UCHAR wchar_t
23 # define BIG_CHAR WCHAR_MAX
24 # define SMALL_CHAR 1273
25 # define MEMCMP wmemcmp
26 # define MEMSET wmemset
27 # define STRNLEN wcsnlen
30 # define UCHAR unsigned char
31 # define BIG_CHAR CHAR_MAX
32 # define SMALL_CHAR 127
33 # define MEMCMP memcmp
34 # define MEMSET memset
35 # define STRNLEN strnlen
39 #ifndef STRNCPY_RESULT
40 # define STRNCPY_RESULT(dst, len, n) dst
43 # define TEST_NAME "strncpy"
45 # define TEST_NAME "wcsncpy"
47 # include "test-string.h"
49 # define SIMPLE_STRNCPY simple_strncpy
50 # define STRNCPY strncpy
52 # define SIMPLE_STRNCPY simple_wcsncpy
53 # define STRNCPY wcsncpy
59 /* Naive implementation to verify results. */
61 SIMPLE_STRNCPY (CHAR
*dst
, const CHAR
*src
, size_t n
)
65 if ((*dst
++ = *src
++) == '\0')
74 #endif /* !STRNCPY_RESULT */
76 typedef CHAR
*(*proto_t
) (CHAR
*, const CHAR
*, size_t);
79 do_one_test (impl_t
*impl
, CHAR
*dst
, const CHAR
*src
, size_t len
, size_t n
)
81 if (CALL (impl
, dst
, src
, n
) != STRNCPY_RESULT (dst
, len
, n
))
83 error (0, 0, "Wrong result in function %s %p %p", impl
->name
,
84 CALL (impl
, dst
, src
, n
), dst
);
89 if (memcmp (dst
, src
, (len
> n
? n
: len
) * sizeof (CHAR
)) != 0)
91 error (0, 0, "Wrong result in function %s", impl
->name
);
100 for (i
= len
; i
< n
; ++i
)
103 error (0, 0, "Wrong result in function %s", impl
->name
);
111 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
)
116 /* For wcsncpy: align1 and align2 here mean alignment not in bytes,
117 but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
119 if ((align1
+ len
) * sizeof (CHAR
) >= page_size
)
123 if ((align2
+ len
) * sizeof (CHAR
) >= page_size
)
126 s1
= (CHAR
*) (buf1
) + align1
;
127 s2
= (CHAR
*) (buf2
) + align2
;
129 for (i
= 0; i
< len
; ++i
)
130 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
132 for (i
= len
+ 1; (i
+ align1
) * sizeof (CHAR
) < page_size
&& i
< len
+ 64;
134 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
136 FOR_EACH_IMPL (impl
, 0)
137 do_one_test (impl
, s2
, s1
, len
, n
);
144 const size_t maxoffset
= 64;
146 /* Put s1 at the maxoffset from the edge of buf1's last page. */
147 s1
= (CHAR
*) buf1
+ BUF1PAGES
* page_size
/ sizeof(CHAR
) - maxoffset
;
148 /* s2 needs room to put a string with size of maxoffset + 1 at s2 +
150 s2
= (CHAR
*) buf2
+ page_size
/ sizeof(CHAR
) - maxoffset
* 2;
152 MEMSET (s1
, 'a', maxoffset
- 1);
153 s1
[maxoffset
- 1] = '\0';
155 /* Both strings are bounded to a page with read/write access and the next
156 page is protected with PROT_NONE (meaning that any access outside of the
157 page regions will trigger an invalid memory access).
159 The loop copies the string s1 for all possible offsets up to maxoffset
160 for both inputs with a size larger than s1 (so memory access outside the
161 expected memory regions might trigger invalid access). */
163 for (size_t off1
= 0; off1
< maxoffset
; off1
++)
165 for (size_t off2
= 0; off2
< maxoffset
; off2
++)
167 FOR_EACH_IMPL (impl
, 0)
168 do_one_test (impl
, s2
+ off2
, s1
+ off1
, maxoffset
- off1
- 1,
169 maxoffset
+ (maxoffset
- off2
));
175 do_random_tests (void)
177 size_t i
, j
, n
, align1
, align2
, len
, size
, mode
;
178 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
) - 512;
179 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
) - 512;
182 for (n
= 0; n
< ITERATIONS
; n
++)
184 /* For wcsncpy: align1 and align2 here mean align not in bytes,
185 but in wchar_ts, in bytes it will equal to align * (sizeof
191 size
= random () & 255;
192 align1
= 512 - size
- (random () & 15);
194 align2
= align1
- (random () & 24);
196 align2
= align1
- (random () & 31);
204 len
= size
- (random () & 31);
208 len
= random () & 511;
212 align1
= random () & 31;
214 align2
= random () & 31;
216 align2
= align1
+ (random () & 24);
217 len
= random () & 511;
223 size
= random () & 511;
225 size
= 512 - j
- (random () & 31);
229 if ((mode
& 8) && len
+ j
>= 512)
230 len
= 512 - j
- (random () & 7);
232 j
= len
+ align1
+ 64;
235 for (i
= 0; i
< j
; i
++)
237 if (i
== len
+ align1
)
241 p1
[i
] = random () & BIG_CHAR
;
242 if (i
>= align1
&& i
< len
+ align1
&& !p1
[i
])
243 p1
[i
] = (random () & SMALL_CHAR
) + 3;
247 FOR_EACH_IMPL (impl
, 1)
249 MEMSET (p2
- 64, '\1', 512 + 64);
250 res
= (UCHAR
*) CALL (impl
, (CHAR
*) (p2
+ align2
),
251 (CHAR
*) (p1
+ align1
), size
);
252 if (res
!= STRNCPY_RESULT (p2
+ align2
, len
, size
))
254 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
255 n
, impl
->name
, align1
, align2
, len
, res
,
256 STRNCPY_RESULT (p2
+ align2
, len
, size
));
259 for (j
= 0; j
< align2
+ 64; ++j
)
261 if (p2
[j
- 64] != '\1')
263 error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
264 n
, impl
->name
, align1
, align2
, len
);
269 j
= align2
+ len
+ 1;
270 if (size
+ align2
> j
)
276 error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
277 n
, impl
->name
, align1
, align2
, len
);
282 for (j
= align2
+ len
+ 1; j
< align2
+ size
; ++j
)
285 error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
286 n
, impl
->name
, align1
, align2
, len
);
293 if (MEMCMP (p1
+ align1
, p2
+ align2
, j
))
295 error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
296 n
, impl
->name
, align1
, align2
, len
);
311 FOR_EACH_IMPL (impl
, 0)
312 printf ("\t%s", impl
->name
);
315 for (i
= 1; i
< 8; ++i
)
317 do_test (i
, i
, 16, 16, SMALL_CHAR
);
318 do_test (i
, i
, 16, 16, BIG_CHAR
);
319 do_test (i
, 2 * i
, 16, 16, SMALL_CHAR
);
320 do_test (2 * i
, i
, 16, 16, BIG_CHAR
);
321 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, SMALL_CHAR
);
322 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, SMALL_CHAR
);
323 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, BIG_CHAR
);
324 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, BIG_CHAR
);
327 for (i
= 1; i
< 8; ++i
)
329 do_test (0, 0, 4 << i
, 8 << i
, SMALL_CHAR
);
330 do_test (0, 0, 16 << i
, 8 << i
, SMALL_CHAR
);
331 do_test (8 - i
, 2 * i
, 4 << i
, 8 << i
, SMALL_CHAR
);
332 do_test (8 - i
, 2 * i
, 16 << i
, 8 << i
, SMALL_CHAR
);
340 #include <support/test-driver.c>