Coverage Report

Created: 2026-07-16 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pcre2/src/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
37.5k
#define MAX_CACHE_BACKREF 128
53
54
/* Set a bit in the starting code unit bit map. */
55
56
375k
#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
421k
{
106
421k
int length = -1;
107
421k
int branchlength = 0;
108
421k
int prev_cap_recno = -1;
109
421k
int prev_cap_d = 0;
110
421k
int prev_recurse_recno = -1;
111
421k
int prev_recurse_d = 0;
112
421k
uint32_t once_fudge = 0;
113
421k
BOOL had_recurse = FALSE;
114
421k
BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
115
421k
PCRE2_SPTR nextbranch = code + GET(code, 1);
116
421k
PCRE2_SPTR cc = code + 1 + LINK_SIZE;
117
421k
recurse_check this_recurse;
118
119
/* If this is a "could be empty" group, its minimum length is 0. */
120
121
421k
if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
122
123
/* Skip over capturing bracket number */
124
125
392k
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
392k
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
392k
for (;;)
136
8.66M
  {
137
8.66M
  int d, min, recno;
138
8.66M
  PCRE2_UCHAR op;
139
8.66M
  PCRE2_SPTR cs, ce;
140
141
8.66M
  if (branchlength >= (int)UINT16_MAX)
142
1.14k
    {
143
1.14k
    branchlength = UINT16_MAX;
144
1.14k
    cc = nextbranch;
145
1.14k
    }
146
147
8.66M
  op = *cc;
148
8.66M
  switch (op)
149
8.66M
    {
150
6.28k
    case OP_COND:
151
6.93k
    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.93k
    cs = cc + GET(cc, 1);
159
6.93k
    if (*cs != OP_ALT)
160
4.72k
      {
161
4.72k
      cc = cs + 1 + LINK_SIZE;
162
4.72k
      break;
163
4.72k
      }
164
2.21k
    goto PROCESS_NON_CAPTURE;
165
166
130k
    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
130k
    if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
173
202
      {
174
202
      once_fudge = 1 + LINK_SIZE;
175
202
      cc += 1 + LINK_SIZE;
176
202
      break;
177
202
      }
178
129k
    PCRE2_FALLTHROUGH /* Fall through */
179
129k
180
152k
    case OP_ONCE:
181
272k
    case OP_SCRIPT_RUN:
182
274k
    case OP_SBRA:
183
277k
    case OP_BRAPOS:
184
279k
    case OP_SBRAPOS:
185
281k
    PROCESS_NON_CAPTURE:
186
281k
    d = find_minlength(re, cc, startcode, utf, recurses, countptr,
187
281k
      backref_cache);
188
281k
    if (d < 0) return d;
189
281k
    branchlength += d;
190
364k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
191
281k
    cc += 1 + LINK_SIZE;
192
281k
    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
348k
    case OP_CBRA:
200
351k
    case OP_SCBRA:
201
353k
    case OP_CBRAPOS:
202
375k
    case OP_SCBRAPOS:
203
375k
    recno = (int)GET2(cc, 1+LINK_SIZE);
204
375k
    if (dupcapused || recno != prev_cap_recno)
205
90.5k
      {
206
90.5k
      prev_cap_recno = recno;
207
90.5k
      prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
208
90.5k
        backref_cache);
209
90.5k
      if (prev_cap_d < 0) return prev_cap_d;
210
90.5k
      }
211
374k
    branchlength += prev_cap_d;
212
446k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
213
374k
    cc += 1 + LINK_SIZE;
214
374k
    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
112k
    case OP_ALT:
231
452k
    case OP_KET:
232
458k
    case OP_KETRMAX:
233
459k
    case OP_KETRMIN:
234
463k
    case OP_KETRPOS:
235
463k
    case OP_END:
236
463k
    if (length < 0 || (!had_recurse && branchlength < length))
237
419k
      length = branchlength;
238
463k
    if (op != OP_ALT || length == 0) return length;
239
71.3k
    nextbranch = cc + GET(cc, 1);
240
71.3k
    cc += 1 + LINK_SIZE;
241
71.3k
    branchlength = 0;
242
71.3k
    had_recurse = FALSE;
243
71.3k
    break;
244
245
    /* Skip over assertive subpatterns */
246
247
172k
    case OP_ASSERT:
248
568k
    case OP_ASSERT_NOT:
249
699k
    case OP_ASSERTBACK:
250
838k
    case OP_ASSERTBACK_NOT:
251
1.89M
    case OP_ASSERT_NA:
252
1.92M
    case OP_ASSERT_SCS:
253
2.01M
    case OP_ASSERTBACK_NA:
254
2.38M
    do cc += GET(cc, 1); while (*cc == OP_ALT);
255
2.01M
    PCRE2_FALLTHROUGH /* Fall through */
256
257
    /* Skip over things that don't match chars */
258
259
2.01M
    case OP_REVERSE:
260
2.01M
    case OP_VREVERSE:
261
2.01M
    case OP_CREF:
262
2.01M
    case OP_DNCREF:
263
2.01M
    case OP_RREF:
264
2.01M
    case OP_DNRREF:
265
2.01M
    case OP_FALSE:
266
2.01M
    case OP_TRUE:
267
2.81M
    case OP_CALLOUT:
268
2.81M
    case OP_SOD:
269
2.81M
    case OP_SOM:
270
2.82M
    case OP_EOD:
271
2.82M
    case OP_EODN:
272
2.83M
    case OP_CIRC:
273
2.83M
    case OP_CIRCM:
274
2.84M
    case OP_DOLL:
275
2.85M
    case OP_DOLLM:
276
2.85M
    case OP_NOT_WORD_BOUNDARY:
277
2.85M
    case OP_WORD_BOUNDARY:
278
2.85M
    case OP_NOT_UCP_WORD_BOUNDARY:
279
2.86M
    case OP_UCP_WORD_BOUNDARY:
280
2.86M
    cc += PRIV(OP_lengths)[*cc];
281
2.86M
    break;
282
283
723
    case OP_CALLOUT_STR:
284
723
    cc += GET(cc, 1 + 2*LINK_SIZE);
285
723
    break;
286
287
    /* Skip over a subpattern that has a {0} or {0,x} quantifier */
288
289
8.25k
    case OP_BRAZERO:
290
10.7k
    case OP_BRAMINZERO:
291
11.9k
    case OP_BRAPOSZERO:
292
12.4k
    case OP_SKIPZERO:
293
12.4k
    cc += PRIV(OP_lengths)[*cc];
294
14.3k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
295
12.4k
    cc += 1 + LINK_SIZE;
296
12.4k
    break;
297
298
    /* Handle literal characters and + repetitions */
299
300
2.67M
    case OP_CHAR:
301
4.00M
    case OP_CHARI:
302
4.01M
    case OP_NOT:
303
4.01M
    case OP_NOTI:
304
4.01M
    case OP_PLUS:
305
4.01M
    case OP_PLUSI:
306
4.02M
    case OP_MINPLUS:
307
4.02M
    case OP_MINPLUSI:
308
4.04M
    case OP_POSPLUS:
309
4.04M
    case OP_POSPLUSI:
310
4.04M
    case OP_NOTPLUS:
311
4.04M
    case OP_NOTPLUSI:
312
4.04M
    case OP_NOTMINPLUS:
313
4.04M
    case OP_NOTMINPLUSI:
314
4.05M
    case OP_NOTPOSPLUS:
315
4.05M
    case OP_NOTPOSPLUSI:
316
4.05M
    branchlength++;
317
4.05M
    cc += 2;
318
4.05M
#ifdef SUPPORT_UNICODE
319
4.05M
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
320
4.05M
#endif
321
4.05M
    break;
322
323
12.8k
    case OP_TYPEPLUS:
324
17.8k
    case OP_TYPEMINPLUS:
325
23.2k
    case OP_TYPEPOSPLUS:
326
23.2k
    branchlength++;
327
23.2k
    cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
328
23.2k
    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.34k
    case OP_EXACT:
334
8.78k
    case OP_EXACTI:
335
10.0k
    case OP_NOTEXACT:
336
12.3k
    case OP_NOTEXACTI:
337
12.3k
    branchlength += GET2(cc,1);
338
12.3k
    cc += 2 + IMM2_SIZE;
339
12.3k
#ifdef SUPPORT_UNICODE
340
12.3k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
341
12.3k
#endif
342
12.3k
    break;
343
344
4.83k
    case OP_TYPEEXACT:
345
4.83k
    branchlength += GET2(cc,1);
346
4.83k
    cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
347
3.31k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
348
4.83k
    break;
349
350
    /* Handle single-char non-literal matchers */
351
352
10.6k
    case OP_PROP:
353
18.9k
    case OP_NOTPROP:
354
18.9k
    cc += 2;
355
18.9k
    PCRE2_FALLTHROUGH /* Fall through */
356
357
24.2k
    case OP_NOT_DIGIT:
358
29.3k
    case OP_DIGIT:
359
36.1k
    case OP_NOT_WHITESPACE:
360
39.3k
    case OP_WHITESPACE:
361
42.5k
    case OP_NOT_WORDCHAR:
362
45.9k
    case OP_WORDCHAR:
363
53.6k
    case OP_ANY:
364
57.1k
    case OP_ALLANY:
365
59.4k
    case OP_EXTUNI:
366
67.4k
    case OP_HSPACE:
367
69.5k
    case OP_NOT_HSPACE:
368
70.7k
    case OP_VSPACE:
369
71.3k
    case OP_NOT_VSPACE:
370
71.3k
    branchlength++;
371
71.3k
    cc++;
372
71.3k
    break;
373
374
    /* "Any newline" might match two characters, but it also might match just
375
    one. */
376
377
5.89k
    case OP_ANYNL:
378
5.89k
    branchlength += 1;
379
5.89k
    cc++;
380
5.89k
    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
0
    case OP_ANYBYTE:
387
0
#ifdef SUPPORT_UNICODE
388
0
    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
15.3k
    case OP_TYPESTAR:
398
21.8k
    case OP_TYPEMINSTAR:
399
32.7k
    case OP_TYPEQUERY:
400
42.9k
    case OP_TYPEMINQUERY:
401
48.3k
    case OP_TYPEPOSSTAR:
402
62.9k
    case OP_TYPEPOSQUERY:
403
62.9k
    if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
404
62.9k
    cc += PRIV(OP_lengths)[op];
405
62.9k
    break;
406
407
3.83k
    case OP_TYPEUPTO:
408
5.24k
    case OP_TYPEMINUPTO:
409
7.55k
    case OP_TYPEPOSUPTO:
410
7.55k
    if (cc[1 + IMM2_SIZE] == OP_PROP
411
6.39k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
412
7.55k
    cc += PRIV(OP_lengths)[op];
413
7.55k
    break;
414
415
    /* Check a class for variable quantification */
416
417
15.3k
    case OP_CLASS:
418
21.9k
    case OP_NCLASS:
419
21.9k
#ifdef SUPPORT_WIDE_CHARS
420
64.3k
    case OP_XCLASS:
421
67.3k
    case OP_ECLASS:
422
    /* The original code caused an unsigned overflow in 64 bit systems,
423
    so now we use a conditional statement. */
424
67.3k
    if (op == OP_XCLASS || op == OP_ECLASS)
425
45.4k
      cc += GET(cc, 1);
426
21.9k
    else
427
21.9k
#endif
428
21.9k
      cc += PRIV(OP_lengths)[OP_CLASS];
429
430
67.3k
    switch (*cc)
431
67.3k
      {
432
7.61k
      case OP_CRPLUS:
433
8.81k
      case OP_CRMINPLUS:
434
11.9k
      case OP_CRPOSPLUS:
435
11.9k
      branchlength++;
436
11.9k
      PCRE2_FALLTHROUGH /* Fall through */
437
438
12.9k
      case OP_CRSTAR:
439
14.1k
      case OP_CRMINSTAR:
440
18.5k
      case OP_CRQUERY:
441
20.3k
      case OP_CRMINQUERY:
442
23.9k
      case OP_CRPOSSTAR:
443
26.5k
      case OP_CRPOSQUERY:
444
26.5k
      cc++;
445
26.5k
      break;
446
447
730
      case OP_CRRANGE:
448
1.99k
      case OP_CRMINRANGE:
449
3.98k
      case OP_CRPOSRANGE:
450
3.98k
      branchlength += GET2(cc,1);
451
3.98k
      cc += 1 + 2 * IMM2_SIZE;
452
3.98k
      break;
453
454
36.7k
      default:
455
36.7k
      branchlength++;
456
36.7k
      break;
457
67.3k
      }
458
67.3k
    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
67.3k
    case OP_DNREF:
478
4.78k
    case OP_DNREFI:
479
4.78k
    if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
480
3.28k
      {
481
3.28k
      int count = GET2(cc, 1+IMM2_SIZE);
482
3.28k
      PCRE2_SPTR slot =
483
3.28k
        (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) +
484
3.28k
          GET2(cc, 1) * re->name_entry_size;
485
486
3.28k
      d = INT_MAX;
487
488
      /* Scan all groups with the same name; find the shortest. */
489
490
12.3k
      while (count-- > 0)
491
12.0k
        {
492
12.0k
        int dd, i;
493
12.0k
        recno = GET2(slot, 0);
494
495
12.0k
        if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
496
9.05k
          dd = backref_cache[recno];
497
3.03k
        else
498
3.03k
          {
499
3.03k
          ce = cs = PRIV(find_bracket)(startcode, utf, recno);
500
3.03k
          if (cs == NULL) return -2;
501
6.63k
          do ce += GET(ce, 1); while (*ce == OP_ALT);
502
503
3.03k
          dd = 0;
504
3.03k
          if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
505
3.03k
            {
506
3.03k
            if (cc > cs && cc < ce)    /* Simple recursion */
507
825
              {
508
825
              had_recurse = TRUE;
509
825
              }
510
2.20k
            else
511
2.20k
              {
512
2.20k
              recurse_check *r = recurses;
513
5.92k
              for (r = recurses; r != NULL; r = r->prev)
514
4.06k
                if (r->group == cs) break;
515
2.20k
              if (r != NULL)           /* Mutual recursion */
516
347
                {
517
347
                had_recurse = TRUE;
518
347
                }
519
1.85k
              else
520
1.85k
                {
521
1.85k
                this_recurse.prev = recurses;  /* No recursion */
522
1.85k
                this_recurse.group = cs;
523
1.85k
                dd = find_minlength(re, cs, startcode, utf, &this_recurse,
524
1.85k
                  countptr, backref_cache);
525
1.85k
                if (dd < 0) return dd;
526
1.85k
                }
527
2.20k
              }
528
3.03k
            }
529
530
3.01k
          backref_cache[recno] = dd;
531
8.68k
          for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
532
3.01k
          backref_cache[0] = recno;
533
3.01k
          }
534
535
12.0k
        if (dd < d) d = dd;
536
12.0k
        if (d <= 0) break;    /* No point looking at any more */
537
9.07k
        slot += re->name_entry_size;
538
9.07k
        }
539
3.28k
      }
540
1.49k
    else d = 0;
541
4.76k
    cc += PRIV(OP_lengths)[*cc];
542
4.76k
    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
24.3k
    case OP_REF:
548
31.4k
    case OP_REFI:
549
31.4k
    recno = GET2(cc, 1);
550
31.4k
    if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
551
26.4k
      d = backref_cache[recno];
552
4.96k
    else
553
4.96k
      {
554
4.96k
      int i;
555
4.96k
      d = 0;
556
557
4.96k
      if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
558
4.84k
        {
559
4.84k
        ce = cs = PRIV(find_bracket)(startcode, utf, recno);
560
4.84k
        if (cs == NULL) return -2;
561
6.87k
        do ce += GET(ce, 1); while (*ce == OP_ALT);
562
563
4.84k
        if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
564
4.75k
          {
565
4.75k
          if (cc > cs && cc < ce)    /* Simple recursion */
566
893
            {
567
893
            had_recurse = TRUE;
568
893
            }
569
3.86k
          else
570
3.86k
            {
571
3.86k
            recurse_check *r = recurses;
572
8.11k
            for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
573
3.86k
            if (r != NULL)           /* Mutual recursion */
574
779
              {
575
779
              had_recurse = TRUE;
576
779
              }
577
3.08k
            else                     /* No recursion */
578
3.08k
              {
579
3.08k
              this_recurse.prev = recurses;
580
3.08k
              this_recurse.group = cs;
581
3.08k
              d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
582
3.08k
                backref_cache);
583
3.08k
              if (d < 0) return d;
584
3.08k
              }
585
3.86k
            }
586
4.75k
          }
587
4.84k
        }
588
589
4.94k
      backref_cache[recno] = d;
590
7.37k
      for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
591
4.94k
      backref_cache[0] = recno;
592
4.94k
      }
593
594
31.4k
    cc += PRIV(OP_lengths)[*cc];
595
596
    /* Handle repeated back references */
597
598
36.2k
    REPEAT_BACK_REFERENCE:
599
36.2k
    switch (*cc)
600
36.2k
      {
601
1.73k
      case OP_CRSTAR:
602
2.78k
      case OP_CRMINSTAR:
603
5.28k
      case OP_CRQUERY:
604
6.25k
      case OP_CRMINQUERY:
605
6.83k
      case OP_CRPOSSTAR:
606
7.48k
      case OP_CRPOSQUERY:
607
7.48k
      min = 0;
608
7.48k
      cc++;
609
7.48k
      break;
610
611
2.72k
      case OP_CRPLUS:
612
3.61k
      case OP_CRMINPLUS:
613
4.41k
      case OP_CRPOSPLUS:
614
4.41k
      min = 1;
615
4.41k
      cc++;
616
4.41k
      break;
617
618
1.23k
      case OP_CRRANGE:
619
2.38k
      case OP_CRMINRANGE:
620
3.23k
      case OP_CRPOSRANGE:
621
3.23k
      min = GET2(cc, 1);
622
3.23k
      cc += 1 + 2 * IMM2_SIZE;
623
3.23k
      break;
624
625
21.0k
      default:
626
21.0k
      min = 1;
627
21.0k
      break;
628
36.2k
      }
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
36.2k
    if ((d > 0 && (INT_MAX/d) < min) || (int)UINT16_MAX - branchlength < min*d)
635
604
      branchlength = UINT16_MAX;
636
35.5k
    else branchlength += min * d;
637
36.2k
    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
108k
    case OP_RECURSE:
644
108k
    cs = ce = startcode + GET(cc, 1);
645
108k
    recno = GET2(cs, 1+LINK_SIZE);
646
108k
    if (recno == prev_recurse_recno)
647
74.0k
      {
648
74.0k
      branchlength += prev_recurse_d;
649
74.0k
      }
650
33.9k
    else
651
33.9k
      {
652
59.6k
      do ce += GET(ce, 1); while (*ce == OP_ALT);
653
33.9k
      if (cc > cs && cc < ce)    /* Simple recursion */
654
23.0k
        had_recurse = TRUE;
655
10.8k
      else
656
10.8k
        {
657
10.8k
        recurse_check *r = recurses;
658
27.1k
        for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
659
10.8k
        if (r != NULL)          /* Mutual recursion */
660
4.30k
          had_recurse = TRUE;
661
6.56k
        else
662
6.56k
          {
663
6.56k
          this_recurse.prev = recurses;
664
6.56k
          this_recurse.group = cs;
665
6.56k
          prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
666
6.56k
            countptr, backref_cache);
667
6.56k
          if (prev_recurse_d < 0) return prev_recurse_d;
668
6.51k
          prev_recurse_recno = recno;
669
6.51k
          branchlength += prev_recurse_d;
670
6.51k
          }
671
10.8k
        }
672
33.9k
      }
673
107k
    cc += 1 + LINK_SIZE + once_fudge;
674
107k
    once_fudge = 0;
675
107k
    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
1.48k
    case OP_UPTO:
686
2.55k
    case OP_UPTOI:
687
4.54k
    case OP_NOTUPTO:
688
5.93k
    case OP_NOTUPTOI:
689
6.65k
    case OP_MINUPTO:
690
7.13k
    case OP_MINUPTOI:
691
8.49k
    case OP_NOTMINUPTO:
692
9.45k
    case OP_NOTMINUPTOI:
693
11.5k
    case OP_POSUPTO:
694
13.2k
    case OP_POSUPTOI:
695
13.4k
    case OP_NOTPOSUPTO:
696
14.8k
    case OP_NOTPOSUPTOI:
697
698
24.8k
    case OP_STAR:
699
29.3k
    case OP_STARI:
700
30.3k
    case OP_NOTSTAR:
701
30.8k
    case OP_NOTSTARI:
702
34.0k
    case OP_MINSTAR:
703
43.8k
    case OP_MINSTARI:
704
44.7k
    case OP_NOTMINSTAR:
705
45.6k
    case OP_NOTMINSTARI:
706
64.2k
    case OP_POSSTAR:
707
70.3k
    case OP_POSSTARI:
708
71.0k
    case OP_NOTPOSSTAR:
709
71.5k
    case OP_NOTPOSSTARI:
710
711
86.2k
    case OP_QUERY:
712
94.3k
    case OP_QUERYI:
713
95.0k
    case OP_NOTQUERY:
714
96.7k
    case OP_NOTQUERYI:
715
98.8k
    case OP_MINQUERY:
716
120k
    case OP_MINQUERYI:
717
121k
    case OP_NOTMINQUERY:
718
121k
    case OP_NOTMINQUERYI:
719
162k
    case OP_POSQUERY:
720
173k
    case OP_POSQUERYI:
721
175k
    case OP_NOTPOSQUERY:
722
176k
    case OP_NOTPOSQUERYI:
723
724
176k
    cc += PRIV(OP_lengths)[op];
725
176k
#ifdef SUPPORT_UNICODE
726
176k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
727
176k
#endif
728
176k
    break;
729
730
    /* Skip these, but we need to add in the name length. */
731
732
2.54k
    case OP_MARK:
733
4.64k
    case OP_COMMIT_ARG:
734
5.15k
    case OP_PRUNE_ARG:
735
6.36k
    case OP_SKIP_ARG:
736
7.63k
    case OP_THEN_ARG:
737
7.63k
    cc += PRIV(OP_lengths)[op] + cc[1];
738
7.63k
    break;
739
740
    /* The remaining opcodes are just skipped over. */
741
742
0
    case OP_CLOSE:
743
1.13k
    case OP_COMMIT:
744
2.64k
    case OP_FAIL:
745
6.44k
    case OP_PRUNE:
746
29.3k
    case OP_SET_SOM:
747
30.3k
    case OP_SKIP:
748
33.7k
    case OP_THEN:
749
33.7k
    cc += PRIV(OP_lengths)[op];
750
33.7k
    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
8.66M
    }
