Coverage Report

Created: 2026-07-16 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libgit2/deps/pcre2/pcre2_study.c
Line
Count
Source
1
/*************************************************
2
*      Perl-Compatible Regular Expressions       *
3
*************************************************/
4
5
/* PCRE is a library of functions to support regular expressions whose syntax
6
and semantics are as close as possible to those of the Perl 5 language.
7
8
                       Written by Philip Hazel
9
     Original API code Copyright (c) 1997-2012 University of Cambridge
10
          New API code Copyright (c) 2016-2024 University of Cambridge
11
12
-----------------------------------------------------------------------------
13
Redistribution and use in source and binary forms, with or without
14
modification, are permitted provided that the following conditions are met:
15
16
    * Redistributions of source code must retain the above copyright notice,
17
      this list of conditions and the following disclaimer.
18
19
    * Redistributions in binary form must reproduce the above copyright
20
      notice, this list of conditions and the following disclaimer in the
21
      documentation and/or other materials provided with the distribution.
22
23
    * Neither the name of the University of Cambridge nor the names of its
24
      contributors may be used to endorse or promote products derived from
25
      this software without specific prior written permission.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
POSSIBILITY OF SUCH DAMAGE.
38
-----------------------------------------------------------------------------
39
*/
40
41
42
/* This module contains functions for scanning a compiled pattern and
43
collecting data (e.g. minimum matching length). */
44
45
46
#include "pcre2_internal.h"
47
48
49
50
/* The maximum remembered capturing brackets minimum. */
51
52
21.9k
#define MAX_CACHE_BACKREF 128
53
54
/* Set a bit in the starting code unit bit map. */
55
56
162k
#define SET_BIT(c) re->start_bitmap[(c)/8] |= (1u << ((c)&7))
57
58
/* Returns from set_start_bits() */
59
60
enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN, SSB_TOODEEP };
61
62
63
/*************************************************
64
*   Find the minimum subject length for a group  *
65
*************************************************/
66
67
/* Scan a parenthesized group and compute the minimum length of subject that
68
is needed to match it. This is a lower bound; it does not mean there is a
69
string of that length that matches. In UTF mode, the result is in characters
70
rather than code units. The field in a compiled pattern for storing the minimum
71
length is 16-bits long (on the grounds that anything longer than that is
72
pathological), so we give up when we reach that amount. This also means that
73
integer overflow for really crazy patterns cannot happen.
74
75
Backreference minimum lengths are cached to speed up multiple references. This
76
function is called only when the highest back reference in the pattern is less
77
than or equal to MAX_CACHE_BACKREF, which is one less than the size of the
78
caching vector. The zeroth element contains the number of the highest set
79
value.
80
81
Arguments:
82
  re              compiled pattern block
83
  code            pointer to start of group (the bracket)
84
  startcode       pointer to start of the whole pattern's code
85
  utf             UTF flag
86
  recurses        chain of recurse_check to catch mutual recursion
87
  countptr        pointer to call count (to catch over complexity)
88
  backref_cache   vector for caching back references.
89
90
This function is no longer called when the pattern contains (*ACCEPT); however,
91
the old code for returning -1 is retained, just in case.
92
93
Returns:   the minimum length
94
           -1 \C in UTF-8 mode
95
              or (*ACCEPT)
96
              or pattern too complicated
97
           -2 internal error (missing capturing bracket)
98
           -3 internal error (opcode not listed)
99
*/
100
101
static int
102
find_minlength(const pcre2_real_code *re, PCRE2_SPTR code,
103
  PCRE2_SPTR startcode, BOOL utf, recurse_check *recurses, int *countptr,
104
  int *backref_cache)
