1 /* Test case for strncmp inside a transactionally executing RTM region.
2    Copyright (C) 2021-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 #include <stdint.h>
20 #include <tst-string-rtm.h>
21 
22 #ifdef WIDE
23 # define CHAR wchar_t
24 # define MEMSET wmemset
25 # define STRNCMP wcsncmp
26 # define TEST_NAME "wcsncmp"
27 #else /* !WIDE */
28 # define CHAR char
29 # define MEMSET memset
30 # ifndef STRNCMP
31 #  define STRNCMP strncmp
32 #  define TEST_NAME "strncmp"
33 # endif
34 #endif /* !WIDE */
35 
36 
37 
38 #define LOOP 3000
39 #define STRING_SIZE 1024
40 CHAR string1[STRING_SIZE];
41 CHAR string2[STRING_SIZE];
42 
43 __attribute__ ((noinline, noclone))
44 static int
prepare(void)45 prepare (void)
46 {
47   MEMSET (string1, 'a', STRING_SIZE - 1);
48   MEMSET (string2, 'a', STRING_SIZE - 1);
49   if (STRNCMP (string1, string2, STRING_SIZE) == 0)
50     return EXIT_SUCCESS;
51   else
52     return EXIT_FAILURE;
53 }
54 
55 __attribute__ ((noinline, noclone))
56 static int
function(void)57 function (void)
58 {
59   if (STRNCMP (string1, string2, STRING_SIZE) == 0)
60     return 0;
61   else
62     return 1;
63 }
64 
65 __attribute__ ((noinline, noclone))
66 static int
function_overflow(void)67 function_overflow (void)
68 {
69   if (STRNCMP (string1, string2, SIZE_MAX) == 0)
70     return 0;
71   else
72     return 1;
73 }
74 
75 __attribute__ ((noinline, noclone))
76 static int
function_overflow2(void)77 function_overflow2 (void)
78 {
79   if (STRNCMP (string1, string2, SIZE_MAX >> 4) == 0)
80     return 0;
81   else
82     return 1;
83 }
84 
85 static int
do_test(void)86 do_test (void)
87 {
88   int status = do_test_1 (TEST_NAME, LOOP, prepare, function);
89   if (status != EXIT_SUCCESS)
90     return status;
91   status = do_test_1 (TEST_NAME, LOOP, prepare, function_overflow);
92   if (status != EXIT_SUCCESS)
93     return status;
94   status = do_test_1 (TEST_NAME, LOOP, prepare, function_overflow2);
95   if (status != EXIT_SUCCESS)
96     return status;
97   return status;
98 }
99