761
8.66M
  }
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
392k
}
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
218k
{
792
218k
uint32_t c = *p++;   /* First code unit */
793
794
218k
(void)utf;           /* Stop compiler warnings when UTF not supported */
795
218k
(void)ucp;
796
797
/* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for
798
0xff. */
799
800
218k
#if PCRE2_CODE_UNIT_WIDTH != 8
801
218k
if (c > 0xff) SET_BIT(0xff); else
802
108k
#endif
803
804
108k
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
218k
#ifdef SUPPORT_UNICODE
810
218k
if (utf)
811
51.4k
  {
812
#if PCRE2_CODE_UNIT_WIDTH == 8
813
  if (c >= 0xc0) GETUTF8INC(c, p);
814
#elif PCRE2_CODE_UNIT_WIDTH == 16
815
51.4k
  if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
816
51.4k
#endif
817
51.4k
  }
818
218k
#endif  /* SUPPORT_UNICODE */
819
820
/* If caseless, handle the other case of the character. */
821
822
218k
if (caseless)
823
58.5k
  {
824
58.5k
#ifdef SUPPORT_UNICODE
825
58.5k
  if (utf || ucp)
826
40.0k
    {
827
40.0k
    c = UCD_OTHERCASE(c);
828
#if PCRE2_CODE_UNIT_WIDTH == 8
829
    if (utf)
830
      {
831
      PCRE2_UCHAR buff[6];
832
      (void)PRIV(ord2utf)(c, buff);
833
      SET_BIT(buff[0]);
834
      }
835
    else if (c < 256) SET_BIT(c);
836
#else  /* 16-bit or 32-bit mode */
837
40.0k
    if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
838
40.0k
#endif
839
40.0k
    }
840
841
18.5k
  else
842
18.5k
#endif  /* SUPPORT_UNICODE */
843
844
  /* Not UTF or UCP */
845
846
18.5k
  if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
847
58.5k
  }
848
849
218k
return p;
850
218k
}
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
23.2k
{
876
23.2k
uint32_t c;
877
766k
for (c = 0; c < table_limit; c++)
878
743k
  re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
879
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
880
if (table_limit == 32) return;
881
for (c = 128; c < 256; c++)
882
  {
883
  if ((re->tables[cbits_offset + c/8] & (1u << (c&7))) != 0)
884
    {
885
    PCRE2_UCHAR buff[6];
886
    (void)PRIV(ord2utf)(c, buff);
887
    SET_BIT(buff[0]);
888
    }
889
  }
890
#endif  /* UTF-8 */
891
23.2k
}
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
31.3k
{
917
31.3k
uint32_t c;
918
1.03M
for (c = 0; c < table_limit; c++)
919
1.00M
  re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type]));