105
238k
{
106
238k
int length = -1;
107
238k
int branchlength = 0;
108
238k
int prev_cap_recno = -1;
109
238k
int prev_cap_d = 0;
110
238k
int prev_recurse_recno = -1;
111
238k
int prev_recurse_d = 0;
112
238k
uint32_t once_fudge = 0;
113
238k
BOOL had_recurse = FALSE;
114
238k
BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
115
238k
PCRE2_SPTR nextbranch = code + GET(code, 1);
116
238k
PCRE2_SPTR cc = code + 1 + LINK_SIZE;
117
238k
recurse_check this_recurse;
118
119
/* If this is a "could be empty" group, its minimum length is 0. */
120
121
238k
if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
122
123
/* Skip over capturing bracket number */
124
125
235k
if (*code == OP_CBRA || *code == OP_CBRAPOS) cc += IMM2_SIZE;
126
127
/* A large and/or complex regex can take too long to process. */
128
129
235k
if ((*countptr)++ > 1000) return -1;
130
131
/* Scan along the opcodes for this branch. If we get to the end of the branch,
132
check the length against that of the other branches. If the accumulated length
133
passes 16-bits, reset to that value and skip the rest of the branch. */
134
135
235k
for (;;)
136
2.72M
  {
137
2.72M
  int d, min, recno;
138
2.72M
  PCRE2_UCHAR op;
139
2.72M
  PCRE2_SPTR cs, ce;
140
141
2.72M
  if (branchlength >= (int)UINT16_MAX)
142
1.17k
    {
143
1.17k
    branchlength = UINT16_MAX;
144
1.17k
    cc = nextbranch;
145
1.17k
    }
146
147
2.72M
  op = *cc;
148
2.72M
  switch (op)
149
2.72M
    {
150
4.90k
    case OP_COND:
151
6.32k
    case OP_SCOND:
152
153
    /* If there is only one branch in a condition, the implied branch has zero
154
    length, so we don't add anything. This covers the DEFINE "condition"
155
    automatically. If there are two branches we can treat it the same as any
156
    other non-capturing subpattern. */
157
158
6.32k
    cs = cc + GET(cc, 1);
159
6.32k
    if (*cs != OP_ALT)
160
4.66k
      {
161
4.66k
      cc = cs + 1 + LINK_SIZE;
162
4.66k
      break;
163
4.66k
      }
164
1.65k
    goto PROCESS_NON_CAPTURE;
165
166
95.3k
    case OP_BRA:
167
    /* There's a special case of OP_BRA, when it is wrapped round a repeated
168
    OP_RECURSE. We'd like to process the latter at this level so that
169
    remembering the value works for repeated cases. So we do nothing, but
170
    set a fudge value to skip over the OP_KET after the recurse. */
171
172
95.3k
    if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
173
1.30k
      {
174
1.30k
      once_fudge = 1 + LINK_SIZE;
175
1.30k
      cc += 1 + LINK_SIZE;
176
1.30k
      break;
177
1.30k
      }
178
94.0k
    PCRE2_FALLTHROUGH /* Fall through */
179
94.0k
180
101k
    case OP_ONCE:
181
106k
    case OP_SCRIPT_RUN:
182
107k
    case OP_SBRA:
183
109k
    case OP_BRAPOS:
184
110k
    case OP_SBRAPOS:
185
112k
    PROCESS_NON_CAPTURE:
186
112k
    d = find_minlength(re, cc, startcode, utf, recurses, countptr,
187
112k
      backref_cache);
188
112k
    if (d < 0) return d;
189
111k
    branchlength += d;
190
121k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
191
111k
    cc += 1 + LINK_SIZE;
192
111k
    break;
193
194
    /* To save time for repeated capturing subpatterns, we remember the
195
    length of the previous one. Unfortunately we can't do the same for
196
    the unnumbered ones above. Nor can we do this if (?| is present in the
197
    pattern because captures with the same number are not then identical. */
198
199
383k
    case OP_CBRA:
200
384k
    case OP_SCBRA:
201
386k
    case OP_CBRAPOS:
202
387k
    case OP_SCBRAPOS:
203
387k
    recno = (int)GET2(cc, 1+LINK_SIZE);
204
387k
    if (dupcapused || recno != prev_cap_recno)
205
87.0k
      {
206
87.0k
      prev_cap_recno = recno;
207
87.0k
      prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
208
87.0k
        backref_cache);
209
87.0k
      if (prev_cap_d < 0) return prev_cap_d;
210
87.0k
      }
211
386k
    branchlength += prev_cap_d;
212
419k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
213
386k
    cc += 1 + LINK_SIZE;
214
386k
    break;
215
216
    /* ACCEPT makes things far too complicated; we have to give up. In fact,
217
    from 10.34 onwards, if a pattern contains (*ACCEPT), this function is not
218
    used. However, leave the code in place, just in case. */
219
220
0
    case OP_ACCEPT:
221
0
    case OP_ASSERT_ACCEPT:
222
0
    return -1;
223
224
    /* Reached end of a branch; if it's a ket it is the end of a nested
225
    call. If it's ALT it is an alternation in a nested call. If it is END it's
226
    the end of the outer call. All can be handled by the same code. If the
227
    length of any branch is zero, there is no need to scan any subsequent
228
    branches. */
229
230
19.4k
    case OP_ALT:
231
245k
    case OP_KET:
232
247k
    case OP_KETRMAX:
233
247k
    case OP_KETRMIN:
234
250k
    case OP_KETRPOS:
235
250k
    case OP_END:
236
250k
    if (length < 0 || (!had_recurse && branchlength < length))
237
243k
      length = branchlength;
238
250k
    if (op != OP_ALT || length == 0) return length;
239
16.9k
    nextbranch = cc + GET(cc, 1);
240
16.9k
    cc += 1 + LINK_SIZE;
241
16.9k
    branchlength = 0;
242
16.9k
    had_recurse = FALSE;
243
16.9k
    break;
244
245
    /* Skip over assertive subpatterns */
246
247
1.48k
    case OP_ASSERT:
248
2.89k
    case OP_ASSERT_NOT:
249
4.27k
    case OP_ASSERTBACK:
250
6.23k
    case OP_ASSERTBACK_NOT:
251
7.77k
    case OP_ASSERT_NA:
252
8.84k
    case OP_ASSERT_SCS:
253
11.3k
    case OP_ASSERTBACK_NA:
254
13.1k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
255
11.3k
    PCRE2_FALLTHROUGH /* Fall through */
256
257
    /* Skip over things that don't match chars */
258
259
11.3k
    case OP_REVERSE:
260
11.3k
    case OP_VREVERSE:
261
27.0k
    case OP_CREF:
262
27.3k
    case OP_DNCREF:
263
27.8k
    case OP_RREF:
264
28.3k
    case OP_DNRREF:
265
28.3k
    case OP_FALSE:
266
28.3k
    case OP_TRUE:
267
32.0k
    case OP_CALLOUT:
268
32.5k
    case OP_SOD:
269
33.3k
    case OP_SOM:
270
34.1k
    case OP_EOD:
271
34.8k
    case OP_EODN:
272
48.7k
    case OP_CIRC:
273
49.0k
    case OP_CIRCM:
274
55.1k
    case OP_DOLL:
275
56.3k
    case OP_DOLLM:
276
56.9k
    case OP_NOT_WORD_BOUNDARY:
277
57.8k
    case OP_WORD_BOUNDARY:
278
59.3k
    case OP_NOT_UCP_WORD_BOUNDARY:
279
59.8k
    case OP_UCP_WORD_BOUNDARY:
280
59.8k
    cc += PRIV(OP_lengths)[*cc];
281
59.8k
    break;
282
283
1.81k
    case OP_CALLOUT_STR:
284
1.81k
    cc += GET(cc, 1 + 2*LINK_SIZE);
285
1.81k
    break;
286
287
    /* Skip over a subpattern that has a {0} or {0,x} quantifier */
288
289
4.91k
    case OP_BRAZERO:
290
5.36k
    case OP_BRAMINZERO:
291
5.90k
    case OP_BRAPOSZERO:
292
6.29k
    case OP_SKIPZERO:
293
6.29k
    cc += PRIV(OP_lengths)[*cc];
294
6.63k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
295
6.29k
    cc += 1 + LINK_SIZE;
296
6.29k
    break;
297
298
    /* Handle literal characters and + repetitions */
299
300
1.46M
    case OP_CHAR:
301
1.61M
    case OP_CHARI:
302
1.61M
    case OP_NOT:
303
1.61M
    case OP_NOTI:
304
1.62M
    case OP_PLUS:
305
1.62M
    case OP_PLUSI:
306
1.62M
    case OP_MINPLUS:
307
1.62M
    case OP_MINPLUSI:
308
1.64M
    case OP_POSPLUS:
309
1.64M
    case OP_POSPLUSI:
310
1.64M
    case OP_NOTPLUS:
311
1.64M
    case OP_NOTPLUSI:
312
1.64M
    case OP_NOTMINPLUS:
313
1.64M
    case OP_NOTMINPLUSI:
314
1.65M
    case OP_NOTPOSPLUS:
315
1.65M
    case OP_NOTPOSPLUSI:
316
1.65M
    branchlength++;
317
1.65M
    cc += 2;
318
1.65M
#ifdef SUPPORT_UNICODE
319
1.65M
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
320
1.65M
#endif
321
1.65M
    break;
322
323
1.28k
    case OP_TYPEPLUS:
324
1.96k
    case OP_TYPEMINPLUS:
325
3.67k
    case OP_TYPEPOSPLUS:
326
3.67k
    branchlength++;
327
3.67k
    cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
328
3.67k
    break;
329
330
    /* Handle exact repetitions. The count is already in characters, but we
331
    may need to skip over a multibyte character in UTF mode.  */
332
333
4.48k
    case OP_EXACT:
334
5.59k
    case OP_EXACTI:
335
6.08k
    case OP_NOTEXACT:
336
6.83k
    case OP_NOTEXACTI:
337
6.83k
    branchlength += GET2(cc,1);
338
6.83k
    cc += 2 + IMM2_SIZE;
339
6.83k
#ifdef SUPPORT_UNICODE
340
6.83k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
341
6.83k
#endif
342
6.83k
    break;
343
344
1.92k
    case OP_TYPEEXACT:
345
1.92k
    branchlength += GET2(cc,1);
346
1.92k
    cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
347
1.24k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
348
1.92k
    break;
349
350
    /* Handle single-char non-literal matchers */
351
352
4.82k
    case OP_PROP:
353
6.82k
    case OP_NOTPROP:
354
6.82k
    cc += 2;
355
6.82k
    PCRE2_FALLTHROUGH /* Fall through */
356
357
7.80k
    case OP_NOT_DIGIT:
358
8.86k
    case OP_DIGIT:
359
9.69k
    case OP_NOT_WHITESPACE:
360
12.8k
    case OP_WHITESPACE:
361
13.7k
    case OP_NOT_WORDCHAR:
362
15.1k
    case OP_WORDCHAR:
363
25.7k
    case OP_ANY:
364
26.7k
    case OP_ALLANY:
365
28.0k
    case OP_EXTUNI:
366
32.1k
    case OP_HSPACE:
367
34.2k
    case OP_NOT_HSPACE:
368
34.6k
    case OP_VSPACE:
369
35.9k
    case OP_NOT_VSPACE:
370
35.9k
    branchlength++;
371
35.9k
    cc++;
372
35.9k
    break;
373
374
    /* "Any newline" might match two characters, but it also might match just
375
    one. */
376
377
1.18k
    case OP_ANYNL:
378
1.18k
    branchlength += 1;
379
1.18k
    cc++;
380
1.18k
    break;
381
382
    /* The single-byte matcher means we can't proceed in UTF mode. (In
383
    non-UTF mode \C will actually be turned into OP_ALLANY, so won't ever
384
    appear, but leave the code, just in case.) */
385
386
14
    case OP_ANYBYTE:
387
14
#ifdef SUPPORT_UNICODE
388
14
    if (utf) return -1;
389
0
#endif
390
0
    branchlength++;
391
0
    cc++;
392
0
    break;
393
394
    /* For repeated character types, we have to test for \p and \P, which have
395
    an extra two bytes of parameters. */
396
397
12.9k
    case OP_TYPESTAR:
398
14.6k
    case OP_TYPEMINSTAR:
399
16.9k
    case OP_TYPEQUERY:
400
19.1k
    case OP_TYPEMINQUERY:
401
20.2k
    case OP_TYPEPOSSTAR:
402
22.4k
    case OP_TYPEPOSQUERY:
403
22.4k
    if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
404
22.4k
    cc += PRIV(OP_lengths)[op];
405
22.4k
    break;
406
407
954
    case OP_TYPEUPTO:
408
1.50k
    case OP_TYPEMINUPTO:
409
2.97k
    case OP_TYPEPOSUPTO:
410
2.97k
    if (cc[1 + IMM2_SIZE] == OP_PROP
411
2.62k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
412
2.97k
    cc += PRIV(OP_lengths)[op];
413
2.97k
    break;
414
415
    /* Check a class for variable quantification */
416
417
18.7k
    case OP_CLASS:
418
30.6k
    case OP_NCLASS:
419
30.6k
#ifdef SUPPORT_WIDE_CHARS
420
38.3k
    case OP_XCLASS:
421
41.0k
    case OP_ECLASS:
422
    /* The original code caused an unsigned overflow in 64 bit systems,
423
    so now we use a conditional statement. */
424
41.0k
    if (op == OP_XCLASS || op == OP_ECLASS)
425
10.3k
      cc += GET(cc, 1);
426
30.6k
    else
427
30.6k
#endif
428
30.6k
      cc += PRIV(OP_lengths)[OP_CLASS];
429
430
41.0k
    switch (*cc)
431
41.0k
      {
432
1.67k
      case OP_CRPLUS:
433
2.33k
      case OP_CRMINPLUS:
434
4.91k
      case OP_CRPOSPLUS:
435
4.91k
      branchlength++;
436
4.91k
      PCRE2_FALLTHROUGH /* Fall through */
437
438
5.80k
      case OP_CRSTAR:
439
6.78k
      case OP_CRMINSTAR:
440
11.4k
      case OP_CRQUERY:
441
15.1k
      case OP_CRMINQUERY:
442
16.0k
      case OP_CRPOSSTAR:
443
19.1k
      case OP_CRPOSQUERY:
444
19.1k
      cc++;
445
19.1k
      break;
446
447
481
      case OP_CRRANGE:
448
911
      case OP_CRMINRANGE:
449
2.34k
      case OP_CRPOSRANGE:
450
2.34k
      branchlength += GET2(cc,1);
451
2.34k
      cc += 1 + 2 * IMM2_SIZE;
452
2.34k
      break;
453
454
19.5k
      default:
455
19.5k
      branchlength++;
456
19.5k
      break;
457
41.0k
      }
458
41.0k
    break;
459
460
    /* Backreferences and subroutine calls (OP_RECURSE) are treated in the same
461
    way: we find the minimum length for the subpattern. A recursion
462
    (backreference or subroutine) causes an a flag to be set that causes the
463
    length of this branch to be ignored. The logic is that a recursion can only
464
    make sense if there is another alternative that stops the recursing. That
465
    will provide the minimum length (when no recursion happens).
466
467
    If PCRE2_MATCH_UNSET_BACKREF is set, a backreference to an unset bracket
468
    matches an empty string (by default it causes a matching failure), so in
469
    that case we must set the minimum length to zero.
470
471
    For backreferenes, if duplicate numbers are present in the pattern we check
472
    for a reference to a duplicate. If it is, we don't know which version will
473
    be referenced, so we have to set the minimum length to zero. */
474
475
    /* Duplicate named pattern back reference. */
476
477
41.0k
    case OP_DNREF:
478
6.54k
    case OP_DNREFI:
479
6.54k
    if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
480
6.11k
      {
481
6.11k
      int count = GET2(cc, 1+IMM2_SIZE);
482
6.11k
      PCRE2_SPTR slot =
483
6.11k
        (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) +
484
6.11k
          GET2(cc, 1) * re->name_entry_size;
485
486
6.11k
      d = INT_MAX;
487
488
      /* Scan all groups with the same name; find the shortest. */
489
490
24.9k
      while (count-- > 0)
491
23.1k
        {
492
23.1k
        int dd, i;
493
23.1k
        recno = GET2(slot, 0);
494
495
23.1k
        if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
496
17.9k
          dd = backref_cache[recno];
497
5.24k
        else
498
5.24k
          {
499
5.24k
          ce = cs = PRIV(find_bracket)(startcode, utf, recno);
500
5.24k
          if (cs == NULL) return -2;
501
5.61k
          do ce += GET(ce, 1); while (*ce == OP_ALT);
502
503
5.24k
          dd = 0;
504
5.24k
          if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
505
5.24k
            {
506
5.24k
            if (cc > cs && cc < ce)    /* Simple recursion */
507
718
              {
508
718
              had_recurse = TRUE;
509
718
              }
510
4.52k
            else
511
4.52k
              {
512
4.52k
              recurse_check *r = recurses;
513
14.2k
              for (r = recurses; r != NULL; r = r->prev)
514
10.4k
                if (r->group == cs) break;
515
4.52k
              if (r != NULL)           /* Mutual recursion */
516
645
                {
517
645
                had_recurse = TRUE;
518
645
                }
519
3.88k
              else
520
3.88k
                {
521
3.88k
                this_recurse.prev = recurses;  /* No recursion */
522
3.88k
                this_recurse.group = cs;
523
3.88k
                dd = find_minlength(re, cs, startcode, utf, &this_recurse,
524
3.88k
                  countptr, backref_cache);
525
3.88k
                if (dd < 0) return dd;
526
3.88k
                }
527
4.52k
              }
528
5.24k
            }
529
530
5.22k
          backref_cache[recno] = dd;
531
21.4k
          for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
532
5.22k
          backref_cache[0] = recno;
533
5.22k
          }
534
535
23.1k
        if (dd < d) d = dd;
536
23.1k
        if (d <= 0) break;    /* No point looking at any more */
537
18.7k
        slot += re->name_entry_size;
538
18.7k
        }
539
6.11k
      }
540
424
    else d = 0;
541
6.52k
    cc += PRIV(OP_lengths)[*cc];
542
6.52k
    goto REPEAT_BACK_REFERENCE;
543
544
    /* Single back reference by number. References by name are converted to by
545
    number when there is no duplication. */
546
547
20.6k
    case OP_REF:
548
24.3k
    case OP_REFI:
549
24.3k
    recno = GET2(cc, 1);
550
24.3k
    if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
551
16.9k
      d = backref_cache[recno];
552
7.40k
    else
553
7.40k
      {
554
7.40k
      int i;
555
7.40k
      d = 0;
556
557
7.40k
      if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
558
7.40k
        {
559
7.40k
        ce = cs = PRIV(find_bracket)(startcode, utf, recno);
560
7.40k
        if (cs == NULL) return -2;
561
8.50k
        do ce += GET(ce, 1); while (*ce == OP_ALT);
562
563
7.40k
        if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
564
7.15k
          {
565
7.15k
          if (cc > cs && cc < ce)    /* Simple recursion */
566
1.02k
            {
567
1.02k
            had_recurse = TRUE;
568
1.02k
            }
569
6.12k
          else
570
6.12k
            {
571
6.12k
            recurse_check *r = recurses;
572
46.1k
            for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
573
6.12k
            if (r != NULL)           /* Mutual recursion */
574
595
              {
575
595
              had_recurse = TRUE;
576
595
              }
577
5.53k
            else                     /* No recursion */
578
5.53k
              {
579
5.53k
              this_recurse.prev = recurses;
580
5.53k
              this_recurse.group = cs;
581
5.53k
              d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
582
5.53k
                backref_cache);
583
5.53k
              if (d < 0) return d;
584
5.53k
              }
585
6.12k
            }
586
7.15k
          }
587
7.40k
        }
588
589
7.33k
      backref_cache[recno] = d;
590
14.6k
      for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
591
7.33k
      backref_cache[0] = recno;
592
7.33k
      }
593
594
24.2k
    cc += PRIV(OP_lengths)[*cc];
595
596
    /* Handle repeated back references */
597
598
30.7k
    REPEAT_BACK_REFERENCE:
599
30.7k
    switch (*cc)
600
30.7k
      {
601
882
      case OP_CRSTAR:
602
1.59k
      case OP_CRMINSTAR:
603
4.16k
      case OP_CRQUERY:
604
4.63k
      case OP_CRMINQUERY:
605
4.63k
      case OP_CRPOSSTAR:
606
4.63k
      case OP_CRPOSQUERY:
607
4.63k
      min = 0;
608
4.63k
      cc++;
609
4.63k
      break;
610
611
816
      case OP_CRPLUS:
612
1.45k
      case OP_CRMINPLUS:
613
1.45k
      case OP_CRPOSPLUS:
614
1.45k
      min = 1;
615
1.45k
      cc++;
616
1.45k
      break;
617
618
549
      case OP_CRRANGE:
619
978
      case OP_CRMINRANGE:
620
978
      case OP_CRPOSRANGE:
621
978
      min = GET2(cc, 1);
622
978
      cc += 1 + 2 * IMM2_SIZE;
623
978
      break;
624
625
23.6k
      default:
626
23.6k
      min = 1;
627
23.6k
      break;
628
30.7k
      }
629
630
    /* Take care not to overflow: (1) min and d are ints, so check that their
631
    product is not greater than INT_MAX. (2) branchlength is limited to
632
    UINT16_MAX (checked at the top of the loop). */
633
634
30.7k
    if ((d > 0 && (INT_MAX/d) < min) || (int)UINT16_MAX - branchlength < min*d)
635
895
      branchlength = UINT16_MAX;
636
29.8k
    else branchlength += min * d;
637
30.7k
    break;
638
639
    /* Recursion always refers to the first occurrence of a subpattern with a
640
    given number. Therefore, we can always make use of caching, even when the
641
    pattern contains multiple subpatterns with the same number. */
642
643
44.4k
    case OP_RECURSE:
644
44.4k
    cs = ce = startcode + GET(cc, 1);
645
44.4k
    recno = GET2(cs, 1+LINK_SIZE);
646
44.4k
    if (recno == prev_recurse_recno)
647
8.93k
      {
648
8.93k
      branchlength += prev_recurse_d;
649
8.93k
      }
650
35.4k
    else
651
35.4k
      {
652
36.9k
      do ce += GET(ce, 1); while (*ce == OP_ALT);
653
35.4k
      if (cc > cs && cc < ce)    /* Simple recursion */
654
25.7k
        had_recurse = TRUE;
655
9.72k
      else
656
9.72k
        {
657
9.72k
        recurse_check *r = recurses;
658
158k
        for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
659
9.72k
        if (r != NULL)          /* Mutual recursion */
660
1.22k
          had_recurse = TRUE;
661
8.49k
        else
662
8.49k
          {
663
8.49k
          this_recurse.prev = recurses;
664
8.49k
          this_recurse.group = cs;
665
8.49k
          prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
666
8.49k
            countptr, backref_cache);
667
8.49k
          if (prev_recurse_d < 0) return prev_recurse_d;
668
8.27k
          prev_recurse_recno = recno;
669
8.27k
          branchlength += prev_recurse_d;
670
8.27k
          }
671
9.72k
        }
672
35.4k
      }
673
44.1k
    cc += 1 + LINK_SIZE + once_fudge;
674
44.1k
    once_fudge = 0;
675
44.1k
    break;
676
677
    /* Anything else does not or need not match a character. We can get the
678
    item's length from the table, but for those that can match zero occurrences
679
    of a character, we must take special action for UTF-8 characters. As it
680
    happens, the "NOT" versions of these opcodes are used at present only for
681
    ASCII characters, so they could be omitted from this list. However, in
682
    future that may change, so we include them here so as not to leave a
683
    gotcha for a future maintainer. */
684
685
638
    case OP_UPTO:
686
917
    case OP_UPTOI:
687
1.24k
    case OP_NOTUPTO:
688
2.29k
    case OP_NOTUPTOI:
689
3.46k
    case OP_MINUPTO:
690
3.89k
    case OP_MINUPTOI:
691
4.11k
    case OP_NOTMINUPTO:
692
4.46k
    case OP_NOTMINUPTOI:
693
6.75k
    case OP_POSUPTO:
694
7.51k
    case OP_POSUPTOI:
695
7.97k
    case OP_NOTPOSUPTO:
696
8.31k
    case OP_NOTPOSUPTOI:
697
698
9.79k
    case OP_STAR:
699
10.8k
    case OP_STARI:
700
11.2k
    case OP_NOTSTAR:
701
11.8k
    case OP_NOTSTARI:
702
12.2k
    case OP_MINSTAR:
703
13.3k
    case OP_MINSTARI:
704
14.1k
    case OP_NOTMINSTAR:
705
14.4k
    case OP_NOTMINSTARI:
706
19.0k
    case OP_POSSTAR:
707
20.7k
    case OP_POSSTARI:
708
21.2k
    case OP_NOTPOSSTAR:
709
21.5k
    case OP_NOTPOSSTARI:
710
711
27.2k
    case OP_QUERY:
712
29.3k
    case OP_QUERYI:
713
29.7k
    case OP_NOTQUERY:
714
30.2k
    case OP_NOTQUERYI:
715
31.3k
    case OP_MINQUERY:
716
32.9k
    case OP_MINQUERYI:
717
33.1k
    case OP_NOTMINQUERY:
718
33.7k
    case OP_NOTMINQUERYI:
719
41.3k
    case OP_POSQUERY:
720
45.2k
    case OP_POSQUERYI:
721
46.5k
    case OP_NOTPOSQUERY:
722
47.6k
    case OP_NOTPOSQUERYI:
723
724
47.6k
    cc += PRIV(OP_lengths)[op];
725
47.6k
#ifdef SUPPORT_UNICODE
726
47.6k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
727
47.6k
#endif
728
47.6k
    break;
729
730
    /* Skip these, but we need to add in the name length. */
731
732
1.43k
    case OP_MARK:
733
1.94k
    case OP_COMMIT_ARG:
734
2.29k
    case OP_PRUNE_ARG:
735
2.83k
    case OP_SKIP_ARG:
736
3.36k
    case OP_THEN_ARG:
737
3.36k
    cc += PRIV(OP_lengths)[op] + cc[1];
738
3.36k
    break;
739
740
    /* The remaining opcodes are just skipped over. */
741
742
0
    case OP_CLOSE:
743
444
    case OP_COMMIT:
744
1.09k
    case OP_FAIL:
745
1.78k
    case OP_PRUNE:
746
2.37k
    case OP_SET_SOM:
747
2.92k
    case OP_SKIP:
748
3.40k
    case OP_THEN:
749
3.40k
    cc += PRIV(OP_lengths)[op];
750
3.40k
    break;
751
752
    /* This should not occur: we list all opcodes explicitly so that when
753
    new ones get added they are properly considered. */
754
755
    /* LCOV_EXCL_START */
756
0
    default:
757
0
    PCRE2_DEBUG_UNREACHABLE();
758
0
    return -3;
759
    /* LCOV_EXCL_STOP */
760
2.72M
    }
