malloc: Decorate malloc maps
[glibc.git] / posix / tst-regcomp-truncated.c
blobc601ae62d88587c4ada8f23dd303cd910b356ea3
1 /* Test compilation of truncated regular expressions.
2 Copyright (C) 2018-2023 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
21 23578. */
23 #include <array_length.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <regex.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <support/check.h>
31 #include <support/next_to_fault.h>
32 #include <support/support.h>
33 #include <support/test-driver.h>
34 #include <wchar.h>
36 /* Locales to test. */
37 static const char locales[][17] =
39 "C",
40 "C.UTF-8",
41 "en_US.UTF-8",
42 "de_DE.ISO-8859-1",
45 /* Syntax options. Will be combined with other flags. */
46 static const reg_syntax_t syntaxes[] =
48 RE_SYNTAX_EMACS,
49 RE_SYNTAX_AWK,
50 RE_SYNTAX_GNU_AWK,
51 RE_SYNTAX_POSIX_AWK,
52 RE_SYNTAX_GREP,
53 RE_SYNTAX_EGREP,
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] =
63 "",
64 "[",
65 "\\",
66 "[\\",
67 "(",
68 "(\\",
69 "\\(",
72 static int
73 do_test (void)
75 /* Staging buffer for the constructed regular expression. */
76 char buffer[16];
78 /* Allocation used to detect over-reading by the regular expression
79 compiler. */
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 combinatorial explosion 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);
90 ++ locale_idx)
92 if (setlocale (LC_ALL, locales[locale_idx]) == NULL)
94 support_record_failure ();
95 printf ("error: setlocale (\"%s\"): %m", locales[locale_idx]);
96 continue;
98 if (test_verbose > 0)
99 printf ("info: testing locale \"%s\"\n", locales[locale_idx]);
101 for (wchar_t wc = 0; wc <= last_character; ++wc)
103 char *after_wc;
104 if (wc == 0)
106 /* wcrtomb treats L'\0' in a special way. */
107 *buffer = '\0';
108 after_wc = &buffer[1];
110 else
112 mbstate_t ps = { };
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. */
118 if (errno != EILSEQ)
120 support_record_failure ();
121 printf ("error: wcrtomb (0x%x) failed: %m\n",
122 (unsigned) wc);
124 continue;
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);
132 ++trailing_idx)
134 char *after_trailing
135 = stpcpy (after_wc, trailing_strings[trailing_idx]);
137 for (int do_nul = 0; do_nul < 2; ++do_nul)
139 char *after_nul;
140 if (do_nul)
142 *after_trailing = '\0';
143 after_nul = &after_trailing[1];
145 else
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;
160 if (do_icase)
161 re_syntax_options |= RE_ICASE;
163 regex_t reg;
164 memset (&reg, 0, sizeof (reg));
165 const char *msg = re_compile_pattern
166 (ntf_start, length, &reg);
167 if (msg != NULL)
169 if (test_verbose > 0)
171 char *quoted = support_quote_blob
172 (buffer, length);
173 printf ("info: compilation failed for pattern"
174 " \"%s\", syntax 0x%lx: %s\n",
175 quoted, re_syntax_options, msg);
176 free (quoted);
179 else
180 regfree (&reg);
187 support_next_to_fault_free (&ntf);
189 return 0;
192 #include <support/test-driver.c>