920
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
921
if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
922
#endif
923
31.3k
}
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
{
946
uint32_t type, list_ind;
947
uint32_t char_list_add = XCL_CHAR_LIST_LOW_16_ADD;
948
uint32_t range_start = ~(uint32_t)0, range_end = 0;
949
const uint8_t *next_char;
950
PCRE2_UCHAR start_buffer[6], end_buffer[6];
951
PCRE2_UCHAR start, end;
952
953
/* Only needed in 8-bit mode at the moment. */
954
type = (uint32_t)(code[0] << 8) | code[1];
955
code += 2;
956
957
/* Align characters. */
958
next_char = char_lists_end - (GET(code, 0) << 1);
959
type &= XCL_TYPE_MASK;
960
list_ind = 0;
961
962
if ((type & XCL_BEGIN_WITH_RANGE) != 0)
963
  range_start = XCL_CHAR_LIST_LOW_16_START;
964
965
while (type > 0)
966
  {
967
  uint32_t item_count = type & XCL_ITEM_COUNT_MASK;
968
969
  if (item_count == XCL_ITEM_COUNT_MASK)
970
    {
971
    if (list_ind <= 1)
972
      {
973
      item_count = *(const uint16_t*)next_char;
974
      next_char += 2;
975
      }
976
    else
977
      {
978
      item_count = *(const uint32_t*)next_char;
979
      next_char += 4;
980
      }
981
    }
982
983
  while (item_count > 0)
984
    {
985
    if (list_ind <= 1)
986
      {
987
      range_end = *(const uint16_t*)next_char;
988
      next_char += 2;
989
      }
990
    else
991
      {
992
      range_end = *(const uint32_t*)next_char;
993
      next_char += 4;
994
      }
995
996
    if ((range_end & XCL_CHAR_END) != 0)
997
      {
998
      range_end = char_list_add + (range_end >> XCL_CHAR_SHIFT);
999
1000
      PRIV(ord2utf)(range_end, end_buffer);
1001
      end = end_buffer[0];
1002
1003
      if (range_start < range_end)
1004
        {
1005
        PRIV(ord2utf)(range_start, start_buffer);
1006
        for (start = start_buffer[0]; start <= end; start++)
1007
          start_bitmap[start / 8] |= (1u << (start & 7));
1008
        }
1009
      else
1010
        start_bitmap[end / 8] |= (1u << (end & 7));
1011
1012
      range_start = ~(uint32_t)0;
1013
      }
1014
    else
1015
      range_start = char_list_add + (range_end >> XCL_CHAR_SHIFT);
1016
1017
    item_count--;
1018
    }
1019
1020
  list_ind++;
1021
  type >>= XCL_TYPE_BIT_LEN;
1022
1023
  if (range_start == ~(uint32_t)0)
1024
    {
1025
    if ((type & XCL_BEGIN_WITH_RANGE) != 0)
1026
      {
1027
      /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_START is not possible. */
1028
      if (list_ind == 1) range_start = XCL_CHAR_LIST_HIGH_16_START;
1029
      else range_start = XCL_CHAR_LIST_LOW_32_START;
1030
      }
1031
    }
1032
  else if ((type & XCL_BEGIN_WITH_RANGE) == 0)
1033
    {
1034
    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
    if (list_ind == 1) range_end = XCL_CHAR_LIST_LOW_16_END;
1039
    else range_end = XCL_CHAR_LIST_HIGH_16_END;
1040
1041
    PRIV(ord2utf)(range_end, end_buffer);
1042
    end = end_buffer[0];
1043
1044
    for (start = start_buffer[0]; start <= end; start++)
1045
      start_bitmap[start / 8] |= (1u << (start & 7));
1046
1047
    range_start = ~(uint32_t)0;
1048
    }
1049
1050
  /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_ADD is not possible. */
1051
  if (list_ind == 1) char_list_add = XCL_CHAR_LIST_HIGH_16_ADD;
1052
  else char_list_add = XCL_CHAR_LIST_LOW_32_ADD;
1053
  }
1054
}
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
273k
{
1096
273k
uint32_t c;
1097
273k
int yield = SSB_DONE;
1098
1099
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1100
int table_limit = utf? 16:32;
1101
#else
1102
273k
int table_limit = 32;
1103
273k
#endif
1104
1105
273k
*depthptr += 1;
1106
273k
if (*depthptr > 1000) return SSB_TOODEEP;
1107
1108
273k
do
1109
529k
  {
1110
529k
  BOOL try_next = TRUE;
1111
529k
  PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
1112
1113
529k
  if (*code == OP_CBRA || *code == OP_SCBRA ||
1114
439k
      *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
1115
1116
1.73M
  while (try_next)    /* Loop for items in this branch */
1117
1.28M
    {
1118
1.28M
    int rc;
1119
1.28M
    PCRE2_SPTR ncode;
1120
1.28M
    const uint8_t *classmap = NULL;
1121
1.28M
#ifdef SUPPORT_WIDE_CHARS
1122
1.28M
    PCRE2_UCHAR xclassflags;
1123
1.28M
#endif
1124
1125
1.28M
    switch(*tcode)
1126
1.28M
      {
1127
      /* If we reach something we don't understand, it means a new opcode has
1128
      been created that hasn't been added to this function. Hopefully this
1129
      problem will be discovered during testing. */
1130
1131
0
      default:
1132
0
      return SSB_UNKNOWN;
1133
1134
      /* Fail for a valid opcode that implies no starting bits. */
1135
1136
107
      case OP_ACCEPT:
1137
123
      case OP_ASSERT_ACCEPT:
1138
388
      case OP_ALLANY:
1139
2.15k
      case OP_ANY:
1140
2.15k
      case OP_ANYBYTE:
1141
2.21k
      case OP_CIRCM:
1142
2.26k
      case OP_CLOSE:
1143
2.29k
      case OP_COMMIT:
1144
2.31k
      case OP_COMMIT_ARG:
1145
3.78k
      case OP_COND:
1146
3.79k
      case OP_CREF:
1147
3.79k
      case OP_FALSE:
1148
3.79k
      case OP_TRUE:
1149
3.79k
      case OP_DNCREF:
1150
3.82k
      case OP_DNREF:
1151
3.87k
      case OP_DNREFI:
1152
3.88k
      case OP_DNRREF:
1153
4.51k
      case OP_DOLL:
1154
4.61k
      case OP_DOLLM:
1155
4.61k
      case OP_END:
1156
4.88k
      case OP_EOD:
1157
5.07k
      case OP_EODN:
1158
5.66k
      case OP_EXTUNI:
1159
5.70k
      case OP_FAIL:
1160
5.78k
      case OP_MARK:
1161
6.00k
      case OP_NOT:
1162
6.16k
      case OP_NOTEXACT:
1163
6.22k
      case OP_NOTEXACTI:
1164
6.31k
      case OP_NOTI:
1165
6.34k
      case OP_NOTMINPLUS:
1166
6.37k
      case OP_NOTMINPLUSI:
1167
6.41k
      case OP_NOTMINQUERY:
1168
6.44k
      case OP_NOTMINQUERYI:
1169
6.49k
      case OP_NOTMINSTAR:
1170
6.54k
      case OP_NOTMINSTARI:
1171
6.55k
      case OP_NOTMINUPTO:
1172
6.55k
      case OP_NOTMINUPTOI:
1173
6.70k
      case OP_NOTPLUS:
1174
6.73k
      case OP_NOTPLUSI:
1175
6.81k
      case OP_NOTPOSPLUS:
1176
6.82k
      case OP_NOTPOSPLUSI:
1177
6.89k
      case OP_NOTPOSQUERY:
1178
6.91k
      case OP_NOTPOSQUERYI:
1179
6.98k
      case OP_NOTPOSSTAR:
1180
7.00k
      case OP_NOTPOSSTARI:
1181
7.03k
      case OP_NOTPOSUPTO:
1182
7.04k
      case OP_NOTPOSUPTOI:
1183
8.26k
      case OP_NOTPROP:
1184
8.39k
      case OP_NOTQUERY:
1185
8.44k
      case OP_NOTQUERYI:
1186
8.73k
      case OP_NOTSTAR:
1187
8.78k
      case OP_NOTSTARI:
1188
8.81k
      case OP_NOTUPTO:
1189
8.83k
      case OP_NOTUPTOI:
1190
10.0k
      case OP_NOT_HSPACE:
1191
10.5k
      case OP_NOT_VSPACE:
1192
10.5k
      case OP_PRUNE:
1193
10.6k
      case OP_PRUNE_ARG:
1194
11.7k
      case OP_RECURSE:
1195
12.0k
      case OP_REF:
1196
12.1k
      case OP_REFI:
1197
12.1k
      case OP_REVERSE:
1198
12.2k
      case OP_VREVERSE:
1199
12.2k
      case OP_RREF:
1200
12.2k
      case OP_SCOND:
1201
12.3k
      case OP_SET_SOM:
1202
12.3k
      case OP_SKIP:
1203
12.4k
      case OP_SKIP_ARG:
1204
12.5k
      case OP_SOD:
1205
12.6k
      case OP_SOM:
1206
12.7k
      case OP_THEN:
1207
12.7k
      case OP_THEN_ARG:
1208
12.7k
      return SSB_FAIL;
1209
1210
      /* OP_CIRC happens only at the start of an anchored branch (multiline ^
1211
      uses OP_CIRCM). Skip over it. */
1212
1213
3.26k
      case OP_CIRC:
1214
3.26k
      tcode += PRIV(OP_lengths)[OP_CIRC];
1215
3.26k
      break;
1216
1217
      /* A "real" property test implies no starting bits, but the fake property
1218
      PT_CLIST identifies a list of characters. These lists are short, as they
1219
      are used for characters with more than one "other case", so there is no
1220
      point in recognizing them for OP_NOTPROP. */
1221
1222
2.49k
      case OP_PROP:
1223
2.49k
      if (tcode[1] != PT_CLIST) return SSB_FAIL;
1224
1.97k
        {
1225
1.97k
        const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
1226
7.89k
        while ((c = *p++) < NOTACHAR)
1227
5.92k
          {
1228
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1229
          if (utf)
1230
            {
1231
            PCRE2_UCHAR buff[6];
1232
            (void)PRIV(ord2utf)(c, buff);
1233
            c = buff[0];
1234
            }
1235
#endif
1236
5.92k
          if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
1237
5.92k
          }
1238
1.97k
        }
1239
1.97k
      try_next = FALSE;
1240
1.97k
      break;
1241
1242
      /* We can ignore word boundary tests. */
1243
1244
1.49k
      case OP_WORD_BOUNDARY:
1245
2.94k
      case OP_NOT_WORD_BOUNDARY:
1246
3.86k
      case OP_UCP_WORD_BOUNDARY:
1247
4.44k
      case OP_NOT_UCP_WORD_BOUNDARY:
1248
4.44k
      tcode++;
1249
4.44k
      break;
1250
1251
      /* For a positive lookahead assertion, inspect what immediately follows,
1252
      ignoring intermediate assertions and callouts. If the next item is one
1253
      that sets a mandatory character, skip this assertion. Otherwise, treat it
1254
      the same as other bracket groups. */
1255
1256
16.4k
      case OP_ASSERT:
1257
34.0k
      case OP_ASSERT_NA:
1258
34.0k
      ncode = tcode + GET(tcode, 1);
1259
71.9k
      while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1260
34.0k
      ncode += 1 + LINK_SIZE;
1261
1262
      /* Skip irrelevant items */
1263
1264
4.37M
      for (BOOL done = FALSE; !done;)
1265
4.34M
        {
1266
4.34M
        switch (*ncode)
1267
4.34M
          {
1268
242k
          case OP_ASSERT:
1269
242k
          case OP_ASSERT_NOT:
1270
243k
          case OP_ASSERTBACK:
1271
245k
          case OP_ASSERTBACK_NOT:
1272
4.29M
          case OP_ASSERT_NA:
1273
4.29M
          case OP_ASSERTBACK_NA:
1274
4.29M
          case OP_ASSERT_SCS:
1275
4.29M
          ncode += GET(ncode, 1);
1276
14.5M
          while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1277
4.29M
          ncode += 1 + LINK_SIZE;
1278
4.29M
          break;
1279
1280
432
          case OP_WORD_BOUNDARY:
1281
2.43k
          case OP_NOT_WORD_BOUNDARY:
1282
2.82k
          case OP_UCP_WORD_BOUNDARY:
1283
4.16k
          case OP_NOT_UCP_WORD_BOUNDARY:
1284
4.16k
          ncode++;
1285
4.16k
          break;
1286
1287
5.31k
          case OP_CALLOUT:
1288
5.31k
          ncode += PRIV(OP_lengths)[OP_CALLOUT];
1289
5.31k
          break;
1290
1291
477
          case OP_CALLOUT_STR:
1292
477
          ncode += GET(ncode, 1 + 2*LINK_SIZE);
1293
477
          break;
1294
1295
34.0k
          default:
1296
34.0k
          done = TRUE;
1297
34.0k
          break;
1298
4.34M
          }
1299
4.34M
        }
1300
1301
      /* Now check the next significant item. */
1302
1303
34.0k
      switch(*ncode)
1304
34.0k
        {
1305
16.8k
        default:
1306
16.8k
        break;
1307
1308
16.8k
        case OP_PROP:
1309
1.38k
        if (ncode[1] != PT_CLIST) break;
1310
211
        PCRE2_FALLTHROUGH /* Fall through */
1311
759
        case OP_ANYNL:
1312
4.17k
        case OP_CHAR:
1313
6.60k
        case OP_CHARI:
1314
7.37k
        case OP_EXACT:
1315
8.13k
        case OP_EXACTI:
1316
8.50k
        case OP_HSPACE:
1317
9.09k
        case OP_MINPLUS:
1318
9.54k
        case OP_MINPLUSI:
1319
9.84k
        case OP_PLUS:
1320
10.2k
        case OP_PLUSI:
1321
11.6k
        case OP_POSPLUS:
1322
12.0k
        case OP_POSPLUSI:
1323
12.5k
        case OP_VSPACE:
1324
        /* Note that these types will only be present in non-UCP mode. */
1325
13.5k
        case OP_DIGIT:
1326
13.7k
        case OP_NOT_DIGIT:
1327
13.9k
        case OP_WORDCHAR:
1328
14.1k
        case OP_NOT_WORDCHAR:
1329
15.7k
        case OP_WHITESPACE:
1330
16.0k
        case OP_NOT_WHITESPACE:
1331
16.0k
        tcode = ncode;
1332
16.0k
        continue;   /* With the following significant opcode */
1333
34.0k
        }
1334
18.0k
      PCRE2_FALLTHROUGH /* Fall through */
1335
18.0k
1336
18.0k
      /* For a group bracket or a positive assertion without an immediately
1337
18.0k
      following mandatory setting, recurse to set bits from within the
1338
18.0k
      subpattern. If it can't find anything, we have to give up. If it finds
1339
18.0k
      some mandatory character(s), we are done for this branch. Otherwise,
1340
18.0k
      carry on scanning after the subpattern. */
1341
18.0k
1342
55.1k
      case OP_BRA:
1343
55.4k
      case OP_SBRA:
1344
143k
      case OP_CBRA:
1345
144k
      case OP_SCBRA:
1346
144k
      case OP_BRAPOS:
1347
145k
      case OP_SBRAPOS:
1348
146k
      case OP_CBRAPOS:
1349
147k
      case OP_SCBRAPOS:
1350
151k
      case OP_ONCE:
1351
200k
      case OP_SCRIPT_RUN:
1352
200k
      rc = set_start_bits(re, tcode, utf, ucp, depthptr);
1353
200k
      if (rc == SSB_DONE)
1354
8.66k
        {
1355
8.66k
        try_next = FALSE;
1356
8.66k
        }
1357
192k
      else if (rc == SSB_CONTINUE)
1358
188k
        {
1359
423k
        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1360
188k
        tcode += 1 + LINK_SIZE;
1361
188k
        }
1362
3.87k
      else return rc;   /* FAIL, UNKNOWN, or TOODEEP */
1363
196k
      break;
1364
1365
      /* If we hit ALT or KET, it means we haven't found anything mandatory in
1366
      this branch, though we might have found something optional. For ALT, we
1367
      continue with the next alternative, but we have to arrange that the final
1368
      result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
1369
      return SSB_CONTINUE: if this is the top level, that indicates failure,
1370
      but after a nested subpattern, it causes scanning to continue. */
1371
1372
196k
      case OP_ALT:
1373
185k
      yield = SSB_CONTINUE;
1374
185k
      try_next = FALSE;
1375
185k
      break;
1376
1377
37.7k
      case OP_KET:
1378
38.8k
      case OP_KETRMAX:
1379
39.1k
      case OP_KETRMIN:
1380
40.1k
      case OP_KETRPOS:
1381
40.1k
      return SSB_CONTINUE;
1382
1383
      /* Skip over callout */
1384
1385
102k
      case OP_CALLOUT:
1386
102k
      tcode += PRIV(OP_lengths)[OP_CALLOUT];
1387
102k
      break;
1388
1389
419
      case OP_CALLOUT_STR:
1390
419
      tcode += GET(tcode, 1 + 2*LINK_SIZE);
1391
419
      break;
1392
1393
      /* Skip over lookbehind, negative lookahead, and scan substring
1394
      assertions */
1395
1396
164k
      case OP_ASSERT_NOT:
1397
209k
      case OP_ASSERTBACK:
1398
276k
      case OP_ASSERTBACK_NOT:
1399
323k
      case OP_ASSERTBACK_NA:
1400
323k
      case OP_ASSERT_SCS:
1401
373k
      do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1402
323k
      tcode += 1 + LINK_SIZE;
1403
323k
      break;
1404
1405
      /* BRAZERO does the bracket, but carries on. */
1406
1407
31.6k
      case OP_BRAZERO:
1408
36.2k
      case OP_BRAMINZERO:
1409
37.7k
      case OP_BRAPOSZERO:
1410
37.7k
      rc = set_start_bits(re, ++tcode, utf, ucp, depthptr);
1411
37.7k
      if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc;
1412
19.8k
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1413
18.8k
      tcode += 1 + LINK_SIZE;
1414
18.8k
      break;
1415
1416
      /* SKIPZERO skips the bracket. */
1417
1418
1.58k
      case OP_SKIPZERO:
1419
1.58k
      tcode++;
1420
1.92k
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1421
1.58k
      tcode += 1 + LINK_SIZE;
1422
1.58k
      break;
1423
1424
      /* Single-char * or ? sets the bit and tries the next item */
1425
1426
5.47k
      case OP_STAR:
1427
5.91k
      case OP_MINSTAR:
1428
9.06k
      case OP_POSSTAR:
1429
15.0k
      case OP_QUERY:
1430
15.8k
      case OP_MINQUERY:
1431
19.6k
      case OP_POSQUERY:
1432
19.6k
      tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1433
19.6k
      break;
1434
1435
4.47k
      case OP_STARI:
1436
9.48k
      case OP_MINSTARI:
1437
10.4k
      case OP_POSSTARI:
1438
11.0k
      case OP_QUERYI:
1439
13.0k
      case OP_MINQUERYI:
1440
14.0k
      case OP_POSQUERYI:
1441
14.0k
      tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1442
14.0k
      break;
1443
1444
      /* Single-char upto sets the bit and tries the next */
1445
1446
519
      case OP_UPTO:
1447
1.16k
      case OP_MINUPTO:
1448
4.87k
      case OP_POSUPTO:
1449
4.87k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp);
1450
4.87k
      break;
1451
1452
1.48k
      case OP_UPTOI:
1453
1.84k
      case OP_MINUPTOI:
1454
2.88k
      case OP_POSUPTOI:
1455
2.88k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp);
1456
2.88k
      break;
1457
1458
      /* At least one single char sets the bit and stops */
1459
1460
1.44k
      case OP_EXACT:
1461
1.44k
      tcode += IMM2_SIZE;
1462
1.44k
      PCRE2_FALLTHROUGH /* Fall through */
1463
126k
      case OP_CHAR:
1464
127k
      case OP_PLUS:
1465
129k
      case OP_MINPLUS:
1466
135k
      case OP_POSPLUS:
1467
135k
      (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1468
135k
      try_next = FALSE;
1469
135k
      break;
1470
1471
1.40k
      case OP_EXACTI:
1472
1.40k
      tcode += IMM2_SIZE;
1473
1.40k
      PCRE2_FALLTHROUGH /* Fall through */
1474
37.7k
      case OP_CHARI:
1475
38.7k
      case OP_PLUSI:
1476
39.9k
      case OP_MINPLUSI:
1477
41.6k
      case OP_POSPLUSI:
1478
41.6k
      (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1479
41.6k
      try_next = FALSE;
1480
41.6k
      break;
1481
1482
      /* Special spacing and line-terminating items. These recognize specific
1483
      lists of characters. The difference between VSPACE and ANYNL is that the
1484
      latter can match the two-character CRLF sequence, but that is not
1485
      relevant for finding the first character, so their code here is
1486
      identical. */
1487
1488
1.59k
      case OP_HSPACE:
1489
1.59k
      SET_BIT(CHAR_HT);
1490
1.59k
      SET_BIT(CHAR_SPACE);
1491
1492
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1493
      the bits for NBSP and for code units >= 255, independently of UTF. */
1494
1495
1.59k
#if PCRE2_CODE_UNIT_WIDTH != 8
1496
1.59k
      SET_BIT(CHAR_NBSP);
1497
1.59k
      SET_BIT(0xFF);
1498
#else
1499
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1500
      units of horizontal space characters. */
1501
1502
#ifdef SUPPORT_UNICODE
1503
      if (utf)
1504
        {
1505
        SET_BIT(0xC2);  /* For U+00A0 */
1506
        SET_BIT(0xE1);  /* For U+1680, U+180E */
1507
        SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1508
        SET_BIT(0xE3);  /* For U+3000 */
1509
        }
1510
      else
1511
#endif
1512
      /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1513
        {
1514
        SET_BIT(CHAR_NBSP);
1515
        }
1516
#endif  /* 8-bit support */
1517
1518
1.59k
      try_next = FALSE;
1519
1.59k
      break;
1520
1521
2.25k
      case OP_ANYNL:
1522
4.52k
      case OP_VSPACE:
1523
4.52k
      SET_BIT(CHAR_LF);
1524
4.52k
      SET_BIT(CHAR_VT);
1525
4.52k
      SET_BIT(CHAR_FF);
1526
4.52k
      SET_BIT(CHAR_CR);
1527
1528
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1529
      the bits for NEL and for code units >= 255, independently of UTF. */
1530
1531
4.52k
#if PCRE2_CODE_UNIT_WIDTH != 8
1532
4.52k
      SET_BIT(CHAR_NEL);
1533
4.52k
      SET_BIT(0xFF);
1534
#else
1535
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1536
      units of vertical space characters. */
1537
1538
#ifdef SUPPORT_UNICODE
1539
      if (utf)
1540
        {
1541
        SET_BIT(0xC2);  /* For U+0085 (NEL) */
1542
        SET_BIT(0xE2);  /* For U+2028, U+2029 */
1543
        }
1544
      else
1545
#endif
1546
      /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1547
        {
1548
        SET_BIT(CHAR_NEL);
1549
        }
1550
#endif  /* 8-bit support */
1551
1552
4.52k
      try_next = FALSE;
1553
4.52k
      break;
1554
1555
      /* Single character types set the bits and stop. Note that if PCRE2_UCP
1556
      is set, we do not see these opcodes because \d etc are converted to
1557
      properties. Therefore, these apply in the case when only characters less
1558
      than 256 are recognized to match the types. */
1559
1560
7.12k
      case OP_NOT_DIGIT:
1561
7.12k
      set_nottype_bits(re, cbit_digit, table_limit);
1562
7.12k
      try_next = FALSE;
1563
7.12k
      break;
1564
1565
5.16k
      case OP_DIGIT:
1566
5.16k
      set_type_bits(re, cbit_digit, table_limit);
1567
5.16k
      try_next = FALSE;
1568
5.16k
      break;
1569
1570
4.73k
      case OP_NOT_WHITESPACE:
1571
4.73k
      set_nottype_bits(re, cbit_space, table_limit);
1572
4.73k
      try_next = FALSE;
1573
4.73k
      break;
1574
1575
4.74k
      case OP_WHITESPACE:
1576
4.74k
      set_type_bits(re, cbit_space, table_limit);
1577
4.74k
      try_next = FALSE;
1578
4.74k
      break;
1579
1580
4.09k
      case OP_NOT_WORDCHAR:
1581
4.09k
      set_nottype_bits(re, cbit_word, table_limit);
1582
4.09k
      try_next = FALSE;
1583
4.09k
      break;
1584
1585
3.08k
      case OP_WORDCHAR:
1586
3.08k
      set_type_bits(re, cbit_word, table_limit);
1587
3.08k
      try_next = FALSE;
1588
3.08k
      break;
1589
1590
      /* One or more character type fudges the pointer and restarts, knowing
1591
      it will hit a single character type and stop there. */
1592
1593
6.71k
      case OP_TYPEPLUS:
1594
9.31k
      case OP_TYPEMINPLUS:
1595
12.2k
      case OP_TYPEPOSPLUS:
1596
12.2k
      tcode++;
1597
12.2k
      break;
1598
1599
1.65k
      case OP_TYPEEXACT:
1600
1.65k
      tcode += 1 + IMM2_SIZE;
1601
1.65k
      break;
1602
1603
      /* Zero or more repeats of character types set the bits and then
1604
      try again. */
1605
1606
318
      case OP_TYPEUPTO:
1607
598
      case OP_TYPEMINUPTO:
1608
1.12k
      case OP_TYPEPOSUPTO:
1609
1.12k
      tcode += IMM2_SIZE;
1610
1.12k
      PCRE2_FALLTHROUGH /* Fall through */
1611
1612
6.30k
      case OP_TYPESTAR:
1613
7.17k
      case OP_TYPEMINSTAR:
1614
10.7k
      case OP_TYPEPOSSTAR:
1615
28.1k
      case OP_TYPEQUERY:
1616
29.2k
      case OP_TYPEMINQUERY:
1617
34.8k
      case OP_TYPEPOSQUERY:
1618
34.8k
      switch(tcode[1])
1619
34.8k
        {
1620
3.02k
        default:
1621
3.37k
        case OP_ANY:
1622
3.50k
        case OP_ALLANY:
1623
3.50k
        return SSB_FAIL;
1624
1625
945
        case OP_HSPACE:
1626
945
        SET_BIT(CHAR_HT);
1627
945
        SET_BIT(CHAR_SPACE);
1628
1629
        /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1630
        the bits for NBSP and for code units >= 255, independently of UTF. */
1631
1632
945
#if PCRE2_CODE_UNIT_WIDTH != 8
1633
945
        SET_BIT(CHAR_NBSP);
1634
945
        SET_BIT(0xFF);
1635
#else
1636
        /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1637
        units of horizontal space characters. */
1638
1639
#ifdef SUPPORT_UNICODE
1640
        if (utf)
1641
          {
1642
          SET_BIT(0xC2);  /* For U+00A0 */
1643
          SET_BIT(0xE1);  /* For U+1680, U+180E */
1644
          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1645
          SET_BIT(0xE3);  /* For U+3000 */
1646
          }
1647
        else
1648
#endif
1649
        /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1650
          {
1651
          SET_BIT(CHAR_NBSP);
1652
          }
1653
#endif  /* 8-bit support */
1654
945
        break;
1655
1656
550
        case OP_ANYNL:
1657
4.72k
        case OP_VSPACE:
1658
4.72k
        SET_BIT(CHAR_LF);
1659
4.72k
        SET_BIT(CHAR_VT);
1660
4.72k
        SET_BIT(CHAR_FF);
1661
4.72k
        SET_BIT(CHAR_CR);
1662
1663
        /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1664
        the bits for NEL and for code units >= 255, independently of UTF. */
1665
1666
4.72k
#if PCRE2_CODE_UNIT_WIDTH != 8
1667
4.72k
        SET_BIT(CHAR_NEL);
1668
4.72k
        SET_BIT(0xFF);
1669
#else
1670
        /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1671
        units of vertical space characters. */
1672
1673
#ifdef SUPPORT_UNICODE
1674
        if (utf)
1675
          {
1676
          SET_BIT(0xC2);  /* For U+0085 (NEL) */
1677
          SET_BIT(0xE2);  /* For U+2028, U+2029 */
1678
          }
1679
        else
1680
#endif
1681
        /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1682
          {
1683
          SET_BIT(CHAR_NEL);
1684
          }
1685
#endif  /* 8-bit support */
1686
4.72k
        break;
1687
1688
5.12k
        case OP_NOT_DIGIT:
1689
5.12k
        set_nottype_bits(re, cbit_digit, table_limit);
1690
5.12k
        break;
1691
1692
2.47k
        case OP_DIGIT:
1693
2.47k
        set_type_bits(re, cbit_digit, table_limit);
1694
2.47k
        break;
1695
1696
5.68k
        case OP_NOT_WHITESPACE:
1697
5.68k
        set_nottype_bits(re, cbit_space, table_limit);
1698
5.68k
        break;
1699
1700
3.68k
        case OP_WHITESPACE:
1701
3.68k
        set_type_bits(re, cbit_space, table_limit);
1702
3.68k
        break;
1703
1704
4.59k
        case OP_NOT_WORDCHAR:
1705
4.59k
        set_nottype_bits(re, cbit_word, table_limit);
1706
4.59k
        break;
1707
1708
4.07k
        case OP_WORDCHAR:
1709
4.07k
        set_type_bits(re, cbit_word, table_limit);
1710
4.07k
        break;
1711
34.8k
        }
1712
1713
31.3k
      tcode += 2;
1714
31.3k
      break;
1715
1716
      /* Set-based ECLASS: treat it the same as a "complex" XCLASS; give up. */
1717
1718
0
#ifdef SUPPORT_WIDE_CHARS
1719
207
      case OP_ECLASS:
1720
207
      return SSB_FAIL;
1721
0
#endif
1722
1723
      /* Extended class: if there are any property checks, or if this is a
1724
      negative XCLASS without a map, give up. If there are no property checks,
1725
      there must be wide characters on the XCLASS list, because otherwise an
1726
      XCLASS would not have been created. This means that code points >= 255
1727
      are potential starters. In the UTF-8 case we can scan them and set bits
1728
      for the relevant leading bytes. */
1729
1730
0
#ifdef SUPPORT_WIDE_CHARS
1731
35.0k
      case OP_XCLASS:
1732
35.0k
      xclassflags = tcode[1 + LINK_SIZE];
1733
35.0k
      if ((xclassflags & XCL_HASPROP) != 0 ||
1734
34.7k
          (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT)
1735
428
        return SSB_FAIL;
1736
1737
      /* We have a positive XCLASS or a negative one without a map. Set up the
1738
      map pointer if there is one, and fall through. */
1739
1740
34.6k
      classmap = ((xclassflags & XCL_MAP) == 0)? NULL :
1741
34.6k
        (const uint8_t *)(tcode + 1 + LINK_SIZE + 1);
1742
1743
      /* In UTF-8 mode, scan the character list and set bits for leading bytes,
1744
      then jump to handle the map. */
1745
1746
#if PCRE2_CODE_UNIT_WIDTH == 8
1747
      if (utf && (xclassflags & XCL_NOT) == 0)
1748
        {
1749
        PCRE2_UCHAR b, e;
1750
        PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32);
1751
        tcode += GET(tcode, 1);
1752
1753
        if (*p >= XCL_LIST)
1754
          {
1755
          study_char_list(p, re->start_bitmap,
1756
            ((const uint8_t *)re + re->code_start));
1757
          goto HANDLE_CLASSMAP;
1758
          }
1759
1760
        for (;;) switch (*p++)
1761
          {
1762
          case XCL_SINGLE:
1763
          b = *p++;
1764
          while ((*p & 0xc0) == 0x80) p++;
1765
          re->start_bitmap[b/8] |= (1u << (b&7));
1766
          break;
1767
1768
          case XCL_RANGE:
1769
          b = *p++;
1770
          while ((*p & 0xc0) == 0x80) p++;
1771
          e = *p++;
1772
          while ((*p & 0xc0) == 0x80) p++;
1773
          for (; b <= e; b++)
1774
            re->start_bitmap[b/8] |= (1u << (b&7));
1775
          break;
1776
1777
          case XCL_END:
1778
          goto HANDLE_CLASSMAP;
1779
1780
          /* LCOV_EXCL_START */
1781
          default:
1782
          PCRE2_DEBUG_UNREACHABLE();
1783
          return SSB_UNKNOWN;   /* Internal error, should not occur */
1784
          /* LCOV_EXCL_STOP */
1785
          }
1786
        }
1787
#endif  /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
1788
34.6k
#endif  /* SUPPORT_WIDE_CHARS */
1789
1790
      /* It seems that the fall through comment must be outside the #ifdef if
1791
      it is to avoid the gcc compiler warning. */
1792
1793
34.6k
      PCRE2_FALLTHROUGH /* Fall through */
1794
1795
      /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
1796
      in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
1797
      because it starts a character with a value > 255. In 8-bit non-UTF mode,
1798
      there is no difference between CLASS and NCLASS. In all other wide
1799
      character modes, set the 0xFF bit to indicate code units >= 255. */
1800
1801
36.9k
      case OP_NCLASS:
1802
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1803
      if (utf)
1804
        {
1805
        re->start_bitmap[24] |= 0xf0;            /* Bits for 0xc4 - 0xc8 */
1806
        memset(re->start_bitmap+25, 0xff, 7);    /* Bits for 0xc9 - 0xff */
1807
        }
1808
      PCRE2_FALLTHROUGH /* Fall through */
1809
#elif PCRE2_CODE_UNIT_WIDTH != 8
1810
36.9k
      SET_BIT(0xFF);                             /* For characters >= 255 */
1811
36.9k
      PCRE2_FALLTHROUGH /* Fall through */
1812
36.9k
#endif
1813
1814
      /* Enter here for a positive non-XCLASS. If we have fallen through from
1815
      an XCLASS, classmap will already be set; just advance the code pointer.
1816
      Otherwise, set up classmap for a non-XCLASS and advance past it. */
1817
1818
47.9k
      case OP_CLASS:
1819
47.9k
      if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
1820
13.2k
        {
1821
13.2k
        classmap = (const uint8_t *)(++tcode);
1822
13.2k
        tcode += 32 / sizeof(PCRE2_UCHAR);
1823
13.2k
        }
1824
1825
      /* When wide characters are supported, classmap may be NULL. In UTF-8
1826
      (sic) mode, the bits in a class bit map correspond to character values,
1827
      not to byte values. However, the bit map we are constructing is for byte
1828
      values. So we have to do a conversion for characters whose code point is
1829
      greater than 127. In fact, there are only two possible starting bytes for
1830
      characters in the range 128 - 255. */
1831
1832
#if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8
1833
      HANDLE_CLASSMAP:
1834
#endif
1835
47.9k
      if (classmap != NULL)
1836
44.8k
        {
1837
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1838
        if (utf)
1839
          {
1840
          for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
1841
          for (c = 128; c < 256; c++)
1842
            {
1843
            if ((classmap[c/8] & (1u << (c&7))) != 0)
1844
              {
1845
              int d = (c >> 6) | 0xc0;                 /* Set bit for this starter */
1846
              re->start_bitmap[d/8] |= (1u << (d&7));  /* and then skip on to the */
1847
              c = (c & 0xc0) + 0x40 - 1;               /* next relevant character. */
1848
              }
1849
            }
1850
          }
1851
        else
1852
#endif
1853
        /* In all modes except UTF-8, the two bit maps are compatible. */
1854
1855
44.8k
          {
1856
1.48M
          for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
1857
44.8k
          }
1858
44.8k
        }
1859
1860
      /* Act on what follows the class. For a zero minimum repeat, continue;
1861
      otherwise stop processing. */
1862
1863
47.9k
      switch (*tcode)
1864
47.9k
        {
1865
901
        case OP_CRSTAR:
1866
1.73k
        case OP_CRMINSTAR:
1867
2.75k
        case OP_CRQUERY:
1868
3.23k
        case OP_CRMINQUERY:
1869
4.34k
        case OP_CRPOSSTAR:
1870
6.11k
        case OP_CRPOSQUERY:
1871
6.11k
        tcode++;
1872
6.11k
        break;
1873
1874
587
        case OP_CRRANGE:
1875
852
        case OP_CRMINRANGE:
1876
1.65k
        case OP_CRPOSRANGE:
1877
1.65k
        if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1878
1.04k
          else try_next = FALSE;
1879
1.65k
        break;
1880
1881
40.1k
        default:
1882
40.1k
        try_next = FALSE;
1883
40.1k
        break;
1884
47.9k
        }
1885
47.9k
      break; /* End of class handling case */
1886
1.28M
      }      /* End of switch for opcodes */
1887
1.28M
    }        /* End of try_next loop */
1888
1889
449k
  code += GET(code, 1);   /* Advance to next branch */
1890
449k
  }
1891
449k
while (*code == OP_ALT);
1892
1893
193k
return yield;
1894
273k
}
1895
1896
1897
1898
/*************************************************
1899
*          Study a compiled expression           *
1900
*************************************************/
1901
1902
/* This function is handed a compiled expression that it must study to produce
1903
information that will speed up the matching.
1904
1905
Argument:
1906
  re       points to the compiled expression
1907
1908
Returns:   0 normally; non-zero should never normally occur
1909
           1 unknown opcode in set_start_bits
1910
           2 missing capturing bracket
1911
           3 unknown opcode in find_minlength
1912
*/
1913
1914
int
1915
PRIV(study)(pcre2_real_code *re)
1916
51.5k
{
1917
51.5k
int count = 0;
1918
51.5k
PCRE2_UCHAR *code;
1919
51.5k
BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
1920
51.5k
BOOL ucp = (re->overall_options & PCRE2_UCP) != 0;
1921
1922
/* Find start of compiled code */
1923
1924
51.5k
code = (PCRE2_UCHAR *)((uint8_t *)re + re->code_start);
1925
1926
/* For a pattern that has a first code unit, or a multiline pattern that
1927
matches only at "line start", there is no point in seeking a list of starting
1928
code units. */
1929
1930
51.5k
if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
1931
34.8k
  {
1932
34.8k
  int depth = 0;
1933
34.8k
  int rc = set_start_bits(re, code, utf, ucp, &depth);
1934
  /* LCOV_EXCL_START */
1935
34.8k
  if (rc == SSB_UNKNOWN)
1936
0
    {
1937
0
    PCRE2_DEBUG_UNREACHABLE();
1938
0
    return 1;
1939
0
    }
1940
  /* LCOV_EXCL_STOP */
1941
1942
  /* If a list of starting code units was set up, scan the list to see if only
1943
  one or two were listed. Having only one listed is rare because usually a
1944
  single starting code unit will have been recognized and PCRE2_FIRSTSET set.
1945
  If two are listed, see if they are caseless versions of the same character;
1946
  if so we can replace the list with a caseless first code unit. This gives
1947
  better performance and is plausibly worth doing for patterns such as [Ww]ord
1948
  or (word|WORD). */
1949
1950
34.8k
  if (rc == SSB_DONE)
1951
11.7k
    {
1952
11.7k
    int i;
1953
11.7k
    int a = -1;
1954
11.7k
    int b = -1;
1955
11.7k
    uint8_t *p = re->start_bitmap;
1956
11.7k
    uint32_t flags = PCRE2_FIRSTMAPSET;
1957
1958
127k
    for (i = 0; i < 256; p++, i += 8)
1959
127k
      {
1960
127k
      uint8_t x = *p;
1961
127k
      if (x != 0)
1962
15.6k
        {
1963
15.6k
        int c;
1964
15.6k
        uint8_t y = x & (~x + 1);   /* Least significant bit */
1965
15.6k
        if (y != x) goto DONE;      /* More than one bit set */
1966
1967
        /* In the 16-bit and 32-bit libraries, the bit for 0xff means "0xff and
1968
        all wide characters", so we cannot use it here. */
1969
1970
8.87k
#if PCRE2_CODE_UNIT_WIDTH != 8
1971
8.87k
        if (i == 248 && x == 0x80) goto DONE;
1972
6.42k
#endif
1973
1974
        /* Compute the character value */
1975
1976
6.42k
        c = i;
1977
6.42k
        switch (x)
1978
6.42k
          {
1979
2.45k
          case 1:   break;
1980
1.27k
          case 2:   c += 1; break;  case 4:  c += 2; break;
1981
813
          case 8:   c += 3; break;  case 16: c += 4; break;
1982
327
          case 32:  c += 5; break;  case 64: c += 6; break;
1983
258
          case 128: c += 7; break;
1984
6.42k
          }
1985
1986
        /* c contains the code unit value, in the range 0-255. In 8-bit UTF
1987
        mode, only values < 128 can be used. In all the other cases, c is a
1988
        character value. */
1989
1990
#if PCRE2_CODE_UNIT_WIDTH == 8
1991
        if (utf && c > 127) goto DONE;
1992
#endif
1993
6.42k
        if (a < 0) a = c;   /* First one found, save in a */
1994
2.36k
        else if (b < 0)     /* Second one found */
1995
2.34k
          {
1996
2.34k
          int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c);
1997
1998
2.34k
#ifdef SUPPORT_UNICODE
1999
2.34k
          if (utf || ucp)
2000
628
            {
2001
628
            if (UCD_CASESET(c) != 0) goto DONE;     /* Multiple case set */
2002
475
            if (c > 127) d = UCD_OTHERCASE(c);
2003
475
            }
2004
2.19k
#endif  /* SUPPORT_UNICODE */
2005
2006
2.19k
          if (d != a) goto DONE;   /* Not the other case of a */
2007
152
          b = c;                   /* Save second in b */
2008
2009
#ifdef EBCDIC
2010
          /* To match ASCII (which puts the uppercase one in a), swap a & b
2011
          if needed. This doesn't really matter, but neatens the tests. */
2012
          if (TABLE_GET((unsigned int)a, re->tables + lcc_offset, a) == a)
2013
            {
2014
            b = a;
2015
            a = c;
2016
            }
2017
#endif
2018
152
          }
2019
17
        else goto DONE;   /* More than two characters found */
2020
6.42k
        }
2021
127k
      }
2022
2023
    /* Replace the start code unit bits with a first code unit. If it is the
2024
    same as a required later code unit, then clear the required later code
2025
    unit. This is because a search for a required code unit starts after an
2026
    explicit first code unit, but at a code unit found from the bitmap.
2027
    Patterns such as /a*a/ don't work if both the start unit and required
2028
    unit are the same. */
2029
2030
390
    if (a >= 0) {
2031
339
      if ((re->flags & PCRE2_LASTSET) && (re->last_codeunit == (uint32_t)a || (b >= 0 && re->last_codeunit == (uint32_t)b))) {
2032
89
        re->flags &= ~(PCRE2_LASTSET | PCRE2_LASTCASELESS);
2033
89
        re->last_codeunit = 0;
2034
89
      }
2035
339
      re->first_codeunit = a;
2036
339
      flags = PCRE2_FIRSTSET;
2037
339
      if (b >= 0) flags |= PCRE2_FIRSTCASELESS;
2038
339
    }
2039
2040
11.7k
    DONE:
2041
11.7k
    re->flags |= flags;
2042
11.7k
    }
2043
34.8k
  }
2044
2045
/* Find the minimum length of subject string. If the pattern can match an empty
2046
string, the minimum length is already known. If the pattern contains (*ACCEPT)
2047
all bets are off, and we don't even try to find a minimum length. If there are
2048
more back references than the size of the vector we are going to cache them in,
2049
do nothing. A pattern that complicated will probably take a long time to
2050
analyze and may in any case turn out to be too complicated. Note that back
2051
reference minima are held as 16-bit numbers. */
2052
2053
51.5k
if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 &&
2054
37.5k
     re->top_backref <= MAX_CACHE_BACKREF)