761
2.72M
  }
762
763
/* LCOV_EXCL_START */
764
0
PCRE2_DEBUG_UNREACHABLE(); /* Control should never reach here */
765
0
return -3;                 /* Avoid compiler warnings */
766
/* LCOV_EXCL_STOP */
767
235k
}
768
769
770
771
/*************************************************
772
*      Set a bit and maybe its alternate case    *
773
*************************************************/
774
775
/* Given a character, set its first code unit's bit in the table, and also the
776
corresponding bit for the other version of a letter if we are caseless.
777
778
Arguments:
779
  re            points to the regex block
780
  p             points to the first code unit of the character
781
  caseless      TRUE if caseless
782
  utf           TRUE for UTF mode
783
  ucp           TRUE for UCP mode
784
785
Returns:        pointer after the character
786
*/
787
788
static PCRE2_SPTR
789
set_table_bit(pcre2_real_code *re, PCRE2_SPTR p, BOOL caseless, BOOL utf,
790
  BOOL ucp)
791
71.2k
{
792
71.2k
uint32_t c = *p++;   /* First code unit */
793
794
71.2k
(void)utf;           /* Stop compiler warnings when UTF not supported */
795
71.2k
(void)ucp;
796
797
/* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for
798
0xff. */
799
800
#if PCRE2_CODE_UNIT_WIDTH != 8
801
if (c > 0xff) SET_BIT(0xff); else
802
#endif
803
804
71.2k
SET_BIT(c);
805
806
/* In UTF-8 or UTF-16 mode, pick up the remaining code units in order to find
807
the end of the character, even when caseless. */
808
809
71.2k
#ifdef SUPPORT_UNICODE
810
71.2k
if (utf)
811
18.6k
  {
812
18.6k
#if PCRE2_CODE_UNIT_WIDTH == 8
813
18.6k
  if (c >= 0xc0) GETUTF8INC(c, p);
814
#elif PCRE2_CODE_UNIT_WIDTH == 16
815
  if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
816
#endif
817
18.6k
  }
818
71.2k
#endif  /* SUPPORT_UNICODE */
819
820
/* If caseless, handle the other case of the character. */
821
822
71.2k
if (caseless)
823
18.5k
  {
824
18.5k
#ifdef SUPPORT_UNICODE
825
18.5k
  if (utf || ucp)
826
3.23k
    {
827
3.23k
    c = UCD_OTHERCASE(c);
828
3.23k
#if PCRE2_CODE_UNIT_WIDTH == 8
829
3.23k
    if (utf)
830
2.09k
      {
831
2.09k
      PCRE2_UCHAR buff[6];
832
2.09k
      (void)PRIV(ord2utf)(c, buff);
833
2.09k
      SET_BIT(buff[0]);
834
2.09k
      }
835
1.14k
    else if (c < 256) SET_BIT(c);
836
#else  /* 16-bit or 32-bit mode */
837
    if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
838
#endif
839
3.23k
    }
840
841
15.2k
  else
842
15.2k
#endif  /* SUPPORT_UNICODE */
843
844
  /* Not UTF or UCP */
845
846
15.2k
  if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
847
18.5k
  }
848
849
71.2k
return p;
850
71.2k
}
851
852
853
854
/*************************************************
855
*     Set bits for a positive character type     *
856
*************************************************/
857
858
/* This function sets starting bits for a character type. In UTF-8 mode, we can
859
only do a direct setting for bytes less than 128, as otherwise there can be
860
confusion with bytes in the middle of UTF-8 characters. In a "traditional"
861
environment, the tables will only recognize ASCII characters anyway, but in at
862
least one Windows environment, some higher bytes bits were set in the tables.
863
So we deal with that case by considering the UTF-8 encoding.
864
865
Arguments:
866
  re             the regex block
867
  cbit type      the type of character wanted
868
  table_limit    32 for non-UTF-8; 16 for UTF-8
869
870
Returns:         nothing
871
*/
872
873
static void
874
set_type_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
875
25.7k
{
876
25.7k
uint32_t c;
877
546k
for (c = 0; c < table_limit; c++)
878
520k
  re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
879
25.7k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
880
25.7k
if (table_limit == 32) return;
881
2.45M
for (c = 128; c < 256; c++)
882
2.43M
  {
883
2.43M
  if ((re->tables[cbits_offset + c/8] & (1u << (c&7))) != 0)
884
0
    {
885
0
    PCRE2_UCHAR buff[6];
886
0
    (void)PRIV(ord2utf)(c, buff);
887
0
    SET_BIT(buff[0]);
888
0
    }
889
2.43M
  }
890
19.0k
#endif  /* UTF-8 */
891
19.0k
}
892
893
894
/*************************************************
895
*     Set bits for a negative character type     *
896
*************************************************/
897
898
/* This function sets starting bits for a negative character type such as \D.
899
In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
900
otherwise there can be confusion with bytes in the middle of UTF-8 characters.
901
Unlike in the positive case, where we can set appropriate starting bits for
902
specific high-valued UTF-8 characters, in this case we have to set the bits for
903
all high-valued characters. The lowest is 0xc2, but we overkill by starting at
904
0xc0 (192) for simplicity.
905
906
Arguments:
907
  re             the regex block
908
  cbit type      the type of character wanted
909
  table_limit    32 for non-UTF-8; 16 for UTF-8
910
911
Returns:         nothing
912
*/
913
914
static void
915
set_nottype_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
916
10.0k
{
917
10.0k
uint32_t c;
918
277k
for (c = 0; c < table_limit; c++)
919
266k
  re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type]));
