1 /* Test compilation of truncated regular expressions.
2 Copyright (C) 2018-2019 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] =
44 /* Syntax options. Will be combined with other flags. */
45 static const reg_syntax_t syntaxes
[] =
53 RE_SYNTAX_POSIX_EGREP
,
54 RE_SYNTAX_POSIX_BASIC
,
55 RE_SYNTAX_POSIX_EXTENDED
,
56 RE_SYNTAX_POSIX_MINIMAL_EXTENDED
,
59 /* Trailing characters placed after the initial character. */
60 static const char trailing_strings
[][4] =
74 /* Staging buffer for the constructed regular expression. */
77 /* Allocation used to detect over-reading by the regular expression
79 struct support_next_to_fault ntf
80 = support_next_to_fault_allocate (sizeof (buffer
));
82 /* Arbitrary Unicode codepoint at which we stop generating
83 characters. We do not probe the whole range because that would
84 take too long due to combinatorical exploision as the result of
85 combination with other flags. */
86 static const wchar_t last_character
= 0xfff;
88 for (size_t locale_idx
= 0; locale_idx
< array_length (locales
);
91 if (setlocale (LC_ALL
, locales
[locale_idx
]) == NULL
)
93 support_record_failure ();
94 printf ("error: setlocale (\"%s\"): %m", locales
[locale_idx
]);
98 printf ("info: testing locale \"%s\"\n", locales
[locale_idx
]);
100 for (wchar_t wc
= 0; wc
<= last_character
; ++wc
)
105 /* wcrtomb treats L'\0' in a special way. */
107 after_wc
= &buffer
[1];
112 size_t ret
= wcrtomb (buffer
, wc
, &ps
);
113 if (ret
== (size_t) -1)
115 /* EILSEQ means that the target character set
116 cannot encode the character. */
119 support_record_failure ();
120 printf ("error: wcrtomb (0x%x) failed: %m\n",
125 TEST_VERIFY_EXIT (ret
!= 0);
126 after_wc
= &buffer
[ret
];
129 for (size_t trailing_idx
= 0;
130 trailing_idx
< array_length (trailing_strings
);
134 = stpcpy (after_wc
, trailing_strings
[trailing_idx
]);
136 for (int do_nul
= 0; do_nul
< 2; ++do_nul
)
141 *after_trailing
= '\0';
142 after_nul
= &after_trailing
[1];
145 after_nul
= after_trailing
;
147 size_t length
= after_nul
- buffer
;
149 /* Make sure that the faulting region starts
150 after the used portion of the buffer. */
151 char *ntf_start
= ntf
.buffer
+ sizeof (buffer
) - length
;
152 memcpy (ntf_start
, buffer
, length
);
154 for (const reg_syntax_t
*psyntax
= syntaxes
;
155 psyntax
< array_end (syntaxes
); ++psyntax
)
156 for (int do_icase
= 0; do_icase
< 2; ++do_icase
)
158 re_syntax_options
= *psyntax
;
160 re_syntax_options
|= RE_ICASE
;
163 memset (®
, 0, sizeof (reg
));
164 const char *msg
= re_compile_pattern
165 (ntf_start
, length
, ®
);
168 if (test_verbose
> 0)
170 char *quoted
= support_quote_blob
172 printf ("info: compilation failed for pattern"
173 " \"%s\", syntax 0x%lx: %s\n",
174 quoted
, re_syntax_options
, msg
);
186 support_next_to_fault_free (&ntf
);
191 #include <support/test-driver.c>