1/* Test case for strncmp inside a transactionally executing RTM region.
2 Copyright (C) 2021-2024 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
40CHAR string1[STRING_SIZE];
41CHAR string2[STRING_SIZE];
42
43__attribute__ ((noinline, noclone))
44static int
45prepare (void)
46{
47 MEMSET (string1, 'a', STRING_SIZE - 1);
48 MEMSET (string2, 'a', STRING_SIZE - 1);
49 if (STRNCMP (s1: string1, s2: string2, STRING_SIZE) == 0)
50 return EXIT_SUCCESS;
51 else
52 return EXIT_FAILURE;
53}
54
55__attribute__ ((noinline, noclone))
56static int
57function (void)
58{
59 if (STRNCMP (s1: string1, s2: string2, STRING_SIZE) == 0)
60 return 0;
61 else
62 return 1;
63}
64
65__attribute__ ((noinline, noclone))
66static int
67function_overflow (void)
68{
69 if (STRNCMP (s1: string1, s2: string2, SIZE_MAX) == 0)
70 return 0;
71 else
72 return 1;
73}
74
75__attribute__ ((noinline, noclone))
76static int
77function_overflow2 (void)
78{
79 if (STRNCMP (s1: string1, s2: string2, SIZE_MAX >> 4) == 0)
80 return 0;
81 else
82 return 1;
83}
84
85static int
86do_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: function_overflow);
92 if (status != EXIT_SUCCESS)
93 return status;
94 status = do_test_1 (TEST_NAME, LOOP, prepare, function: function_overflow2);
95 if (status != EXIT_SUCCESS)
96 return status;
97 return status;
98}
99

source code of glibc/sysdeps/x86/tst-strncmp-rtm.c