920
10.0k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
921
30.9k
if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
922
10.0k
#endif
923
10.0k
}
924
925
926
927
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
928
/*************************************************
929
*     Set starting bits for a character list.    *
930
*************************************************/
931
932
/* This function sets starting bits for a character list. It enumerates
933
all characters and character ranges in the character list, and sets
934
the starting bits accordingly.
935
936
Arguments:
937
  code           pointer to the code
938
  start_bitmap   pointer to the starting bitmap
939
940
Returns:         nothing
941
*/
942
static void
943
study_char_list(PCRE2_SPTR code, uint8_t *start_bitmap,
944
  const uint8_t *char_lists_end)
945
7.91k
{
946
7.91k
uint32_t type, list_ind;
947
7.91k
uint32_t char_list_add = XCL_CHAR_LIST_LOW_16_ADD;
948
7.91k
uint32_t range_start = ~(uint32_t)0, range_end = 0;
949
7.91k
const uint8_t *next_char;
950
7.91k
PCRE2_UCHAR start_buffer[6], end_buffer[6];
951
7.91k
PCRE2_UCHAR start, end;
952
953
/* Only needed in 8-bit mode at the moment. */
954
7.91k
type = (uint32_t)(code[0] << 8) | code[1];
955
7.91k
code += 2;
956
957
/* Align characters. */
958
7.91k
next_char = char_lists_end - (GET(code, 0) << 1);
959
7.91k
type &= XCL_TYPE_MASK;
960
7.91k
list_ind = 0;
961
962
7.91k
if ((type & XCL_BEGIN_WITH_RANGE) != 0)
963
2.45k
  range_start = XCL_CHAR_LIST_LOW_16_START;
964
965
23.7k
while (type > 0)
966
15.8k
  {
967
15.8k
  uint32_t item_count = type & XCL_ITEM_COUNT_MASK;
968
969
15.8k
  if (item_count == XCL_ITEM_COUNT_MASK)
970
8.54k
    {
971
8.54k
    if (list_ind <= 1)
972
7.72k
      {
973
7.72k
      item_count = *(const uint16_t*)next_char;
974
7.72k
      next_char += 2;
975
7.72k
      }
976
812
    else
977
812
      {
978
812
      item_count = *(const uint32_t*)next_char;
979
812
      next_char += 4;
980
812
      }
981
8.54k
    }
982
983
97.5k
  while (item_count > 0)
984
81.7k
    {
985
81.7k
    if (list_ind <= 1)
986
75.7k
      {
987
75.7k
      range_end = *(const uint16_t*)next_char;
988
75.7k
      next_char += 2;
989
75.7k
      }
990
5.91k
    else
991
5.91k
      {
992
5.91k
      range_end = *(const uint32_t*)next_char;
993
5.91k
      next_char += 4;
994
5.91k
      }
995
996
81.7k
    if ((range_end & XCL_CHAR_END) != 0)
997
56.4k
      {
998
56.4k
      range_end = char_list_add + (range_end >> XCL_CHAR_SHIFT);
999
1000
56.4k
      PRIV(ord2utf)(range_end, end_buffer);
1001
56.4k
      end = end_buffer[0];
1002
1003
56.4k
      if (range_start < range_end)
1004
27.2k
        {
1005
27.2k
        PRIV(ord2utf)(range_start, start_buffer);
1006
165k
        for (start = start_buffer[0]; start <= end; start++)
1007
138k
          start_bitmap[start / 8] |= (1u << (start & 7));
1008
27.2k
        }
1009
29.2k
      else
1010
29.2k
        start_bitmap[end / 8] |= (1u << (end & 7));
1011
1012
56.4k
      range_start = ~(uint32_t)0;
1013
56.4k
      }
1014
25.2k
    else
1015
25.2k
      range_start = char_list_add + (range_end >> XCL_CHAR_SHIFT);
1016
1017
81.7k
    item_count--;
1018
81.7k
    }
1019
1020
15.8k
  list_ind++;
1021
15.8k
  type >>= XCL_TYPE_BIT_LEN;
1022
1023
15.8k
  if (range_start == ~(uint32_t)0)
1024
9.57k
    {
1025
9.57k
    if ((type & XCL_BEGIN_WITH_RANGE) != 0)
1026
876
      {
1027
      /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_START is not possible. */
1028
876
      if (list_ind == 1) range_start = XCL_CHAR_LIST_HIGH_16_START;
1029
296
      else range_start = XCL_CHAR_LIST_LOW_32_START;
1030
876
      }
1031
9.57k
    }
1032
6.28k
  else if ((type & XCL_BEGIN_WITH_RANGE) == 0)
1033
1.32k
    {
1034
1.32k
    PRIV(ord2utf)(range_start, start_buffer);
1035
1036
    /* In 8 bit mode XCL_CHAR_LIST_LOW_32_END and
1037
    XCL_CHAR_LIST_HIGH_32_END are not possible. */
1038
1.32k
    if (list_ind == 1) range_end = XCL_CHAR_LIST_LOW_16_END;
1039
362
    else range_end = XCL_CHAR_LIST_HIGH_16_END;
1040
1041
1.32k
    PRIV(ord2utf)(range_end, end_buffer);
1042
1.32k
    end = end_buffer[0];
1043
1044
13.1k
    for (start = start_buffer[0]; start <= end; start++)
1045
11.7k
      start_bitmap[start / 8] |= (1u << (start & 7));
1046
1047
1.32k
    range_start = ~(uint32_t)0;
1048
1.32k
    }
1049
1050
  /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_ADD is not possible. */
1051
15.8k
  if (list_ind == 1) char_list_add = XCL_CHAR_LIST_HIGH_16_ADD;
1052
7.94k
  else char_list_add = XCL_CHAR_LIST_LOW_32_ADD;
1053
15.8k
  }
1054
7.91k
}
1055
#endif
1056
1057
1058
1059
/*************************************************
1060
*      Create bitmap of starting code units      *
1061
*************************************************/
1062
1063
/* This function scans a compiled unanchored expression recursively and
1064
attempts to build a bitmap of the set of possible starting code units whose
1065
values are less than 256. In 16-bit and 32-bit mode, values above 255 all cause
1066
the 255 bit to be set. When calling set[_not]_type_bits() in UTF-8 (sic) mode
1067
we pass a value of 16 rather than 32 as the final argument. (See comments in
1068
those functions for the reason.)
1069
1070
The SSB_CONTINUE return is useful for parenthesized groups in patterns such as
1071
(a*)b where the group provides some optional starting code units but scanning
1072
must continue at the outer level to find at least one mandatory code unit. At
1073
the outermost level, this function fails unless the result is SSB_DONE.
1074
1075
We restrict recursion (for nested groups) to 1000 to avoid stack overflow
1076
issues.
1077
1078
Arguments:
1079
  re           points to the compiled regex block
1080
  code         points to an expression
1081
  utf          TRUE if in UTF mode
1082
  ucp          TRUE if in UCP mode
1083
  depthptr     pointer to recurse depth
1084
1085
Returns:       SSB_FAIL     => Failed to find any starting code units
1086
               SSB_DONE     => Found mandatory starting code units
1087
               SSB_CONTINUE => Found optional starting code units
1088
               SSB_UNKNOWN  => Hit an unrecognized opcode
1089
               SSB_TOODEEP  => Recursion is too deep
1090
*/
1091
1092
static int
1093
set_start_bits(pcre2_real_code *re, PCRE2_SPTR code, BOOL utf, BOOL ucp,
1094
  int *depthptr)
