1 /* Test strncmp and wcsncmp functions.
2 Copyright (C) 1999-2021 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/>. */
21 # define TEST_NAME "wcsncmp"
23 # define TEST_NAME "strncmp"
25 #include "test-string.h"
30 # define L(str) L##str
31 # define STRNCMP wcsncmp
32 # define STRCPY wcscpy
33 # define STRDUP wcsdup
34 # define MEMCPY wmemcpy
35 # define SIMPLE_STRNCMP simple_wcsncmp
36 # define STUPID_STRNCMP stupid_wcsncmp
38 # define UCHAR wchar_t
40 # define CHAR__MAX WCHAR_MAX
41 # define CHAR__MIN WCHAR_MIN
43 /* Wcsncmp uses signed semantics for comparison, not unsigned.
44 Avoid using substraction since possible overflow */
46 simple_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
54 if (c1
== L('\0') || c1
!= c2
)
55 return c1
> c2
? 1 : (c1
< c2
? -1 : 0);
61 stupid_wcsncmp (const CHAR
*s1
, const CHAR
*s2
, size_t n
)
64 size_t ns1
= wcsnlen (s1
, n
) + 1, ns2
= wcsnlen (s2
, n
) + 1;
66 n
= ns1
< n
? ns1
: n
;
67 n
= ns2
< n
? ns2
: n
;
74 return c1
> c2
? 1 : -1;
81 # define STRNCMP strncmp
82 # define STRCPY strcpy
83 # define STRDUP strdup
84 # define MEMCPY memcpy
85 # define SIMPLE_STRNCMP simple_strncmp
86 # define STUPID_STRNCMP stupid_strncmp
88 # define UCHAR unsigned char
90 # define CHAR__MAX CHAR_MAX
91 # define CHAR__MIN CHAR_MIN
93 /* Strncmp uses unsigned semantics for comparison. */
95 simple_strncmp (const char *s1
, const char *s2
, size_t n
)
99 while (n
-- && (ret
= *(unsigned char *) s1
- * (unsigned char *) s2
++) == 0
105 stupid_strncmp (const char *s1
, const char *s2
, size_t n
)
107 size_t ns1
= strnlen (s1
, n
) + 1, ns2
= strnlen (s2
, n
) + 1;
110 n
= ns1
< n
? ns1
: n
;
111 n
= ns2
< n
? ns2
: n
;
112 while (n
-- && (ret
= *(unsigned char *) s1
++ - * (unsigned char *) s2
++) == 0);
118 typedef int (*proto_t
) (const CHAR
*, const CHAR
*, size_t);
120 IMPL (STUPID_STRNCMP
, 0)
121 IMPL (SIMPLE_STRNCMP
, 0)
126 check_result (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t n
,
129 int result
= CALL (impl
, s1
, s2
, n
);
130 if ((exp_result
== 0 && result
!= 0)
131 || (exp_result
< 0 && result
>= 0)
132 || (exp_result
> 0 && result
<= 0))
134 error (0, 0, "Wrong result in function %s %d %d", impl
->name
,
144 do_one_test (impl_t
*impl
, const CHAR
*s1
, const CHAR
*s2
, size_t n
,
147 if (check_result (impl
, s1
, s2
, n
, exp_result
) < 0)
152 do_test_limit (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
158 align1
&= ~(CHARBYTES
- 1);
159 align2
&= ~(CHARBYTES
- 1);
163 s1
= (CHAR
*) (buf1
+ page_size
);
164 s2
= (CHAR
*) (buf2
+ page_size
);
166 FOR_EACH_IMPL (impl
, 0)
167 do_one_test (impl
, s1
, s2
, n
, 0);
174 align_n
= (page_size
- n
* CHARBYTES
) & 15;
176 s1
= (CHAR
*) (buf1
+ page_size
- n
* CHARBYTES
);
177 s2
= (CHAR
*) (buf2
+ page_size
- n
* CHARBYTES
);
179 if (align1
< align_n
)
180 s1
= (CHAR
*) ((char *) s1
- (align_n
- align1
));
182 if (align2
< align_n
)
183 s2
= (CHAR
*) ((char *) s2
- (align_n
- align2
));
185 for (i
= 0; i
< n
; i
++)
186 s1
[i
] = s2
[i
] = 1 + 23 * i
% max_char
;
194 else if (exp_result
> 0)
198 FOR_EACH_IMPL (impl
, 0)
199 do_one_test (impl
, s1
, s2
, n
, exp_result
);
203 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
,
209 align1
&= ~(CHARBYTES
- 1);
210 align2
&= ~(CHARBYTES
- 1);
216 if (align1
+ (n
+ 1) * CHARBYTES
>= page_size
)
220 if (align2
+ (n
+ 1) * CHARBYTES
>= page_size
)
223 s1
= (CHAR
*) (buf1
+ align1
);
224 s2
= (CHAR
*) (buf2
+ align2
);
226 for (i
= 0; i
< n
; i
++)
227 s1
[i
] = s2
[i
] = 1 + (23 << ((CHARBYTES
- 1) * 8)) * i
% max_char
;
229 s1
[n
] = 24 + exp_result
;
235 else if (exp_result
> 0)
238 s2
[n
- 1] -= exp_result
;
240 FOR_EACH_IMPL (impl
, 0)
241 do_one_test (impl
, s1
, s2
, n
, exp_result
);
245 do_page_test (size_t offset1
, size_t offset2
, CHAR
*s2
)
250 if (offset1
* CHARBYTES
>= page_size
|| offset2
* CHARBYTES
>= page_size
)
259 FOR_EACH_IMPL (impl
, 0)
261 check_result (impl
, s1
, s2
, page_size
, -exp_result
);
262 check_result (impl
, s2
, s1
, page_size
, exp_result
);
267 do_random_tests (void)
269 size_t i
, j
, n
, align1
, align2
, pos
, len1
, len2
, size
;
272 UCHAR
*p1
= (UCHAR
*) (buf1
+ page_size
- 512 * CHARBYTES
);
273 UCHAR
*p2
= (UCHAR
*) (buf2
+ page_size
- 512 * CHARBYTES
);
275 for (n
= 0; n
< ITERATIONS
; n
++)
277 align1
= random () & 31;
279 align2
= random () & 31;
281 align2
= align1
+ (random () & 24);
282 pos
= random () & 511;
283 size
= random () & 511;
284 j
= align1
> align2
? align1
: align2
;
286 pos
= 510 - j
- (random () & 7);
287 len1
= random () & 511;
288 if (pos
>= len1
&& (random () & 1))
289 len1
= pos
+ (random () & 7);
291 len1
= 511 - j
- (random () & 7);
295 len2
= len1
+ (len1
!= 511 - j
? random () % (511 - j
- len1
) : 0);
296 j
= (pos
> len2
? pos
: len2
) + align1
+ 64;
299 for (i
= 0; i
< j
; ++i
)
301 p1
[i
] = random () & 255;
302 if (i
< len1
+ align1
&& !p1
[i
])
304 p1
[i
] = random () & 255;
306 p1
[i
] = 1 + (random () & 127);
309 for (i
= 0; i
< j
; ++i
)
311 p2
[i
] = random () & 255;
312 if (i
< len2
+ align2
&& !p2
[i
])
314 p2
[i
] = random () & 255;
316 p2
[i
] = 1 + (random () & 127);
321 MEMCPY (p2
+ align2
, p1
+ align1
, pos
);
324 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
326 p2
[align2
+ pos
] = random () & 255;
327 if (p2
[align2
+ pos
] == p1
[align1
+ pos
])
328 p2
[align2
+ pos
] = p1
[align1
+ pos
] + 3 + (random () & 127);
333 if (p1
[align1
+ pos
] < p2
[align2
+ pos
])
339 p1
[len1
+ align1
] = 0;
340 p2
[len2
+ align2
] = 0;
342 FOR_EACH_IMPL (impl
, 1)
344 r
= CALL (impl
, (CHAR
*) (p1
+ align1
), (CHAR
*) (p2
+ align2
), size
);
345 /* Test whether on 64-bit architectures where ABI requires
346 callee to promote has the promotion been done. */
347 asm ("" : "=g" (r
) : "0" (r
));
348 if ((r
== 0 && result
)
349 || (r
< 0 && result
>= 0)
350 || (r
> 0 && result
<= 0))
352 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
353 n
, impl
->name
, align1
, align2
, len1
, len2
, pos
, size
, r
, result
, p1
, p2
);
363 CHAR
*s1
= (CHAR
*) (buf1
+ 0xb2c);
364 CHAR
*s2
= (CHAR
*) (buf1
+ 0xfd8);
368 STRCPY(s1
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
369 STRCPY(s2
, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
371 /* Check possible overflow bug for wcsncmp */
375 for (offset
= 0; offset
< 6; offset
++)
377 for (i
= 0; i
< 80; i
++)
379 exp_result
= SIMPLE_STRNCMP (s1
+ offset
, s2
+ offset
, i
);
380 FOR_EACH_IMPL (impl
, 0)
381 check_result (impl
, s1
+ offset
, s2
+ offset
, i
, exp_result
);
393 for (i
= 0; i
< (page_size
/ CHARBYTES
) - 1; i
++)
399 for (i
= 0; i
< 64; ++i
)
400 do_page_test ((3988 / CHARBYTES
) + i
, (2636 / CHARBYTES
), s2
);
408 /* To trigger bug 25933, we need a size that is equal to the vector
409 length times 4. In the case of AVX2 for Intel, we need 32 * 4. We
410 make this test generic and run it for all architectures as additional
411 boundary testing for such related algorithms. */
412 size_t size
= 32 * 4;
413 CHAR
*s1
= (CHAR
*) (buf1
+ (BUF1PAGES
- 1) * page_size
);
414 CHAR
*s2
= (CHAR
*) (buf2
+ (BUF1PAGES
- 1) * page_size
);
417 memset (s1
, 'a', page_size
);
418 memset (s2
, 'a', page_size
);
419 s1
[(page_size
/ CHARBYTES
) - 1] = (CHAR
) 0;
421 /* Iterate over a size that is just below where we expect the bug to
422 trigger up to the size we expect will trigger the bug e.g. [99-128].
423 Likewise iterate the start of two strings between 30 and 31 bytes
424 away from the boundary to simulate alignment changes. */
425 for (size_t s
= 99; s
<= size
; s
++)
426 for (size_t s1a
= 30; s1a
< 32; s1a
++)
427 for (size_t s2a
= 30; s2a
< 32; s2a
++)
429 CHAR
*s1p
= s1
+ (page_size
/ CHARBYTES
- s
) - s1a
;
430 CHAR
*s2p
= s2
+ (page_size
/ CHARBYTES
- s
) - s2a
;
431 exp_result
= SIMPLE_STRNCMP (s1p
, s2p
, s
);
432 FOR_EACH_IMPL (impl
, 0)
433 check_result (impl
, s1p
, s2p
, s
, exp_result
);
449 FOR_EACH_IMPL (impl
, 0)
450 printf ("\t%s", impl
->name
);
453 for (i
=0; i
< 16; ++i
)
455 do_test (0, 0, 8, i
, 127, 0);
456 do_test (0, 0, 8, i
, 127, -1);
457 do_test (0, 0, 8, i
, 127, 1);
458 do_test (i
, i
, 8, i
, 127, 0);
459 do_test (i
, i
, 8, i
, 127, 1);
460 do_test (i
, i
, 8, i
, 127, -1);
461 do_test (i
, 2 * i
, 8, i
, 127, 0);
462 do_test (2 * i
, i
, 8, i
, 127, 1);
463 do_test (i
, 3 * i
, 8, i
, 127, -1);
464 do_test (0, 0, 8, i
, 255, 0);
465 do_test (0, 0, 8, i
, 255, -1);
466 do_test (0, 0, 8, i
, 255, 1);
467 do_test (i
, i
, 8, i
, 255, 0);
468 do_test (i
, i
, 8, i
, 255, 1);
469 do_test (i
, i
, 8, i
, 255, -1);
470 do_test (i
, 2 * i
, 8, i
, 255, 0);
471 do_test (2 * i
, i
, 8, i
, 255, 1);
472 do_test (i
, 3 * i
, 8, i
, 255, -1);
475 for (i
= 1; i
< 8; ++i
)
477 do_test (0, 0, 8 << i
, 16 << i
, 127, 0);
478 do_test (0, 0, 8 << i
, 16 << i
, 127, 1);
479 do_test (0, 0, 8 << i
, 16 << i
, 127, -1);
480 do_test (0, 0, 8 << i
, 16 << i
, 255, 0);
481 do_test (0, 0, 8 << i
, 16 << i
, 255, 1);
482 do_test (0, 0, 8 << i
, 16 << i
, 255, -1);
483 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 0);
484 do_test (8 - i
, 2 * i
, 8 << i
, 16 << i
, 127, 1);
485 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 0);
486 do_test (2 * i
, i
, 8 << i
, 16 << i
, 255, 1);
489 do_test_limit (0, 0, 0, 0, 127, 0);
490 do_test_limit (4, 0, 21, 20, 127, 0);
491 do_test_limit (0, 4, 21, 20, 127, 0);
492 do_test_limit (8, 0, 25, 24, 127, 0);
493 do_test_limit (0, 8, 25, 24, 127, 0);
495 for (i
= 0; i
< 8; ++i
)
497 do_test_limit (0, 0, 17 - i
, 16 - i
, 127, 0);
498 do_test_limit (0, 0, 17 - i
, 16 - i
, 255, 0);
499 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 0);
500 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, 1);
501 do_test_limit (0, 0, 15 - i
, 16 - i
, 127, -1);
502 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 0);
503 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, 1);
504 do_test_limit (0, 0, 15 - i
, 16 - i
, 255, -1);
511 #include <support/test-driver.c>