2055
37.5k
  {
2056
37.5k
  int min;
2057
37.5k
  int backref_cache[MAX_CACHE_BACKREF+1];
2058
37.5k
  backref_cache[0] = 0;    /* Highest one that is set */
2059
37.5k
  min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
2060
37.5k
  switch(min)
2061
37.5k
    {
2062
90
    case -1:  /* \C in UTF mode or over-complex regex */
2063
90
    break;    /* Leave minlength unchanged (will be zero) */
2064
2065
    /* LCOV_EXCL_START */
2066
0
    case -2:
2067
0
    PCRE2_DEBUG_UNREACHABLE();
2068
0
    return 2; /* missing capturing bracket */
2069
    /* LCOV_EXCL_STOP */
2070
2071
    /* LCOV_EXCL_START */
2072
0
    case -3:
2073
0
    PCRE2_DEBUG_UNREACHABLE();
2074
0
    return 3; /* unrecognized opcode */
2075
    /* LCOV_EXCL_STOP */
2076
2077
37.4k
    default:
2078
37.4k
    re->minlength = (min > (int)UINT16_MAX)? (int)UINT16_MAX : min;
2079
37.4k
    break;
2080
37.5k
    }
2081
37.5k
  }
2082
2083
51.5k
return 0;
2084
51.5k
}
2085
2086
/* End of pcre2_study.c */