aboutsummaryrefslogtreecommitdiff
path: root/posix/tst-regcomp-truncated.c
blob: a4a1581bbc2b39eb25fcf35b3b1408cdc46de6ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Test compilation of truncated regular expressions.
   Copyright (C) 2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

/* This test constructs various patterns in an attempt to trigger
   over-reading the regular expression compiler, such as bug
   23578.  */

#include <array_length.h>
#include <errno.h>
#include <locale.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/next_to_fault.h>
#include <support/support.h>
#include <support/test-driver.h>
#include <wchar.h>

/* Locales to test.  */
static const char locales[][17] =
  {
    "C",
    "en_US.UTF-8",
    "de_DE.ISO-8859-1",
  };

/* Syntax options.  Will be combined with other flags.  */
static const reg_syntax_t syntaxes[] =
  {
    RE_SYNTAX_EMACS,
    RE_SYNTAX_AWK,
    RE_SYNTAX_GNU_AWK,
    RE_SYNTAX_POSIX_AWK,
    RE_SYNTAX_GREP,
    RE_SYNTAX_EGREP,
    RE_SYNTAX_POSIX_EGREP,
    RE_SYNTAX_POSIX_BASIC,
    RE_SYNTAX_POSIX_EXTENDED,
    RE_SYNTAX_POSIX_MINIMAL_EXTENDED,
  };

/* Trailing characters placed after the initial character.  */
static const char trailing_strings[][4] =
  {
    "",
    "[",
    "\\",
    "[\\",
    "(",
    "(\\",
    "\\(",
  };

static int
do_test (void)
{
  /* Staging buffer for the constructed regular expression.  */
  char buffer[16];

  /* Allocation used to detect over-reading by the regular expression
     compiler.  */
  struct support_next_to_fault ntf
    = support_next_to_fault_allocate (sizeof (buffer));

  /* Arbitrary Unicode codepoint at which we stop generating
     characters.  We do not probe the whole range because that would
     take too long due to combinatorical exploision as the result of
     combination with other flags.  */
  static const wchar_t last_character = 0xfff;

  for (size_t locale_idx = 0; locale_idx < array_length (locales);
       ++ locale_idx)
    {
      if (setlocale (LC_ALL, locales[locale_idx]) == NULL)
        {
          support_record_failure ();
          printf ("error: setlocale (\"%s\"): %m", locales[locale_idx]);
          continue;
        }
      if (test_verbose > 0)
        printf ("info: testing locale \"%s\"\n", locales[locale_idx]);

      for (wchar_t wc = 0; wc <= last_character; ++wc)
        {
          char *after_wc;
          if (wc == 0)
            {
              /* wcrtomb treats L'\0' in a special way.  */
              *buffer = '\0';
              after_wc = &buffer[1];
            }
          else
            {
              mbstate_t ps = { };
              size_t ret = wcrtomb (buffer, wc, &ps);
              if (ret == (size_t) -1)
                {
                  /* EILSEQ means that the target character set
                     cannot encode the character.  */
                  if (errno != EILSEQ)
                    {
                      support_record_failure ();
                      printf ("error: wcrtomb (0x%x) failed: %m\n",
                              (unsigned) wc);
                    }
                  continue;
                }
              TEST_VERIFY_EXIT (ret != 0);
              after_wc = &buffer[ret];
            }

          for (size_t trailing_idx = 0;
               trailing_idx < array_length (trailing_strings);
               ++trailing_idx)
            {
              char *after_trailing
                = stpcpy (after_wc, trailing_strings[trailing_idx]);

              for (int do_nul = 0; do_nul < 2; ++do_nul)
                {
                  char *after_nul;
                  if (do_nul)
                    {
                      *after_trailing = '\0';
                      after_nul = &after_trailing[1];
                    }
                  else
                    after_nul = after_trailing;

                  size_t length = after_nul - buffer;

                  /* Make sure that the faulting region starts
                     after the used portion of the buffer.  */
                  char *ntf_start = ntf.buffer + sizeof (buffer) - length;
                  memcpy (ntf_start, buffer, length);

                  for (const reg_syntax_t *psyntax = syntaxes;
                       psyntax < array_end (syntaxes); ++psyntax)
                    for (int do_icase = 0; do_icase < 2; ++do_icase)
                      {
                        re_syntax_options = *psyntax;
                        if (do_icase)
                          re_syntax_options |= RE_ICASE;

                        regex_t reg;
                        memset (&reg, 0, sizeof (reg));
                        const char *msg = re_compile_pattern
                          (ntf_start, length, &reg);
                        if (msg != NULL)
                          {
                            if (test_verbose > 0)
                              {
                                char *quoted = support_quote_blob
                                  (buffer, length);
                                printf ("info: compilation failed for pattern"
                                        " \"%s\", syntax 0x%lx: %s\n",
                                        quoted, re_syntax_options, msg);
                                free (quoted);
                              }
                          }
                        else
                          regfree (&reg);
                      }
                }
            }
        }
    }

  support_next_to_fault_free (&ntf);

  return 0;
}

#include <support/test-driver.c>