1095
216k
{
1096
216k
uint32_t c;
1097
216k
int yield = SSB_DONE;
1098
216k
BOOL done;
1099
1100
216k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1101
216k
int table_limit = utf? 16:32;
1102
#else
1103
int table_limit = 32;
1104
#endif
1105
1106
216k
*depthptr += 1;
1107
216k
if (*depthptr > 1000) return SSB_TOODEEP;
1108
1109
216k
do
1110
305k
  {
1111
305k
  BOOL try_next = TRUE;
1112
305k
  PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
1113
1114
305k
  if (*code == OP_CBRA || *code == OP_SCBRA ||
1115
154k
      *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
1116
1117
728k
  while (try_next)    /* Loop for items in this branch */
1118
612k
    {
1119
612k
    int rc;
1120
612k
    PCRE2_SPTR ncode;
1121
612k
    const uint8_t *classmap = NULL;
1122
612k
#ifdef SUPPORT_WIDE_CHARS
1123
612k
    PCRE2_UCHAR xclassflags;
1124
612k
#endif
1125
1126
612k
    switch(*tcode)
1127
612k
      {
1128
      /* If we reach something we don't understand, it means a new opcode has
1129
      been created that hasn't been added to this function. Hopefully this
1130
      problem will be discovered during testing. */
1131
1132
0
      default:
1133
0
      return SSB_UNKNOWN;
1134
1135
      /* Fail for a valid opcode that implies no starting bits. */
1136
1137
15
      case OP_ACCEPT:
1138
18
      case OP_ASSERT_ACCEPT:
1139
59
      case OP_ALLANY:
1140
519
      case OP_ANY:
1141
524
      case OP_ANYBYTE:
1142
530
      case OP_CIRCM:
1143
537
      case OP_CLOSE:
1144
538
      case OP_COMMIT:
1145
547
      case OP_COMMIT_ARG:
1146
723
      case OP_COND:
1147
725
      case OP_CREF:
1148
725
      case OP_FALSE:
1149
725
      case OP_TRUE:
1150
728
      case OP_DNCREF:
1151
778
      case OP_DNREF:
1152
787
      case OP_DNREFI:
1153
788
      case OP_DNRREF:
1154
859
      case OP_DOLL:
1155
866
      case OP_DOLLM:
1156
866
      case OP_END:
1157
874
      case OP_EOD:
1158
894
      case OP_EODN:
1159
905
      case OP_EXTUNI:
1160
922
      case OP_FAIL:
1161
970
      case OP_MARK:
1162
1.00k
      case OP_NOT:
1163
1.00k
      case OP_NOTEXACT:
1164
1.01k
      case OP_NOTEXACTI:
1165
1.03k
      case OP_NOTI:
1166
1.03k
      case OP_NOTMINPLUS:
1167
1.04k
      case OP_NOTMINPLUSI:
1168
1.04k
      case OP_NOTMINQUERY:
1169
1.05k
      case OP_NOTMINQUERYI:
1170
1.06k
      case OP_NOTMINSTAR:
1171
1.06k
      case OP_NOTMINSTARI:
1172
1.06k
      case OP_NOTMINUPTO:
1173
1.07k
      case OP_NOTMINUPTOI:
1174
1.08k
      case OP_NOTPLUS:
1175
1.08k
      case OP_NOTPLUSI:
1176
1.09k
      case OP_NOTPOSPLUS:
1177
1.10k
      case OP_NOTPOSPLUSI:
1178
1.11k
      case OP_NOTPOSQUERY:
1179
1.12k
      case OP_NOTPOSQUERYI:
1180
1.13k
      case OP_NOTPOSSTAR:
1181
1.13k
      case OP_NOTPOSSTARI:
1182
1.14k
      case OP_NOTPOSUPTO:
1183
1.14k
      case OP_NOTPOSUPTOI:
1184
1.35k
      case OP_NOTPROP:
1185
1.37k
      case OP_NOTQUERY:
1186
1.38k
      case OP_NOTQUERYI:
1187
1.39k
      case OP_NOTSTAR:
1188
1.39k
      case OP_NOTSTARI:
1189
1.40k
      case OP_NOTUPTO:
1190
1.41k
      case OP_NOTUPTOI:
1191
1.51k
      case OP_NOT_HSPACE:
1192
1.54k
      case OP_NOT_VSPACE:
1193
1.54k
      case OP_PRUNE:
1194
1.56k
      case OP_PRUNE_ARG:
1195
1.79k
      case OP_RECURSE:
1196
1.91k
      case OP_REF:
1197
1.92k
      case OP_REFI:
1198
1.94k
      case OP_REVERSE:
1199
1.95k
      case OP_VREVERSE:
1200
1.95k
      case OP_RREF:
1201
1.98k
      case OP_SCOND:
1202
1.98k
      case OP_SET_SOM:
1203
1.98k
      case OP_SKIP:
1204
2.00k
      case OP_SKIP_ARG:
1205
2.02k
      case OP_SOD:
1206
2.02k
      case OP_SOM:
1207
2.03k
      case OP_THEN:
1208
2.04k
      case OP_THEN_ARG:
1209
2.04k
      return SSB_FAIL;
1210
1211
      /* OP_CIRC happens only at the start of an anchored branch (multiline ^
1212
      uses OP_CIRCM). Skip over it. */
1213
1214
2.66k
      case OP_CIRC:
1215
2.66k
      tcode += PRIV(OP_lengths)[OP_CIRC];
1216
2.66k
      break;
1217
1218
      /* A "real" property test implies no starting bits, but the fake property
1219
      PT_CLIST identifies a list of characters. These lists are short, as they
1220
      are used for characters with more than one "other case", so there is no
1221
      point in recognizing them for OP_NOTPROP. */
1222
1223
3.36k
      case OP_PROP:
1224
3.36k
      if (tcode[1] != PT_CLIST) return SSB_FAIL;
1225
3.18k
        {
1226
3.18k
        const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
1227
12.7k
        while ((c = *p++) < NOTACHAR)
1228
9.56k
          {
1229
9.56k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1230
9.56k
          if (utf)
1231
6.93k
            {
1232
6.93k
            PCRE2_UCHAR buff[6];
1233
6.93k
            (void)PRIV(ord2utf)(c, buff);
1234
6.93k
            c = buff[0];
1235
6.93k
            }
1236
9.56k
#endif
1237
9.56k
          if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
1238
9.56k
          }
1239
3.18k
        }
1240
3.18k
      try_next = FALSE;
1241
3.18k
      break;
1242
1243
      /* We can ignore word boundary tests. */
1244
1245
3.25k
      case OP_WORD_BOUNDARY:
1246
4.35k
      case OP_NOT_WORD_BOUNDARY:
1247
5.15k
      case OP_UCP_WORD_BOUNDARY:
1248
5.69k
      case OP_NOT_UCP_WORD_BOUNDARY:
1249
5.69k
      tcode++;
1250
5.69k
      break;
1251
1252
      /* For a positive lookahead assertion, inspect what immediately follows,
1253
      ignoring intermediate assertions and callouts. If the next item is one
1254
      that sets a mandatory character, skip this assertion. Otherwise, treat it
1255
      the same as other bracket groups. */
1256
1257
9.73k
      case OP_ASSERT:
1258
21.7k
      case OP_ASSERT_NA:
1259
21.7k
      ncode = tcode + GET(tcode, 1);
1260
27.4k
      while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1261
21.7k
      ncode += 1 + LINK_SIZE;
1262
1263
      /* Skip irrelevant items */
1264
1265
4.54M
      for (done = FALSE; !done;)
1266
4.52M
        {
1267
4.52M
        switch (*ncode)
1268
4.52M
          {
1269
4.36M
          case OP_ASSERT:
1270
4.36M
          case OP_ASSERT_NOT:
1271
4.36M
          case OP_ASSERTBACK:
1272
4.36M
          case OP_ASSERTBACK_NOT:
1273
4.49M
          case OP_ASSERT_NA:
1274
4.49M
          case OP_ASSERTBACK_NA:
1275
4.49M
          case OP_ASSERT_SCS:
1276
4.49M
          ncode += GET(ncode, 1);
1277
4.63M
          while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1278
4.49M
          ncode += 1 + LINK_SIZE;
1279
4.49M
          break;
1280
1281
778
          case OP_WORD_BOUNDARY:
1282
1.00k
          case OP_NOT_WORD_BOUNDARY:
1283
1.32k
          case OP_UCP_WORD_BOUNDARY:
1284
1.55k
          case OP_NOT_UCP_WORD_BOUNDARY:
1285
1.55k
          ncode++;
1286
1.55k
          break;
1287
1288
290
          case OP_CALLOUT:
1289
290
          ncode += PRIV(OP_lengths)[OP_CALLOUT];
1290
290
          break;
1291
1292
1.10k
          case OP_CALLOUT_STR:
1293
1.10k
          ncode += GET(ncode, 1 + 2*LINK_SIZE);
1294
1.10k
          break;
1295
1296
21.7k
          default:
1297
21.7k
          done = TRUE;
1298
21.7k
          break;
1299
4.52M
          }
1300
4.52M
        }
1301
1302
      /* Now check the next significant item. */
1303
1304
21.7k
      switch(*ncode)
1305
21.7k
        {
1306
7.66k
        default:
1307
7.66k
        break;
1308
1309
7.66k
        case OP_PROP:
1310
1.14k
        if (ncode[1] != PT_CLIST) break;
1311
552
        PCRE2_FALLTHROUGH /* Fall through */
1312
2.29k
        case OP_ANYNL:
1313
4.09k
        case OP_CHAR:
1314
4.84k
        case OP_CHARI:
1315
5.43k
        case OP_EXACT:
1316
6.03k
        case OP_EXACTI:
1317
7.07k
        case OP_HSPACE:
1318
7.59k
        case OP_MINPLUS:
1319
8.16k
        case OP_MINPLUSI:
1320
8.61k
        case OP_PLUS:
1321
9.23k
        case OP_PLUSI:
1322
9.83k
        case OP_POSPLUS:
1323
10.6k
        case OP_POSPLUSI:
1324
11.2k
        case OP_VSPACE:
1325
        /* Note that these types will only be present in non-UCP mode. */
1326
11.5k
        case OP_DIGIT:
1327
12.3k
        case OP_NOT_DIGIT:
1328
12.5k
        case OP_WORDCHAR:
1329
12.8k
        case OP_NOT_WORDCHAR:
1330
13.2k
        case OP_WHITESPACE:
1331
13.4k
        case OP_NOT_WHITESPACE:
1332
13.4k
        tcode = ncode;
1333
13.4k
        continue;   /* With the following significant opcode */
1334
21.7k
        }
1335
8.25k
      PCRE2_FALLTHROUGH /* Fall through */
1336
8.25k
1337
8.25k
      /* For a group bracket or a positive assertion without an immediately
1338
8.25k
      following mandatory setting, recurse to set bits from within the
1339
8.25k
      subpattern. If it can't find anything, we have to give up. If it finds
1340
8.25k
      some mandatory character(s), we are done for this branch. Otherwise,
1341
8.25k
      carry on scanning after the subpattern. */
1342
8.25k
1343
28.2k
      case OP_BRA:
1344
28.6k
      case OP_SBRA:
1345
175k
      case OP_CBRA:
1346
176k
      case OP_SCBRA:
1347
177k
      case OP_BRAPOS:
1348
177k
      case OP_SBRAPOS:
1349
178k
      case OP_CBRAPOS:
1350
179k
      case OP_SCBRAPOS:
1351
182k
      case OP_ONCE:
1352
183k
      case OP_SCRIPT_RUN:
1353
183k
      rc = set_start_bits(re, tcode, utf, ucp, depthptr);
1354
183k
      if (rc == SSB_DONE)
1355
7.47k
        {
1356
7.47k
        try_next = FALSE;
1357
7.47k
        }
1358
176k
      else if (rc == SSB_CONTINUE)
1359
173k
        {
1360
253k
        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1361
173k
        tcode += 1 + LINK_SIZE;
1362
173k
        }
1363
3.27k
      else return rc;   /* FAIL, UNKNOWN, or TOODEEP */
1364
180k
      break;
1365
1366
      /* If we hit ALT or KET, it means we haven't found anything mandatory in
1367
      this branch, though we might have found something optional. For ALT, we
1368
      continue with the next alternative, but we have to arrange that the final
1369
      result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
1370
      return SSB_CONTINUE: if this is the top level, that indicates failure,
1371
      but after a nested subpattern, it causes scanning to continue. */
1372
1373
180k
      case OP_ALT:
1374
29.0k
      yield = SSB_CONTINUE;
1375
29.0k
      try_next = FALSE;
1376
29.0k
      break;
1377
1378
174k
      case OP_KET:
1379
176k
      case OP_KETRMAX:
1380
176k
      case OP_KETRMIN:
1381
178k
      case OP_KETRPOS:
1382
178k
      return SSB_CONTINUE;
1383
1384
      /* Skip over callout */
1385
1386
812
      case OP_CALLOUT:
1387
812
      tcode += PRIV(OP_lengths)[OP_CALLOUT];
1388
812
      break;
1389
1390
2.48k
      case OP_CALLOUT_STR:
1391
2.48k
      tcode += GET(tcode, 1 + 2*LINK_SIZE);
1392
2.48k
      break;
1393
1394
      /* Skip over lookbehind, negative lookahead, and scan substring
1395
      assertions */
1396
1397
9.79k
      case OP_ASSERT_NOT:
1398
11.6k
      case OP_ASSERTBACK:
1399
13.5k
      case OP_ASSERTBACK_NOT:
1400
15.2k
      case OP_ASSERTBACK_NA:
1401
15.5k
      case OP_ASSERT_SCS:
1402
16.8k
      do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1403
15.5k
      tcode += 1 + LINK_SIZE;
1404
15.5k
      break;
1405
1406
      /* BRAZERO does the bracket, but carries on. */
1407
1408
22.0k
      case OP_BRAZERO:
1409
23.0k
      case OP_BRAMINZERO:
1410
23.5k
      case OP_BRAPOSZERO:
1411
23.5k
      rc = set_start_bits(re, ++tcode, utf, ucp, depthptr);
1412
23.5k
      if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc;
1413
19.5k
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1414
18.7k
      tcode += 1 + LINK_SIZE;
1415
18.7k
      break;
1416
1417
      /* SKIPZERO skips the bracket. */
1418
1419
1.41k
      case OP_SKIPZERO:
1420
1.41k
      tcode++;
1421
1.69k
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1422
1.41k
      tcode += 1 + LINK_SIZE;
1423
1.41k
      break;
1424
1425
      /* Single-char * or ? sets the bit and tries the next item */
1426
1427
6.35k
      case OP_STAR:
1428
6.79k
      case OP_MINSTAR:
1429
9.22k
      case OP_POSSTAR:
1430
20.7k
      case OP_QUERY:
1431
21.6k
      case OP_MINQUERY:
1432
24.1k
      case OP_POSQUERY:
1433
24.1k
      tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1434
24.1k
      break;
1435
1436
2.21k
      case OP_STARI:
1437
2.78k
      case OP_MINSTARI:
1438
3.11k
      case OP_POSSTARI:
1439
4.56k
      case OP_QUERYI:
1440
5.27k
      case OP_MINQUERYI:
1441
6.22k
      case OP_POSQUERYI:
1442
6.22k
      tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1443
6.22k
      break;
1444
1445
      /* Single-char upto sets the bit and tries the next */
1446
1447
728
      case OP_UPTO:
1448
1.05k
      case OP_MINUPTO:
1449
1.31k
      case OP_POSUPTO:
1450
1.31k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp);
1451
1.31k
      break;
1452
1453
212
      case OP_UPTOI:
1454
816
      case OP_MINUPTOI:
1455
1.46k
      case OP_POSUPTOI:
1456
1.46k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp);
1457
1.46k
      break;
1458
1459
      /* At least one single char sets the bit and stops */
1460
1461
1.28k
      case OP_EXACT:
1462
1.28k
      tcode += IMM2_SIZE;
1463
1.28k
      PCRE2_FALLTHROUGH /* Fall through */
1464
23.2k
      case OP_CHAR:
1465
24.7k
      case OP_PLUS:
1466
26.1k
      case OP_MINPLUS:
1467
27.3k
      case OP_POSPLUS:
1468
27.3k
      (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1469
27.3k
      try_next = FALSE;
1470
27.3k
      break;
1471
1472
1.16k
      case OP_EXACTI:
1473
1.16k
      tcode += IMM2_SIZE;
1474
1.16k
      PCRE2_FALLTHROUGH /* Fall through */
1475
7.18k
      case OP_CHARI:
1476
8.20k
      case OP_PLUSI:
1477
9.36k
      case OP_MINPLUSI:
1478
10.8k
      case OP_POSPLUSI:
1479
10.8k
      (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1480
10.8k
      try_next = FALSE;
1481
10.8k
      break;
1482
1483
      /* Special spacing and line-terminating items. These recognize specific
1484
      lists of characters. The difference between VSPACE and ANYNL is that the
1485
      latter can match the two-character CRLF sequence, but that is not
1486
      relevant for finding the first character, so their code here is
1487
      identical. */
1488
1489
2.65k
      case OP_HSPACE:
1490
2.65k
      SET_BIT(CHAR_HT);
1491
2.65k
      SET_BIT(CHAR_SPACE);
1492
1493
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1494
      the bits for NBSP and for code units >= 255, independently of UTF. */
1495
1496
#if PCRE2_CODE_UNIT_WIDTH != 8
1497
      SET_BIT(CHAR_NBSP);
1498
      SET_BIT(0xFF);
1499
#else
1500
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1501
      units of horizontal space characters. */
1502
1503
2.65k
#ifdef SUPPORT_UNICODE
1504
2.65k
      if (utf)
1505
956
        {
1506
956
        SET_BIT(0xC2);  /* For U+00A0 */
1507
956
        SET_BIT(0xE1);  /* For U+1680, U+180E */
1508
956
        SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1509
956
        SET_BIT(0xE3);  /* For U+3000 */
1510
956
        }
1511
1.69k
      else
1512
1.69k
#endif
1513
      /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1514
1.69k
        {
1515
1.69k
        SET_BIT(CHAR_NBSP);
1516
1.69k
        }
1517
2.65k
#endif  /* 8-bit support */
1518
1519
2.65k
      try_next = FALSE;
1520
2.65k
      break;
1521
1522
2.14k
      case OP_ANYNL:
1523
4.87k
      case OP_VSPACE:
1524
4.87k
      SET_BIT(CHAR_LF);
1525
4.87k
      SET_BIT(CHAR_VT);
1526
4.87k
      SET_BIT(CHAR_FF);
1527
4.87k
      SET_BIT(CHAR_CR);
1528
1529
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1530
      the bits for NEL and for code units >= 255, independently of UTF. */
1531
1532
#if PCRE2_CODE_UNIT_WIDTH != 8
1533
      SET_BIT(CHAR_NEL);
1534
      SET_BIT(0xFF);
1535
#else
1536
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1537
      units of vertical space characters. */
1538
1539
4.87k
#ifdef SUPPORT_UNICODE
1540
4.87k
      if (utf)
1541
1.01k
        {
1542
1.01k
        SET_BIT(0xC2);  /* For U+0085 (NEL) */
1543
1.01k
        SET_BIT(0xE2);  /* For U+2028, U+2029 */
1544
1.01k
        }
1545
3.86k
      else
1546
3.86k
#endif
1547
      /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1548
3.86k
        {
1549
3.86k
        SET_BIT(CHAR_NEL);
1550
3.86k
        }
1551
4.87k
#endif  /* 8-bit support */
1552
1553
4.87k
      try_next = FALSE;
1554
4.87k
      break;
1555
1556
      /* Single character types set the bits and stop. Note that if PCRE2_UCP
1557
      is set, we do not see these opcodes because \d etc are converted to
1558
      properties. Therefore, these apply in the case when only characters less
1559
      than 256 are recognized to match the types. */
1560
1561
1.50k
      case OP_NOT_DIGIT:
1562
1.50k
      set_nottype_bits(re, cbit_digit, table_limit);
1563
1.50k
      try_next = FALSE;
1564
1.50k
      break;
1565
1566
4.34k
      case OP_DIGIT:
1567
4.34k
      set_type_bits(re, cbit_digit, table_limit);
1568
4.34k
      try_next = FALSE;
1569
4.34k
      break;
1570
1571
1.40k
      case OP_NOT_WHITESPACE:
1572
1.40k
      set_nottype_bits(re, cbit_space, table_limit);
1573
1.40k
      try_next = FALSE;
1574
1.40k
      break;
1575
1576
3.27k
      case OP_WHITESPACE:
1577
3.27k
      set_type_bits(re, cbit_space, table_limit);
1578
3.27k
      try_next = FALSE;
1579
3.27k
      break;
1580
1581
1.38k
      case OP_NOT_WORDCHAR:
1582
1.38k
      set_nottype_bits(re, cbit_word, table_limit);
1583
1.38k
      try_next = FALSE;
1584
1.38k
      break;
1585
1586
5.06k
      case OP_WORDCHAR:
1587
5.06k
      set_type_bits(re, cbit_word, table_limit);
1588
5.06k
      try_next = FALSE;
1589
5.06k
      break;
1590
1591
      /* One or more character type fudges the pointer and restarts, knowing
1592
      it will hit a single character type and stop there. */
1593
1594
1.69k
      case OP_TYPEPLUS:
1595
2.02k
      case OP_TYPEMINPLUS:
1596
3.12k
      case OP_TYPEPOSPLUS:
1597
3.12k
      tcode++;
1598
3.12k
      break;
1599
1600
395
      case OP_TYPEEXACT:
1601
395
      tcode += 1 + IMM2_SIZE;
1602
395
      break;
1603
1604
      /* Zero or more repeats of character types set the bits and then
1605
      try again. */
1606
1607
626
      case OP_TYPEUPTO:
1608
983
      case OP_TYPEMINUPTO:
1609
1.31k
      case OP_TYPEPOSUPTO:
1610
1.31k
      tcode += IMM2_SIZE;
1611
1.31k
      PCRE2_FALLTHROUGH /* Fall through */
1612
1613
6.66k
      case OP_TYPESTAR:
1614
7.20k
      case OP_TYPEMINSTAR:
1615
8.66k
      case OP_TYPEPOSSTAR:
1616
23.9k
      case OP_TYPEQUERY:
1617
24.3k
      case OP_TYPEMINQUERY:
1618
25.0k
      case OP_TYPEPOSQUERY:
1619
25.0k
      switch(tcode[1])
1620
25.0k
        {
1621
292
        default:
1622
413
        case OP_ANY:
1623
446
        case OP_ALLANY:
1624
446
        return SSB_FAIL;
1625
1626
2.56k
        case OP_HSPACE:
1627
2.56k
        SET_BIT(CHAR_HT);
1628
2.56k
        SET_BIT(CHAR_SPACE);
1629
1630
        /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1631
        the bits for NBSP and for code units >= 255, independently of UTF. */
1632
1633
#if PCRE2_CODE_UNIT_WIDTH != 8
1634
        SET_BIT(CHAR_NBSP);
1635
        SET_BIT(0xFF);
1636
#else
1637
        /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1638
        units of horizontal space characters. */
1639
1640
2.56k
#ifdef SUPPORT_UNICODE
1641
2.56k
        if (utf)
1642
1.25k
          {
1643
1.25k
          SET_BIT(0xC2);  /* For U+00A0 */
1644
1.25k
          SET_BIT(0xE1);  /* For U+1680, U+180E */
1645
1.25k
          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1646
1.25k
          SET_BIT(0xE3);  /* For U+3000 */
1647
1.25k
          }
1648
1.30k
        else
1649
1.30k
#endif
1650
        /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1651
1.30k
          {
1652
1.30k
          SET_BIT(CHAR_NBSP);
1653
1.30k
          }
1654
2.56k
#endif  /* 8-bit support */
1655
2.56k
        break;
1656
1657
1.46k
        case OP_ANYNL:
1658
3.13k
        case OP_VSPACE:
1659
3.13k
        SET_BIT(CHAR_LF);
1660
3.13k
        SET_BIT(CHAR_VT);
1661
3.13k
        SET_BIT(CHAR_FF);
1662
3.13k
        SET_BIT(CHAR_CR);
1663
1664
        /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1665
        the bits for NEL and for code units >= 255, independently of UTF. */
1666
1667
#if PCRE2_CODE_UNIT_WIDTH != 8
1668
        SET_BIT(CHAR_NEL);
1669
        SET_BIT(0xFF);
1670
#else
1671
        /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1672
        units of vertical space characters. */
1673
1674
3.13k
#ifdef SUPPORT_UNICODE
1675
3.13k
        if (utf)
1676
392
          {
1677
392
          SET_BIT(0xC2);  /* For U+0085 (NEL) */
1678
392
          SET_BIT(0xE2);  /* For U+2028, U+2029 */
1679
392
          }
1680
2.74k
        else
1681
2.74k
#endif
1682
        /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1683
2.74k
          {
1684
2.74k
          SET_BIT(CHAR_NEL);
1685
2.74k
          }
1686
3.13k
#endif  /* 8-bit support */
1687
3.13k
        break;
1688
1689
2.34k
        case OP_NOT_DIGIT:
1690
2.34k
        set_nottype_bits(re, cbit_digit, table_limit);
1691
2.34k
        break;
1692
1693
5.33k
        case OP_DIGIT:
1694
5.33k
        set_type_bits(re, cbit_digit, table_limit);
1695
5.33k
        break;
1696
1697
2.06k
        case OP_NOT_WHITESPACE:
1698
2.06k
        set_nottype_bits(re, cbit_space, table_limit);
1699
2.06k
        break;
1700
1701
4.46k
        case OP_WHITESPACE:
1702
4.46k
        set_type_bits(re, cbit_space, table_limit);
1703
4.46k
        break;
1704
1705
1.35k
        case OP_NOT_WORDCHAR:
1706
1.35k
        set_nottype_bits(re, cbit_word, table_limit);
1707
1.35k
        break;
1708
1709
3.30k
        case OP_WORDCHAR:
1710
3.30k
        set_type_bits(re, cbit_word, table_limit);
1711
3.30k
        break;
1712
25.0k
        }
1713
1714
24.5k
      tcode += 2;
1715
24.5k
      break;
1716
1717
      /* Set-based ECLASS: treat it the same as a "complex" XCLASS; give up. */
1718
1719
0
#ifdef SUPPORT_WIDE_CHARS
1720
37
      case OP_ECLASS:
1721
37
      return SSB_FAIL;
1722
0
#endif
1723
1724
      /* Extended class: if there are any property checks, or if this is a
1725
      negative XCLASS without a map, give up. If there are no property checks,
1726
      there must be wide characters on the XCLASS list, because otherwise an
1727
      XCLASS would not have been created. This means that code points >= 255
1728
      are potential starters. In the UTF-8 case we can scan them and set bits
1729
      for the relevant leading bytes. */
1730
1731
0
#ifdef SUPPORT_WIDE_CHARS
1732
12.0k
      case OP_XCLASS:
1733
12.0k
      xclassflags = tcode[1 + LINK_SIZE];
1734
12.0k
      if ((xclassflags & XCL_HASPROP) != 0 ||
1735
11.6k
          (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT)
1736
434
        return SSB_FAIL;
1737
1738
      /* We have a positive XCLASS or a negative one without a map. Set up the
1739
      map pointer if there is one, and fall through. */
1740
1741
11.6k
      classmap = ((xclassflags & XCL_MAP) == 0)? NULL :
1742
11.6k
        (const uint8_t *)(tcode + 1 + LINK_SIZE + 1);
1743
1744
      /* In UTF-8 mode, scan the character list and set bits for leading bytes,
1745
      then jump to handle the map. */
1746
1747
11.6k
#if PCRE2_CODE_UNIT_WIDTH == 8
1748
11.6k
      if (utf && (xclassflags & XCL_NOT) == 0)
1749
10.9k
        {
1750
10.9k
        PCRE2_UCHAR b, e;
1751
10.9k
        PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32);
1752
10.9k
        tcode += GET(tcode, 1);
1753
1754
10.9k
        if (*p >= XCL_LIST)
1755
7.91k
          {
1756
7.91k
          study_char_list(p, re->start_bitmap,
1757
7.91k
            ((const uint8_t *)re + re->code_start));
1758
7.91k
          goto HANDLE_CLASSMAP;
1759
7.91k
          }
1760
1761
6.74k
        for (;;) switch (*p++)
1762
6.74k
          {
1763
1.43k
          case XCL_SINGLE:
1764
1.43k
          b = *p++;
1765
3.30k
          while ((*p & 0xc0) == 0x80) p++;
1766
1.43k
          re->start_bitmap[b/8] |= (1u << (b&7));
1767
1.43k
          break;
1768
1769
2.29k
          case XCL_RANGE:
1770
2.29k
          b = *p++;
1771
6.50k
          while ((*p & 0xc0) == 0x80) p++;
1772
2.29k
          e = *p++;
1773
7.10k
          while ((*p & 0xc0) == 0x80) p++;
1774
19.2k
          for (; b <= e; b++)
1775
16.9k
            re->start_bitmap[b/8] |= (1u << (b&7));
1776
2.29k
          break;
1777
1778
3.01k
          case XCL_END:
1779
3.01k
          goto HANDLE_CLASSMAP;
1780
1781
          /* LCOV_EXCL_START */
1782
0
          default:
1783
0
          PCRE2_DEBUG_UNREACHABLE();
1784
0
          return SSB_UNKNOWN;   /* Internal error, should not occur */
1785
          /* LCOV_EXCL_STOP */
1786
6.74k
          }
1787
3.01k
        }
1788
694
#endif  /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
1789
694
#endif  /* SUPPORT_WIDE_CHARS */
1790
1791
      /* It seems that the fall through comment must be outside the #ifdef if
1792
      it is to avoid the gcc compiler warning. */
1793
1794
694
      PCRE2_FALLTHROUGH /* Fall through */
1795
694
1796
694
      /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
1797
694
      in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
1798
694
      because it starts a character with a value > 255. In 8-bit non-UTF mode,
1799
694
      there is no difference between CLASS and NCLASS. In all other wide
1800
694
      character modes, set the 0xFF bit to indicate code units >= 255. */
1801
694
1802
3.79k
      case OP_NCLASS:
1803
3.79k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1804
3.79k
      if (utf)
1805
2.09k
        {
1806
2.09k
        re->start_bitmap[24] |= 0xf0;            /* Bits for 0xc4 - 0xc8 */
1807
2.09k
        memset(re->start_bitmap+25, 0xff, 7);    /* Bits for 0xc9 - 0xff */
1808
2.09k
        }
1809
3.79k
      PCRE2_FALLTHROUGH /* Fall through */
1810
#elif PCRE2_CODE_UNIT_WIDTH != 8
1811
      SET_BIT(0xFF);                             /* For characters >= 255 */
1812
      PCRE2_FALLTHROUGH /* Fall through */
1813
#endif
1814
1815
      /* Enter here for a positive non-XCLASS. If we have fallen through from
1816
      an XCLASS, classmap will already be set; just advance the code pointer.
1817
      Otherwise, set up classmap for a non-XCLASS and advance past it. */
1818
1819
13.9k
      case OP_CLASS:
1820
13.9k
      if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
1821
13.2k
        {
1822
13.2k
        classmap = (const uint8_t *)(++tcode);
1823
13.2k
        tcode += 32 / sizeof(PCRE2_UCHAR);
1824
13.2k
        }
1825
1826
      /* When wide characters are supported, classmap may be NULL. In UTF-8
1827
      (sic) mode, the bits in a class bit map correspond to character values,
1828
      not to byte values. However, the bit map we are constructing is for byte
1829
      values. So we have to do a conversion for characters whose code point is
1830
      greater than 127. In fact, there are only two possible starting bytes for
1831
      characters in the range 128 - 255. */
1832
1833
13.9k
#if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8
1834
24.8k
      HANDLE_CLASSMAP:
1835
24.8k
#endif
1836
24.8k
      if (classmap != NULL)
1837
24.4k
        {
1838
24.4k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1839
24.4k
        if (utf)
1840
14.5k
          {
1841
247k
          for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
1842
925k
          for (c = 128; c < 256; c++)
1843
910k
            {
1844
910k
            if ((classmap[c/8] & (1u << (c&7))) != 0)
1845
17.7k
              {
1846
17.7k
              int d = (c >> 6) | 0xc0;                 /* Set bit for this starter */
1847
17.7k
              re->start_bitmap[d/8] |= (1u << (d&7));  /* and then skip on to the */
1848
17.7k
              c = (c & 0xc0) + 0x40 - 1;               /* next relevant character. */
1849
17.7k
              }
1850
910k
            }
1851
14.5k
          }
1852
9.87k
        else
1853
9.87k
#endif
1854
        /* In all modes except UTF-8, the two bit maps are compatible. */
1855
1856
9.87k
          {
1857
325k
          for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
1858
9.87k
          }
1859
24.4k
        }
1860
1861
      /* Act on what follows the class. For a zero minimum repeat, continue;
1862
      otherwise stop processing. */
1863
1864
24.8k
      switch (*tcode)
1865
24.8k
        {
1866
3.84k
        case OP_CRSTAR:
1867
4.76k
        case OP_CRMINSTAR:
1868
8.13k
        case OP_CRQUERY:
1869
9.61k
        case OP_CRMINQUERY:
1870
10.0k
        case OP_CRPOSSTAR:
1871
10.6k
        case OP_CRPOSQUERY:
1872
10.6k
        tcode++;
1873
10.6k
        break;
1874
1875
667
        case OP_CRRANGE:
1876
1.17k
        case OP_CRMINRANGE:
1877
2.16k
        case OP_CRPOSRANGE:
1878
2.16k
        if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1879
1.67k
          else try_next = FALSE;
1880
2.16k
        break;
1881
1882
12.0k
        default:
1883
12.0k
        try_next = FALSE;
1884
12.0k
        break;
1885
24.8k
        }
1886
24.8k
      break; /* End of class handling case */
1887
612k
      }      /* End of switch for opcodes */
1888
612k
    }        /* End of try_next loop */
1889
1890
116k
  code += GET(code, 1);   /* Advance to next branch */
1891
116k
  }
1892
216k
while (*code == OP_ALT);
1893
1894
27.1k
return yield;
1895
216k
}
1896
1897
1898
1899
/*************************************************
1900
*          Study a compiled expression           *
1901
*************************************************/
1902
1903
/* This function is handed a compiled expression that it must study to produce
1904
information that will speed up the matching.
1905
1906
Argument:
1907
  re       points to the compiled expression
1908
1909
Returns:   0 normally; non-zero should never normally occur
1910
           1 unknown opcode in set_start_bits
1911
           2 missing capturing bracket
1912
           3 unknown opcode in find_minlength
1913
*/
1914
1915
int
1916
PRIV(study)(pcre2_real_code *re)
1917
25.6k
{
1918
25.6k
int count = 0;
1919
25.6k
PCRE2_UCHAR *code;
1920
25.6k
BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
1921
25.6k
BOOL ucp = (re->overall_options & PCRE2_UCP) != 0;
1922
1923
/* Find start of compiled code */
1924
1925
25.6k
code = (PCRE2_UCHAR *)((uint8_t *)re + re->code_start);
1926
1927
/* For a pattern that has a first code unit, or a multiline pattern that
1928
matches only at "line start", there is no point in seeking a list of starting
1929
code units. */
1930
1931
25.6k
if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
1932
9.27k
  {
1933
9.27k
  int depth = 0;
1934
9.27k
  int rc = set_start_bits(re, code, utf, ucp, &depth);
1935
  /* LCOV_EXCL_START */
1936
9.27k
  if (rc == SSB_UNKNOWN)
1937
0
    {
1938
0
    PCRE2_DEBUG_UNREACHABLE();
1939
0
    return 1;
1940
0
    }
1941
  /* LCOV_EXCL_STOP */
1942
1943
  /* If a list of starting code units was set up, scan the list to see if only
1944
  one or two were listed. Having only one listed is rare because usually a
1945
  single starting code unit will have been recognized and PCRE2_FIRSTSET set.
1946
  If two are listed, see if they are caseless versions of the same character;
1947
  if so we can replace the list with a caseless first code unit. This gives
1948
  better performance and is plausibly worth doing for patterns such as [Ww]ord
1949
  or (word|WORD). */
1950
1951
9.27k
  if (rc == SSB_DONE)
1952
3.44k
    {
1953
3.44k
    int i;
1954
3.44k
    int a = -1;
1955
3.44k
    int b = -1;
1956
3.44k
    uint8_t *p = re->start_bitmap;
1957
3.44k
    uint32_t flags = PCRE2_FIRSTMAPSET;
1958
1959
37.6k
    for (i = 0; i < 256; p++, i += 8)
1960
37.4k
      {
1961
37.4k
      uint8_t x = *p;
1962
37.4k
      if (x != 0)
1963
5.05k
        {
1964
5.05k
        int c;
1965
5.05k
        uint8_t y = x & (~x + 1);   /* Least significant bit */
1966
5.05k
        if (y != x) goto DONE;      /* More than one bit set */
1967
1968
        /* In the 16-bit and 32-bit libraries, the bit for 0xff means "0xff and
1969
        all wide characters", so we cannot use it here. */
1970
1971
#if PCRE2_CODE_UNIT_WIDTH != 8
1972
        if (i == 248 && x == 0x80) goto DONE;
1973
#endif
1974
1975
        /* Compute the character value */
1976
1977
3.21k
        c = i;
1978
3.21k
        switch (x)
1979
3.21k
          {
1980
524
          case 1:   break;
1981
555
          case 2:   c += 1; break;  case 4:  c += 2; break;
1982
388
          case 8:   c += 3; break;  case 16: c += 4; break;
1983
424
          case 32:  c += 5; break;  case 64: c += 6; break;
1984
484
          case 128: c += 7; break;
1985
3.21k
          }
1986
1987
        /* c contains the code unit value, in the range 0-255. In 8-bit UTF
1988
        mode, only values < 128 can be used. In all the other cases, c is a
1989
        character value. */
1990
1991
3.21k
#if PCRE2_CODE_UNIT_WIDTH == 8
1992
3.21k
        if (utf && c > 127) goto DONE;
1993
3.08k
#endif
1994
3.08k
        if (a < 0) a = c;   /* First one found, save in a */
1995
1.31k
        else if (b < 0)     /* Second one found */
1996
1.29k
          {
1997
1.29k
          int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c);
1998
1999
1.29k
#ifdef SUPPORT_UNICODE
2000
1.29k
          if (utf || ucp)
2001
467
            {
2002
467
            if (UCD_CASESET(c) != 0) goto DONE;     /* Multiple case set */
2003
426
            if (c > 127) d = UCD_OTHERCASE(c);
2004
426
            }
2005
1.25k
#endif  /* SUPPORT_UNICODE */
2006
2007
1.25k
          if (d != a) goto DONE;   /* Not the other case of a */
2008
48
          b = c;                   /* Save second in b */
2009
2010
#ifdef EBCDIC
2011
          /* To match ASCII (which puts the uppercase one in a), swap a & b
2012
          if needed. This doesn't really matter, but neatens the tests. */
2013
          if (TABLE_GET((unsigned int)a, re->tables + lcc_offset, a) == a)
2014
            {
2015
            b = a;
2016
            a = c;
2017
            }
2018
#endif
2019
48
          }
2020
22
        else goto DONE;   /* More than two characters found */
2021
3.08k
        }
2022
37.4k
      }
2023
2024
    /* Replace the start code unit bits with a first code unit. If it is the
2025
    same as a required later code unit, then clear the required later code
2026
    unit. This is because a search for a required code unit starts after an
2027
    explicit first code unit, but at a code unit found from the bitmap.
2028
    Patterns such as /a*a/ don't work if both the start unit and required
2029
    unit are the same. */
2030
2031
215
    if (a >= 0) {
2032
192
      if ((re->flags & PCRE2_LASTSET) && (re->last_codeunit == (uint32_t)a || (b >= 0 && re->last_codeunit == (uint32_t)b))) {
2033
53
        re->flags &= ~(PCRE2_LASTSET | PCRE2_LASTCASELESS);
2034
53
        re->last_codeunit = 0;
2035
53
      }
2036
192
      re->first_codeunit = a;
2037
192
      flags = PCRE2_FIRSTSET;
2038
192
      if (b >= 0) flags |= PCRE2_FIRSTCASELESS;
2039
192
    }
2040
2041
3.44k
    DONE:
2042
3.44k
    re->flags |= flags;
2043
3.44k
    }
2044
9.27k
  }
2045
2046
/* Find the minimum length of subject string. If the pattern can match an empty
2047
string, the minimum length is already known. If the pattern contains (*ACCEPT)
2048
all bets are off, and we don't even try to find a minimum length. If there are
2049
more back references than the size of the vector we are going to cache them in,
2050
do nothing. A pattern that complicated will probably take a long time to
2051
analyze and may in any case turn out to be too complicated. Note that back
2052
reference minima are held as 16-bit numbers. */
2053
2054
25.6k
if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 &&
2055
21.9k
     re->top_backref <= MAX_CACHE_BACKREF)
