1 /* Test compilation of truncated regular expressions.
2 Copyright (C) 2018-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/>. */
19 /* This test constructs various patterns in an attempt to trigger
20 over-reading the regular expression compiler, such as bug
23 #include <array_length.h>
30 #include <support/check.h>
31 #include <support/next_to_fault.h>
32 #include <support/support.h>
33 #include <support/test-driver.h>
36 /* Locales to test. */
37 static const char locales
[][17] =
45 /* Syntax options. Will be combined with other flags. */
46 static const reg_syntax_t syntaxes
[] =
54 RE_SYNTAX_POSIX_EGREP
,
55 RE_SYNTAX_POSIX_BASIC
,
56 RE_SYNTAX_POSIX_EXTENDED
,
57 RE_SYNTAX_POSIX_MINIMAL_EXTENDED
,
60 /* Trailing characters placed after the initial character. */
61 static const char trailing_strings
[][4] =
75 /* Staging buffer for the constructed regular expression. */
78 /* Allocation used to detect over-reading by the regular expression
80 struct support_next_to_fault ntf
81 = support_next_to_fault_allocate (sizeof (buffer
));
83 /* Arbitrary Unicode codepoint at which we stop generating
84 characters. We do not probe the whole range because that would
85 take too long due to combinatorical exploision as the result of
86 combination with other flags. */
87 static const wchar_t last_character
= 0xfff;
89 for (size_t locale_idx
= 0; locale_idx
< array_length (locales
);
92 if (setlocale (LC_ALL
, locales
[locale_idx
]) == NULL
)
94 support_record_failure ();
95 printf ("error: setlocale (\"%s\"): %m", locales
[locale_idx
]);
99 printf ("info: testing locale \"%s\"\n", locales
[locale_idx
]);
101 for (wchar_t wc
= 0; wc
<= last_character
; ++wc
)
106 /* wcrtomb treats L'\0' in a special way. */
108 after_wc
= &buffer
[1];
113 size_t ret
= wcrtomb (buffer
, wc
, &ps
);
114 if (ret
== (size_t) -1)
116 /* EILSEQ means that the target character set
117 cannot encode the character. */
120 support_record_failure ();
121 printf ("error: wcrtomb (0x%x) failed: %m\n",
126 TEST_VERIFY_EXIT (ret
!= 0);
127 after_wc
= &buffer
[ret
];
130 for (size_t trailing_idx
= 0;
131 trailing_idx
< array_length (trailing_strings
);
135 = stpcpy (after_wc
, trailing_strings
[trailing_idx
]);
137 for (int do_nul
= 0; do_nul
< 2; ++do_nul
)
142 *after_trailing
= '\0';
143 after_nul
= &after_trailing
[1];
146 after_nul
= after_trailing
;
148 size_t length
= after_nul
- buffer
;
150 /* Make sure that the faulting region starts
151 after the used portion of the buffer. */
152 char *ntf_start
= ntf
.buffer
+ sizeof (buffer
) - length
;
153 memcpy (ntf_start
, buffer
, length
);
155 for (const reg_syntax_t
*psyntax
= syntaxes
;
156 psyntax
< array_end (syntaxes
); ++psyntax
)
157 for (int do_icase
= 0; do_icase
< 2; ++do_icase
)
159 re_syntax_options
= *psyntax
;
161 re_syntax_options
|= RE_ICASE
;
164 memset (®
, 0, sizeof (reg
));
165 const char *msg
= re_compile_pattern
166 (ntf_start
, length
, ®
);
169 if (test_verbose
> 0)
171 char *quoted
= support_quote_blob
173 printf ("info: compilation failed for pattern"
174 " \"%s\", syntax 0x%lx: %s\n",
175 quoted
, re_syntax_options
, msg
);
187 support_next_to_fault_free (&ntf
);
192 #include <support/test-driver.c>