2056
21.8k
  {
2057
21.8k
  int min;
2058
21.8k
  int backref_cache[MAX_CACHE_BACKREF+1];
2059
21.8k
  backref_cache[0] = 0;    /* Highest one that is set */
2060
21.8k
  min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
2061
21.8k
  switch(min)
2062
21.8k
    {
2063
76
    case -1:  /* \C in UTF mode or over-complex regex */
2064
76
    break;    /* Leave minlength unchanged (will be zero) */
2065
2066
    /* LCOV_EXCL_START */
2067
0
    case -2:
2068
0
    PCRE2_DEBUG_UNREACHABLE();
2069
0
    return 2; /* missing capturing bracket */
2070
    /* LCOV_EXCL_STOP */
2071
2072
    /* LCOV_EXCL_START */
2073
0
    case -3:
2074
0
    PCRE2_DEBUG_UNREACHABLE();
2075
0
    return 3; /* unrecognized opcode */
2076
    /* LCOV_EXCL_STOP */
2077
2078
21.8k
    default:
2079
21.8k
    re->minlength = (min > (int)UINT16_MAX)? (int)UINT16_MAX : min;
2080
21.8k
    break;
2081
21.8k
    }
2082
21.8k
  }
2083
2084
25.6k
return 0;
2085
25.6k
}
2086
2087
/* End of pcre2_study.c */