Coverage Report

Created: 2026-08-31 06:36

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
23.0k
#define MAX_CACHE_BACKREF 128
53
54
/* Set a bit in the starting code unit bit map. */
55
56
116k
#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
207k
{
106
207k
int length = -1;
107
207k
int branchlength = 0;
108
207k
int prev_cap_recno = -1;
109
207k
int prev_cap_d = 0;
110
207k
int prev_recurse_recno = -1;
111
207k
int prev_recurse_d = 0;
112
207k
uint32_t once_fudge = 0;
113
207k
BOOL had_recurse = FALSE;
114
207k
BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
115
207k
PCRE2_SPTR nextbranch = code + GET(code, 1);
116
207k
PCRE2_SPTR cc = code + 1 + LINK_SIZE;
117
207k
recurse_check this_recurse;
118
119
/* If this is a "could be empty" group, its minimum length is 0. */
120
121
207k
if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
122
123
/* Skip over capturing bracket number */
124
125
205k
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
205k
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
205k
for (;;)
136
1.35M
  {
137
1.35M
  int d, min, recno;
138
1.35M
  PCRE2_UCHAR op;
139
1.35M
  PCRE2_SPTR cs, ce;
140
141
1.35M
  if (branchlength >= (int)UINT16_MAX)
142
1.87k
    {
143
1.87k
    branchlength = UINT16_MAX;
144
1.87k
    cc = nextbranch;
145
1.87k
    }
146
147
1.35M
  op = *cc;
148
1.35M
  switch (op)
149
1.35M
    {
150
4.30k
    case OP_COND:
151
4.73k
    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
4.73k
    cs = cc + GET(cc, 1);
159
4.73k
    if (*cs != OP_ALT)
160
2.07k
      {
161
2.07k
      cc = cs + 1 + LINK_SIZE;
162
2.07k
      break;
163
2.07k
      }
164
2.66k
    goto PROCESS_NON_CAPTURE;
165
166
88.6k
    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
88.6k
    if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
173
365
      {
174
365
      once_fudge = 1 + LINK_SIZE;
175
365
      cc += 1 + LINK_SIZE;
176
365
      break;
177
365
      }
178
88.2k
    PCRE2_FALLTHROUGH /* Fall through */
179
88.2k
180
98.9k
    case OP_ONCE:
181
99.6k
    case OP_SCRIPT_RUN:
182
99.9k
    case OP_SBRA:
183
100k
    case OP_BRAPOS:
184
101k
    case OP_SBRAPOS:
185
104k
    PROCESS_NON_CAPTURE:
186
104k
    d = find_minlength(re, cc, startcode, utf, recurses, countptr,
187
104k
      backref_cache);
188
104k
    if (d < 0) return d;
189
103k
    branchlength += d;
190
109k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
191
103k
    cc += 1 + LINK_SIZE;
192
103k
    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
282k
    case OP_CBRA:
200
283k
    case OP_SCBRA:
201
284k
    case OP_CBRAPOS:
202
284k
    case OP_SCBRAPOS:
203
284k
    recno = (int)GET2(cc, 1+LINK_SIZE);
204
284k
    if (dupcapused || recno != prev_cap_recno)
205
64.2k
      {
206
64.2k
      prev_cap_recno = recno;
207
64.2k
      prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
208
64.2k
        backref_cache);
209
64.2k
      if (prev_cap_d < 0) return prev_cap_d;
210
64.2k
      }
211
284k
    branchlength += prev_cap_d;
212
292k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
213
284k
    cc += 1 + LINK_SIZE;
214
284k
    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
9.06k
    case OP_ALT:
231
207k
    case OP_KET:
232
208k
    case OP_KETRMAX:
233
208k
    case OP_KETRMIN:
234
210k
    case OP_KETRPOS:
235
210k
    case OP_END:
236
210k
    if (length < 0 || (!had_recurse && branchlength < length))
237
207k
      length = branchlength;
238
210k
    if (op != OP_ALT || length == 0) return length;
239
5.97k
    nextbranch = cc + GET(cc, 1);
240
5.97k
    cc += 1 + LINK_SIZE;
241
5.97k
    branchlength = 0;
242
5.97k
    had_recurse = FALSE;
243
5.97k
    break;
244
245
    /* Skip over assertive subpatterns */
246
247
1.47k
    case OP_ASSERT:
248
2.14k
    case OP_ASSERT_NOT:
249
2.77k
    case OP_ASSERTBACK:
250
3.19k
    case OP_ASSERTBACK_NOT:
251
4.66k
    case OP_ASSERT_NA:
252
10.6k
    case OP_ASSERT_SCS:
253
11.0k
    case OP_ASSERTBACK_NA:
254
11.2k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
255
11.0k
    PCRE2_FALLTHROUGH /* Fall through */
256
257
    /* Skip over things that don't match chars */
258
259
11.0k
    case OP_REVERSE:
260
11.0k
    case OP_VREVERSE:
261
20.1k
    case OP_CREF:
262
20.5k
    case OP_DNCREF:
263
21.4k
    case OP_RREF:
264
21.9k
    case OP_DNRREF:
265
21.9k
    case OP_FALSE:
266
21.9k
    case OP_TRUE:
267
29.5k
    case OP_CALLOUT:
268
29.8k
    case OP_SOD:
269
30.2k
    case OP_SOM:
270
31.0k
    case OP_EOD:
271
31.9k
    case OP_EODN:
272
33.0k
    case OP_CIRC:
273
33.3k
    case OP_CIRCM:
274
34.0k
    case OP_DOLL:
275
34.4k
    case OP_DOLLM:
276
35.0k
    case OP_NOT_WORD_BOUNDARY:
277
35.7k
    case OP_WORD_BOUNDARY:
278
36.0k
    case OP_NOT_UCP_WORD_BOUNDARY:
279
36.4k
    case OP_UCP_WORD_BOUNDARY:
280
36.4k
    cc += PRIV(OP_lengths)[*cc];
281
36.4k
    break;
282
283
775
    case OP_CALLOUT_STR:
284
775
    cc += GET(cc, 1 + 2*LINK_SIZE);
285
775
    break;
286
287
    /* Skip over a subpattern that has a {0} or {0,x} quantifier */
288
289
2.94k
    case OP_BRAZERO:
290
4.00k
    case OP_BRAMINZERO:
291
4.78k
    case OP_BRAPOSZERO:
292
5.09k
    case OP_SKIPZERO:
293
5.09k
    cc += PRIV(OP_lengths)[*cc];
294
5.44k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
295
5.09k
    cc += 1 + LINK_SIZE;
296
5.09k
    break;
297
298
    /* Handle literal characters and + repetitions */
299
300
501k
    case OP_CHAR:
301
531k
    case OP_CHARI:
302
532k
    case OP_NOT:
303
532k
    case OP_NOTI:
304
535k
    case OP_PLUS:
305
536k
    case OP_PLUSI:
306
536k
    case OP_MINPLUS:
307
537k
    case OP_MINPLUSI:
308
538k
    case OP_POSPLUS:
309
540k
    case OP_POSPLUSI:
310
541k
    case OP_NOTPLUS:
311
541k
    case OP_NOTPLUSI:
312
542k
    case OP_NOTMINPLUS:
313
543k
    case OP_NOTMINPLUSI:
314
543k
    case OP_NOTPOSPLUS:
315
544k
    case OP_NOTPOSPLUSI:
316
544k
    branchlength++;
317
544k
    cc += 2;
318
544k
#ifdef SUPPORT_UNICODE
319
544k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
320
544k
#endif
321
544k
    break;
322
323
1.71k
    case OP_TYPEPLUS:
324
2.84k
    case OP_TYPEMINPLUS:
325
3.37k
    case OP_TYPEPOSPLUS:
326
3.37k
    branchlength++;
327
3.37k
    cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
328
3.37k
    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
2.05k
    case OP_EXACT:
334
2.42k
    case OP_EXACTI:
335
2.75k
    case OP_NOTEXACT:
336
3.20k
    case OP_NOTEXACTI:
337
3.20k
    branchlength += GET2(cc,1);
338
3.20k
    cc += 2 + IMM2_SIZE;
339
3.20k
#ifdef SUPPORT_UNICODE
340
3.20k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
341
3.20k
#endif
342
3.20k
    break;
343
344
1.74k
    case OP_TYPEEXACT:
345
1.74k
    branchlength += GET2(cc,1);
346
1.74k
    cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
347
1.25k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
348
1.74k
    break;
349
350
    /* Handle single-char non-literal matchers */
351
352
2.12k
    case OP_PROP:
353
5.05k
    case OP_NOTPROP:
354
5.05k
    cc += 2;
355
5.05k
    PCRE2_FALLTHROUGH /* Fall through */
356
357
6.76k
    case OP_NOT_DIGIT:
358
7.25k
    case OP_DIGIT:
359
7.81k
    case OP_NOT_WHITESPACE:
360
9.09k
    case OP_WHITESPACE:
361
11.4k
    case OP_NOT_WORDCHAR:
362
12.9k
    case OP_WORDCHAR:
363
14.8k
    case OP_ANY:
364
15.8k
    case OP_ALLANY:
365
16.3k
    case OP_EXTUNI:
366
17.0k
    case OP_HSPACE:
367
18.0k
    case OP_NOT_HSPACE:
368
18.9k
    case OP_VSPACE:
369
19.5k
    case OP_NOT_VSPACE:
370
19.5k
    branchlength++;
371
19.5k
    cc++;
372
19.5k
    break;
373
374
    /* "Any newline" might match two characters, but it also might match just
375
    one. */
376
377
1.17k
    case OP_ANYNL:
378
1.17k
    branchlength += 1;
379
1.17k
    cc++;
380
1.17k
    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
12
    case OP_ANYBYTE:
387
12
#ifdef SUPPORT_UNICODE
388
12
    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
21.1k
    case OP_TYPESTAR:
398
22.1k
    case OP_TYPEMINSTAR:
399
23.4k
    case OP_TYPEQUERY:
400
24.0k
    case OP_TYPEMINQUERY:
401
24.7k
    case OP_TYPEPOSSTAR:
402
25.2k
    case OP_TYPEPOSQUERY:
403
25.2k
    if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
404
25.2k
    cc += PRIV(OP_lengths)[op];
405
25.2k
    break;
406
407
744
    case OP_TYPEUPTO:
408
1.01k
    case OP_TYPEMINUPTO:
409
2.02k
    case OP_TYPEPOSUPTO:
410
2.02k
    if (cc[1 + IMM2_SIZE] == OP_PROP
411
1.20k
      || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
412
2.02k
    cc += PRIV(OP_lengths)[op];
413
2.02k
    break;
414
415
    /* Check a class for variable quantification */
416
417
11.5k
    case OP_CLASS:
418
17.2k
    case OP_NCLASS:
419
17.2k
#ifdef SUPPORT_WIDE_CHARS
420
23.8k
    case OP_XCLASS:
421
26.1k
    case OP_ECLASS:
422
    /* The original code caused an unsigned overflow in 64 bit systems,
423
    so now we use a conditional statement. */
424
26.1k
    if (op == OP_XCLASS || op == OP_ECLASS)
425
8.98k
      cc += GET(cc, 1);
426
17.2k
    else
427
17.2k
#endif
428
17.2k
      cc += PRIV(OP_lengths)[OP_CLASS];
429
430
26.1k
    switch (*cc)
431
26.1k
      {
432
1.08k
      case OP_CRPLUS:
433
1.39k
      case OP_CRMINPLUS:
434
2.89k
      case OP_CRPOSPLUS:
435
2.89k
      branchlength++;
436
2.89k
      PCRE2_FALLTHROUGH /* Fall through */
437
438
4.07k
      case OP_CRSTAR:
439
4.39k
      case OP_CRMINSTAR:
440
5.18k
      case OP_CRQUERY:
441
6.01k
      case OP_CRMINQUERY:
442
6.99k
      case OP_CRPOSSTAR:
443
8.03k
      case OP_CRPOSQUERY:
444
8.03k
      cc++;
445
8.03k
      break;
446
447
662
      case OP_CRRANGE:
448
909
      case OP_CRMINRANGE:
449
1.39k
      case OP_CRPOSRANGE:
450
1.39k
      branchlength += GET2(cc,1);
451
1.39k
      cc += 1 + 2 * IMM2_SIZE;
452
1.39k
      break;
453
454
16.7k
      default:
455
16.7k
      branchlength++;
456
16.7k
      break;
457
26.1k
      }
458
26.1k
    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
26.1k
    case OP_DNREF:
478
4.54k
    case OP_DNREFI:
479
4.54k
    if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
480
3.93k
      {
481
3.93k
      int count = GET2(cc, 1+IMM2_SIZE);
482
3.93k
      PCRE2_SPTR slot =
483
3.93k
        (PCRE2_SPTR)((const uint8_t *)re + sizeof(pcre2_real_code)) +
484
3.93k
          GET2(cc, 1) * re->name_entry_size;
485
486
3.93k
      d = INT_MAX;
487
488
      /* Scan all groups with the same name; find the shortest. */
489
490
14.6k
      while (count-- > 0)
491
13.5k
        {
492
13.5k
        int dd, i;
493
13.5k
        recno = GET2(slot, 0);
494
495
13.5k
        if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
496
9.12k
          dd = backref_cache[recno];
497
4.37k
        else
498
4.37k
          {
499
4.37k
          ce = cs = PRIV(find_bracket)(startcode, utf, recno);
500
4.37k
          if (cs == NULL) return -2;
501
4.58k
          do ce += GET(ce, 1); while (*ce == OP_ALT);
502
503
4.37k
          dd = 0;
504
4.37k
          if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
505
4.37k
            {
506
4.37k
            if (cc > cs && cc < ce)    /* Simple recursion */
507
474
              {
508
474
              had_recurse = TRUE;
509
474
              }
510
3.89k
            else
511
3.89k
              {
512
3.89k
              recurse_check *r = recurses;
513
12.9k
              for (r = recurses; r != NULL; r = r->prev)
514
9.67k
                if (r->group == cs) break;
515
3.89k
              if (r != NULL)           /* Mutual recursion */
516
615
                {
517
615
                had_recurse = TRUE;
518
615
                }
519
3.28k
              else
520
3.28k
                {
521
3.28k
                this_recurse.prev = recurses;  /* No recursion */
522
3.28k
                this_recurse.group = cs;
523
3.28k
                dd = find_minlength(re, cs, startcode, utf, &this_recurse,
524
3.28k
                  countptr, backref_cache);
525
3.28k
                if (dd < 0) return dd;
526
3.28k
                }
527
3.89k
              }
528
4.37k
            }
529
530
4.35k
          backref_cache[recno] = dd;
531
11.2k
          for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
532
4.35k
          backref_cache[0] = recno;
533
4.35k
          }
534
535
13.4k
        if (dd < d) d = dd;
536
13.4k
        if (d <= 0) break;    /* No point looking at any more */
537
10.7k
        slot += re->name_entry_size;
538
10.7k
        }
539
3.93k
      }
540
612
    else d = 0;
541
4.53k
    cc += PRIV(OP_lengths)[*cc];
542
4.53k
    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
17.0k
    case OP_REF:
548
17.4k
    case OP_REFI:
549
17.4k
    recno = GET2(cc, 1);
550
17.4k
    if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
551
9.24k
      d = backref_cache[recno];
552
8.21k
    else
553
8.21k
      {
554
8.21k
      int i;
555
8.21k
      d = 0;
556
557
8.21k
      if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
558
8.21k
        {
559
8.21k
        ce = cs = PRIV(find_bracket)(startcode, utf, recno);
560
8.21k
        if (cs == NULL) return -2;
561
8.42k
        do ce += GET(ce, 1); while (*ce == OP_ALT);
562
563
8.21k
        if (!dupcapused || PRIV(find_bracket)(ce, utf, recno) == NULL)
564
8.00k
          {
565
8.00k
          if (cc > cs && cc < ce)    /* Simple recursion */
566
650
            {
567
650
            had_recurse = TRUE;
568
650
            }
569
7.35k
          else
570
7.35k
            {
571
7.35k
            recurse_check *r = recurses;
572
52.9k
            for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
573
7.35k
            if (r != NULL)           /* Mutual recursion */
574
541
              {
575
541
              had_recurse = TRUE;
576
541
              }
577
6.81k
            else                     /* No recursion */
578
6.81k
              {
579
6.81k
              this_recurse.prev = recurses;
580
6.81k
              this_recurse.group = cs;
581
6.81k
              d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
582
6.81k
                backref_cache);
583
6.81k
              if (d < 0) return d;
584
6.81k
              }
585
7.35k
            }
586
8.00k
          }
587
8.21k
        }
588
589
8.14k
      backref_cache[recno] = d;
590
13.5k
      for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
591
8.14k
      backref_cache[0] = recno;
592
8.14k
      }
593
594
17.3k
    cc += PRIV(OP_lengths)[*cc];
595
596
    /* Handle repeated back references */
597
598
21.9k
    REPEAT_BACK_REFERENCE:
599
21.9k
    switch (*cc)
600
21.9k
      {
601
422
      case OP_CRSTAR:
602
1.06k
      case OP_CRMINSTAR:
603
1.71k
      case OP_CRQUERY:
604
2.01k
      case OP_CRMINQUERY:
605
2.01k
      case OP_CRPOSSTAR:
606
2.01k
      case OP_CRPOSQUERY:
607
2.01k
      min = 0;
608
2.01k
      cc++;
609
2.01k
      break;
610
611
364
      case OP_CRPLUS:
612
664
      case OP_CRMINPLUS:
613
664
      case OP_CRPOSPLUS:
614
664
      min = 1;
615
664
      cc++;
616
664
      break;
617
618
2.68k
      case OP_CRRANGE:
619
2.93k
      case OP_CRMINRANGE:
620
2.93k
      case OP_CRPOSRANGE:
621
2.93k
      min = GET2(cc, 1);
622
2.93k
      cc += 1 + 2 * IMM2_SIZE;
623
2.93k
      break;
624
625
16.3k
      default:
626
16.3k
      min = 1;
627
16.3k
      break;
628
21.9k
      }
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
21.9k
    if ((d > 0 && (INT_MAX/d) < min) || (int)UINT16_MAX - branchlength < min*d)
635
1.78k
      branchlength = UINT16_MAX;
636
20.1k
    else branchlength += min * d;
637
21.9k
    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
19.6k
    case OP_RECURSE:
644
19.6k
    cs = ce = startcode + GET(cc, 1);
645
19.6k
    recno = GET2(cs, 1+LINK_SIZE);
646
19.6k
    if (recno == prev_recurse_recno)
647
2.45k
      {
648
2.45k
      branchlength += prev_recurse_d;
649
2.45k
      }
650
17.1k
    else
651
17.1k
      {
652
18.6k
      do ce += GET(ce, 1); while (*ce == OP_ALT);
653
17.1k
      if (cc > cs && cc < ce)    /* Simple recursion */
654
9.99k
        had_recurse = TRUE;
655
7.20k
      else
656
7.20k
        {
657
7.20k
        recurse_check *r = recurses;
658
122k
        for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
659
7.20k
        if (r != NULL)          /* Mutual recursion */
660
946
          had_recurse = TRUE;
661
6.26k
        else
662
6.26k
          {
663
6.26k
          this_recurse.prev = recurses;
664
6.26k
          this_recurse.group = cs;
665
6.26k
          prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
666
6.26k
            countptr, backref_cache);
667
6.26k
          if (prev_recurse_d < 0) return prev_recurse_d;
668
6.05k
          prev_recurse_recno = recno;
669
6.05k
          branchlength += prev_recurse_d;
670
6.05k
          }
671
7.20k
        }
672
17.1k
      }
673
19.4k
    cc += 1 + LINK_SIZE + once_fudge;
674
19.4k
    once_fudge = 0;
675
19.4k
    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
298
    case OP_UPTO:
686
612
    case OP_UPTOI:
687
910
    case OP_NOTUPTO:
688
1.69k
    case OP_NOTUPTOI:
689
1.94k
    case OP_MINUPTO:
690
2.21k
    case OP_MINUPTOI:
691
2.49k
    case OP_NOTMINUPTO:
692
3.75k
    case OP_NOTMINUPTOI:
693
4.58k
    case OP_POSUPTO:
694
4.96k
    case OP_POSUPTOI:
695
5.30k
    case OP_NOTPOSUPTO:
696
6.25k
    case OP_NOTPOSUPTOI:
697
698
7.99k
    case OP_STAR:
699
18.7k
    case OP_STARI:
700
19.2k
    case OP_NOTSTAR:
701
19.5k
    case OP_NOTSTARI:
702
20.1k
    case OP_MINSTAR:
703
21.2k
    case OP_MINSTARI:
704
22.0k
    case OP_NOTMINSTAR:
705
22.3k
    case OP_NOTMINSTARI:
706
25.1k
    case OP_POSSTAR:
707
25.5k
    case OP_POSSTARI:
708
25.7k
    case OP_NOTPOSSTAR:
709
26.0k
    case OP_NOTPOSSTARI:
710
711
28.6k
    case OP_QUERY:
712
29.2k
    case OP_QUERYI:
713
29.6k
    case OP_NOTQUERY:
714
30.7k
    case OP_NOTQUERYI:
715
31.3k
    case OP_MINQUERY:
716
31.6k
    case OP_MINQUERYI:
717
32.4k
    case OP_NOTMINQUERY:
718
32.7k
    case OP_NOTMINQUERYI:
719
35.3k
    case OP_POSQUERY:
720
37.2k
    case OP_POSQUERYI:
721
37.8k
    case OP_NOTPOSQUERY:
722
38.6k
    case OP_NOTPOSQUERYI:
723
724
38.6k
    cc += PRIV(OP_lengths)[op];
725
38.6k
#ifdef SUPPORT_UNICODE
726
38.6k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
727
38.6k
#endif
728
38.6k
    break;
729
730
    /* Skip these, but we need to add in the name length. */
731
732
883
    case OP_MARK:
733
1.25k
    case OP_COMMIT_ARG:
734
1.91k
    case OP_PRUNE_ARG:
735
2.53k
    case OP_SKIP_ARG:
736
3.40k
    case OP_THEN_ARG:
737
3.40k
    cc += PRIV(OP_lengths)[op] + cc[1];
738
3.40k
    break;
739
740
    /* The remaining opcodes are just skipped over. */
741
742
0
    case OP_CLOSE:
743
339
    case OP_COMMIT:
744
854
    case OP_FAIL:
745
1.31k
    case OP_PRUNE:
746
1.91k
    case OP_SET_SOM:
747
2.40k
    case OP_SKIP:
748
3.27k
    case OP_THEN:
749
3.27k
    cc += PRIV(OP_lengths)[op];
750
3.27k
    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
1.35M
    }
761
1.35M
  }
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
205k
}
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
48.0k
{
792
48.0k
uint32_t c = *p++;   /* First code unit */
793
794
48.0k
(void)utf;           /* Stop compiler warnings when UTF not supported */
795
48.0k
(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
48.0k
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
48.0k
#ifdef SUPPORT_UNICODE
810
48.0k
if (utf)
811
5.87k
  {
812
5.87k
#if PCRE2_CODE_UNIT_WIDTH == 8
813
5.87k
  if (c >= 0xc0) GETUTF8INC(c, p);
814
#elif PCRE2_CODE_UNIT_WIDTH == 16
815
  if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
816
#endif
817
5.87k
  }
818
48.0k
#endif  /* SUPPORT_UNICODE */
819
820
/* If caseless, handle the other case of the character. */
821
822
48.0k
if (caseless)
823
12.6k
  {
824
12.6k
#ifdef SUPPORT_UNICODE
825
12.6k
  if (utf || ucp)
826
1.42k
    {
827
1.42k
    c = UCD_OTHERCASE(c);
828
1.42k
#if PCRE2_CODE_UNIT_WIDTH == 8
829
1.42k
    if (utf)
830
799
      {
831
799
      PCRE2_UCHAR buff[6];
832
799
      (void)PRIV(ord2utf)(c, buff);
833
799
      SET_BIT(buff[0]);
834
799
      }
835
626
    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
1.42k
    }
840
841
11.2k
  else
842
11.2k
#endif  /* SUPPORT_UNICODE */
843
844
  /* Not UTF or UCP */
845
846
11.2k
  if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
847
12.6k
  }
848
849
48.0k
return p;
850
48.0k
}
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
20.3k
{
876
20.3k
uint32_t c;
877
463k
for (c = 0; c < table_limit; c++)
878
442k
  re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
879
20.3k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
880
20.3k
if (table_limit == 32) return;
881
1.68M
for (c = 128; c < 256; c++)
882
1.67M
  {
883
1.67M
  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
1.67M
  }
890
13.0k
#endif  /* UTF-8 */
891
13.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
9.07k
{
917
9.07k
uint32_t c;
918
237k
for (c = 0; c < table_limit; c++)
919
228k
  re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type]));
920
9.07k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
921
34.5k
if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
922
9.07k
#endif
923
9.07k
}
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
4.07k
{
946
4.07k
uint32_t type, list_ind;
947
4.07k
uint32_t char_list_add = XCL_CHAR_LIST_LOW_16_ADD;
948
4.07k
uint32_t range_start = ~(uint32_t)0, range_end = 0;
949
4.07k
const uint8_t *next_char;
950
4.07k
PCRE2_UCHAR start_buffer[6], end_buffer[6];
951
4.07k
PCRE2_UCHAR start, end;
952
953
/* Only needed in 8-bit mode at the moment. */
954
4.07k
type = (uint32_t)(code[0] << 8) | code[1];
955
4.07k
code += 2;
956
957
/* Align characters. */
958
4.07k
next_char = char_lists_end - (GET(code, 0) << 1);
959
4.07k
type &= XCL_TYPE_MASK;
960
4.07k
list_ind = 0;
961
962
4.07k
if ((type & XCL_BEGIN_WITH_RANGE) != 0)
963
862
  range_start = XCL_CHAR_LIST_LOW_16_START;
964
965
11.7k
while (type > 0)
966
7.68k
  {
967
7.68k
  uint32_t item_count = type & XCL_ITEM_COUNT_MASK;
968
969
7.68k
  if (item_count == XCL_ITEM_COUNT_MASK)
970
4.23k
    {
971
4.23k
    if (list_ind <= 1)
972
3.90k
      {
973
3.90k
      item_count = *(const uint16_t*)next_char;
974
3.90k
      next_char += 2;
975
3.90k
      }
976
332
    else
977
332
      {
978
332
      item_count = *(const uint32_t*)next_char;
979
332
      next_char += 4;
980
332
      }
981
4.23k
    }
982
983
51.2k
  while (item_count > 0)
984
43.6k
    {
985
43.6k
    if (list_ind <= 1)
986
40.0k
      {
987
40.0k
      range_end = *(const uint16_t*)next_char;
988
40.0k
      next_char += 2;
989
40.0k
      }
990
3.56k
    else
991
3.56k
      {
992
3.56k
      range_end = *(const uint32_t*)next_char;
993
3.56k
      next_char += 4;
994
3.56k
      }
995
996
43.6k
    if ((range_end & XCL_CHAR_END) != 0)
997
30.4k
      {
998
30.4k
      range_end = char_list_add + (range_end >> XCL_CHAR_SHIFT);
999
1000
30.4k
      PRIV(ord2utf)(range_end, end_buffer);
1001
30.4k
      end = end_buffer[0];
1002
1003
30.4k
      if (range_start < range_end)
1004
13.5k
        {
1005
13.5k
        PRIV(ord2utf)(range_start, start_buffer);
1006
63.9k
        for (start = start_buffer[0]; start <= end; start++)
1007
50.3k
          start_bitmap[start / 8] |= (1u << (start & 7));
1008
13.5k
        }
1009
16.9k
      else
1010
16.9k
        start_bitmap[end / 8] |= (1u << (end & 7));
1011
1012
30.4k
      range_start = ~(uint32_t)0;
1013
30.4k
      }
1014
13.1k
    else
1015
13.1k
      range_start = char_list_add + (range_end >> XCL_CHAR_SHIFT);
1016
1017
43.6k
    item_count--;
1018
43.6k
    }
1019
1020
7.68k
  list_ind++;
1021
7.68k
  type >>= XCL_TYPE_BIT_LEN;
1022
1023
7.68k
  if (range_start == ~(uint32_t)0)
1024
4.86k
    {
1025
4.86k
    if ((type & XCL_BEGIN_WITH_RANGE) != 0)
1026
881
      {
1027
      /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_START is not possible. */
1028
881
      if (list_ind == 1) range_start = XCL_CHAR_LIST_HIGH_16_START;
1029
238
      else range_start = XCL_CHAR_LIST_LOW_32_START;
1030
881
      }
1031
4.86k
    }
1032
2.82k
  else if ((type & XCL_BEGIN_WITH_RANGE) == 0)
1033
1.29k
    {
1034
1.29k
    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.29k
    if (list_ind == 1) range_end = XCL_CHAR_LIST_LOW_16_END;
1039
290
    else range_end = XCL_CHAR_LIST_HIGH_16_END;
1040
1041
1.29k
    PRIV(ord2utf)(range_end, end_buffer);
1042
1.29k
    end = end_buffer[0];
1043
1044
2.66k
    for (start = start_buffer[0]; start <= end; start++)
1045
1.37k
      start_bitmap[start / 8] |= (1u << (start & 7));
1046
1047
1.29k
    range_start = ~(uint32_t)0;
1048
1.29k
    }
1049
1050
  /* In 8 bit mode XCL_CHAR_LIST_HIGH_32_ADD is not possible. */
1051
7.68k
  if (list_ind == 1) char_list_add = XCL_CHAR_LIST_HIGH_16_ADD;
1052
3.60k
  else char_list_add = XCL_CHAR_LIST_LOW_32_ADD;
1053
7.68k
  }
1054
4.07k
}
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
200k
{
1096
200k
uint32_t c;
1097
200k
int yield = SSB_DONE;
1098
200k
BOOL done;
1099
1100
200k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1101
200k
int table_limit = utf? 16:32;
1102
#else
1103
int table_limit = 32;
1104
#endif
1105
1106
200k
*depthptr += 1;
1107
200k
if (*depthptr > 1000) return SSB_TOODEEP;
1108
1109
200k
do
1110
251k
  {
1111
251k
  BOOL try_next = TRUE;
1112
251k
  PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
1113
1114
251k
  if (*code == OP_CBRA || *code == OP_SCBRA ||
1115
141k
      *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
1116
1117
592k
  while (try_next)    /* Loop for items in this branch */
1118
521k
    {
1119
521k
    int rc;
1120
521k
    PCRE2_SPTR ncode;
1121
521k
    const uint8_t *classmap = NULL;
1122
521k
#ifdef SUPPORT_WIDE_CHARS
1123
521k
    PCRE2_UCHAR xclassflags;
1124
521k
#endif
1125
1126
521k
    switch(*tcode)
1127
521k
      {
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
23
      case OP_ACCEPT:
1138
24
      case OP_ASSERT_ACCEPT:
1139
88
      case OP_ALLANY:
1140
514
      case OP_ANY:
1141
524
      case OP_ANYBYTE:
1142
528
      case OP_CIRCM:
1143
544
      case OP_CLOSE:
1144
553
      case OP_COMMIT:
1145
585
      case OP_COMMIT_ARG:
1146
771
      case OP_COND:
1147
776
      case OP_CREF:
1148
776
      case OP_FALSE:
1149
776
      case OP_TRUE:
1150
777
      case OP_DNCREF:
1151
857
      case OP_DNREF:
1152
870
      case OP_DNREFI:
1153
871
      case OP_DNRREF:
1154
980
      case OP_DOLL:
1155
985
      case OP_DOLLM:
1156
985
      case OP_END:
1157
999
      case OP_EOD:
1158
1.01k
      case OP_EODN:
1159
1.02k
      case OP_EXTUNI:
1160
1.05k
      case OP_FAIL:
1161
1.11k
      case OP_MARK:
1162
1.16k
      case OP_NOT:
1163
1.18k
      case OP_NOTEXACT:
1164
1.19k
      case OP_NOTEXACTI:
1165
1.21k
      case OP_NOTI:
1166
1.21k
      case OP_NOTMINPLUS:
1167
1.22k
      case OP_NOTMINPLUSI:
1168
1.23k
      case OP_NOTMINQUERY:
1169
1.24k
      case OP_NOTMINQUERYI:
1170
1.24k
      case OP_NOTMINSTAR:
1171
1.25k
      case OP_NOTMINSTARI:
1172
1.26k
      case OP_NOTMINUPTO:
1173
1.27k
      case OP_NOTMINUPTOI:
1174
1.28k
      case OP_NOTPLUS:
1175
1.29k
      case OP_NOTPLUSI:
1176
1.30k
      case OP_NOTPOSPLUS:
1177
1.31k
      case OP_NOTPOSPLUSI:
1178
1.32k
      case OP_NOTPOSQUERY:
1179
1.33k
      case OP_NOTPOSQUERYI:
1180
1.34k
      case OP_NOTPOSSTAR:
1181
1.34k
      case OP_NOTPOSSTARI:
1182
1.35k
      case OP_NOTPOSUPTO:
1183
1.36k
      case OP_NOTPOSUPTOI:
1184
1.61k
      case OP_NOTPROP:
1185
1.62k
      case OP_NOTQUERY:
1186
1.64k
      case OP_NOTQUERYI:
1187
1.65k
      case OP_NOTSTAR:
1188
1.66k
      case OP_NOTSTARI:
1189
1.66k
      case OP_NOTUPTO:
1190
1.67k
      case OP_NOTUPTOI:
1191
1.84k
      case OP_NOT_HSPACE:
1192
1.90k
      case OP_NOT_VSPACE:
1193
1.91k
      case OP_PRUNE:
1194
1.94k
      case OP_PRUNE_ARG:
1195
2.40k
      case OP_RECURSE:
1196
2.69k
      case OP_REF:
1197
2.71k
      case OP_REFI:
1198
2.71k
      case OP_REVERSE:
1199
2.71k
      case OP_VREVERSE:
1200
2.72k
      case OP_RREF:
1201
2.75k
      case OP_SCOND:
1202
2.76k
      case OP_SET_SOM:
1203
2.77k
      case OP_SKIP:
1204
2.80k
      case OP_SKIP_ARG:
1205
2.81k
      case OP_SOD:
1206
2.81k
      case OP_SOM:
1207
2.82k
      case OP_THEN:
1208
2.85k
      case OP_THEN_ARG:
1209
2.85k
      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
1.58k
      case OP_CIRC:
1215
1.58k
      tcode += PRIV(OP_lengths)[OP_CIRC];
1216
1.58k
      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
1.68k
      case OP_PROP:
1224
1.68k
      if (tcode[1] != PT_CLIST) return SSB_FAIL;
1225
1.33k
        {
1226
1.33k
        const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
1227
5.32k
        while ((c = *p++) < NOTACHAR)
1228
3.99k
          {
1229
3.99k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1230
3.99k
          if (utf)
1231
2.05k
            {
1232
2.05k
            PCRE2_UCHAR buff[6];
1233
2.05k
            (void)PRIV(ord2utf)(c, buff);
1234
2.05k
            c = buff[0];
1235
2.05k
            }
1236
3.99k
#endif
1237
3.99k
          if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
1238
3.99k
          }
1239
1.33k
        }
1240
1.33k
      try_next = FALSE;
1241
1.33k
      break;
1242
1243
      /* We can ignore word boundary tests. */
1244
1245
1.01k
      case OP_WORD_BOUNDARY:
1246
1.96k
      case OP_NOT_WORD_BOUNDARY:
1247
3.17k
      case OP_UCP_WORD_BOUNDARY:
1248
3.97k
      case OP_NOT_UCP_WORD_BOUNDARY:
1249
3.97k
      tcode++;
1250
3.97k
      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
5.16k
      case OP_ASSERT:
1258
15.5k
      case OP_ASSERT_NA:
1259
15.5k
      ncode = tcode + GET(tcode, 1);
1260
18.1k
      while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1261
15.5k
      ncode += 1 + LINK_SIZE;
1262
1263
      /* Skip irrelevant items */
1264
1265
1.35M
      for (done = FALSE; !done;)
1266
1.33M
        {
1267
1.33M
        switch (*ncode)
1268
1.33M
          {
1269
463k
          case OP_ASSERT:
1270
463k
          case OP_ASSERT_NOT:
1271
464k
          case OP_ASSERTBACK:
1272
464k
          case OP_ASSERTBACK_NOT:
1273
1.31M
          case OP_ASSERT_NA:
1274
1.31M
          case OP_ASSERTBACK_NA:
1275
1.31M
          case OP_ASSERT_SCS:
1276
1.31M
          ncode += GET(ncode, 1);
1277
1.36M
          while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1278
1.31M
          ncode += 1 + LINK_SIZE;
1279
1.31M
          break;
1280
1281
619
          case OP_WORD_BOUNDARY:
1282
966
          case OP_NOT_WORD_BOUNDARY:
1283
1.25k
          case OP_UCP_WORD_BOUNDARY:
1284
1.59k
          case OP_NOT_UCP_WORD_BOUNDARY:
1285
1.59k
          ncode++;
1286
1.59k
          break;
1287
1288
570
          case OP_CALLOUT:
1289
570
          ncode += PRIV(OP_lengths)[OP_CALLOUT];
1290
570
          break;
1291
1292
584
          case OP_CALLOUT_STR:
1293
584
          ncode += GET(ncode, 1 + 2*LINK_SIZE);
1294
584
          break;
1295
1296
15.5k
          default:
1297
15.5k
          done = TRUE;
1298
15.5k
          break;
1299
1.33M
          }
1300
1.33M
        }
1301
1302
      /* Now check the next significant item. */
1303
1304
15.5k
      switch(*ncode)
1305
15.5k
        {
1306
8.11k
        default:
1307
8.11k
        break;
1308
1309
8.11k
        case OP_PROP:
1310
527
        if (ncode[1] != PT_CLIST) break;
1311
277
        PCRE2_FALLTHROUGH /* Fall through */
1312
605
        case OP_ANYNL:
1313
1.42k
        case OP_CHAR:
1314
2.11k
        case OP_CHARI:
1315
2.39k
        case OP_EXACT:
1316
2.60k
        case OP_EXACTI:
1317
2.94k
        case OP_HSPACE:
1318
3.26k
        case OP_MINPLUS:
1319
3.51k
        case OP_MINPLUSI:
1320
3.77k
        case OP_PLUS:
1321
4.26k
        case OP_PLUSI:
1322
4.55k
        case OP_POSPLUS:
1323
4.79k
        case OP_POSPLUSI:
1324
5.02k
        case OP_VSPACE:
1325
        /* Note that these types will only be present in non-UCP mode. */
1326
5.24k
        case OP_DIGIT:
1327
5.53k
        case OP_NOT_DIGIT:
1328
5.81k
        case OP_WORDCHAR:
1329
6.12k
        case OP_NOT_WORDCHAR:
1330
6.85k
        case OP_WHITESPACE:
1331
7.16k
        case OP_NOT_WHITESPACE:
1332
7.16k
        tcode = ncode;
1333
7.16k
        continue;   /* With the following significant opcode */
1334
15.5k
        }
1335
8.36k
      PCRE2_FALLTHROUGH /* Fall through */
1336
8.36k
1337
8.36k
      /* For a group bracket or a positive assertion without an immediately
1338
8.36k
      following mandatory setting, recurse to set bits from within the
1339
8.36k
      subpattern. If it can't find anything, we have to give up. If it finds
1340
8.36k
      some mandatory character(s), we are done for this branch. Otherwise,
1341
8.36k
      carry on scanning after the subpattern. */
1342
8.36k
1343
24.7k
      case OP_BRA:
1344
25.0k
      case OP_SBRA:
1345
162k
      case OP_CBRA:
1346
162k
      case OP_SCBRA:
1347
163k
      case OP_BRAPOS:
1348
163k
      case OP_SBRAPOS:
1349
164k
      case OP_CBRAPOS:
1350
165k
      case OP_SCBRAPOS:
1351
167k
      case OP_ONCE:
1352
168k
      case OP_SCRIPT_RUN:
1353
168k
      rc = set_start_bits(re, tcode, utf, ucp, depthptr);
1354
168k
      if (rc == SSB_DONE)
1355
4.63k
        {
1356
4.63k
        try_next = FALSE;
1357
4.63k
        }
1358
164k
      else if (rc == SSB_CONTINUE)
1359
160k
        {
1360
207k
        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1361
160k
        tcode += 1 + LINK_SIZE;
1362
160k
        }
1363
3.24k
      else return rc;   /* FAIL, UNKNOWN, or TOODEEP */
1364
165k
      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
165k
      case OP_ALT:
1374
18.2k
      yield = SSB_CONTINUE;
1375
18.2k
      try_next = FALSE;
1376
18.2k
      break;
1377
1378
164k
      case OP_KET:
1379
166k
      case OP_KETRMAX:
1380
166k
      case OP_KETRMIN:
1381
168k
      case OP_KETRPOS:
1382
168k
      return SSB_CONTINUE;
1383
1384
      /* Skip over callout */
1385
1386
1.18k
      case OP_CALLOUT:
1387
1.18k
      tcode += PRIV(OP_lengths)[OP_CALLOUT];
1388
1.18k
      break;
1389
1390
2.53k
      case OP_CALLOUT_STR:
1391
2.53k
      tcode += GET(tcode, 1 + 2*LINK_SIZE);
1392
2.53k
      break;
1393
1394
      /* Skip over lookbehind, negative lookahead, and scan substring
1395
      assertions */
1396
1397
8.06k
      case OP_ASSERT_NOT:
1398
11.0k
      case OP_ASSERTBACK:
1399
13.3k
      case OP_ASSERTBACK_NOT:
1400
14.7k
      case OP_ASSERTBACK_NA:
1401
15.3k
      case OP_ASSERT_SCS:
1402
16.7k
      do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1403
15.3k
      tcode += 1 + LINK_SIZE;
1404
15.3k
      break;
1405
1406
      /* BRAZERO does the bracket, but carries on. */
1407
1408
17.9k
      case OP_BRAZERO:
1409
18.8k
      case OP_BRAMINZERO:
1410
19.4k
      case OP_BRAPOSZERO:
1411
19.4k
      rc = set_start_bits(re, ++tcode, utf, ucp, depthptr);
1412
19.4k
      if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc;
1413
15.0k
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1414
14.7k
      tcode += 1 + LINK_SIZE;
1415
14.7k
      break;
1416
1417
      /* SKIPZERO skips the bracket. */
1418
1419
659
      case OP_SKIPZERO:
1420
659
      tcode++;
1421
990
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1422
659
      tcode += 1 + LINK_SIZE;
1423
659
      break;
1424
1425
      /* Single-char * or ? sets the bit and tries the next item */
1426
1427
7.56k
      case OP_STAR:
1428
8.66k
      case OP_MINSTAR:
1429
9.93k
      case OP_POSSTAR:
1430
19.4k
      case OP_QUERY:
1431
19.8k
      case OP_MINQUERY:
1432
21.4k
      case OP_POSQUERY:
1433
21.4k
      tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1434
21.4k
      break;
1435
1436
710
      case OP_STARI:
1437
1.98k
      case OP_MINSTARI:
1438
2.60k
      case OP_POSSTARI:
1439
4.19k
      case OP_QUERYI:
1440
4.59k
      case OP_MINQUERYI:
1441
4.98k
      case OP_POSQUERYI:
1442
4.98k
      tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1443
4.98k
      break;
1444
1445
      /* Single-char upto sets the bit and tries the next */
1446
1447
504
      case OP_UPTO:
1448
1.69k
      case OP_MINUPTO:
1449
2.66k
      case OP_POSUPTO:
1450
2.66k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp);
1451
2.66k
      break;
1452
1453
681
      case OP_UPTOI:
1454
991
      case OP_MINUPTOI:
1455
1.29k
      case OP_POSUPTOI:
1456
1.29k
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp);
1457
1.29k
      break;
1458
1459
      /* At least one single char sets the bit and stops */
1460
1461
762
      case OP_EXACT:
1462
762
      tcode += IMM2_SIZE;
1463
762
      PCRE2_FALLTHROUGH /* Fall through */
1464
6.71k
      case OP_CHAR:
1465
8.22k
      case OP_PLUS:
1466
9.81k
      case OP_MINPLUS:
1467
11.2k
      case OP_POSPLUS:
1468
11.2k
      (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1469
11.2k
      try_next = FALSE;
1470
11.2k
      break;
1471
1472
882
      case OP_EXACTI:
1473
882
      tcode += IMM2_SIZE;
1474
882
      PCRE2_FALLTHROUGH /* Fall through */
1475
3.48k
      case OP_CHARI:
1476
5.01k
      case OP_PLUSI:
1477
5.85k
      case OP_MINPLUSI:
1478
6.39k
      case OP_POSPLUSI:
1479
6.39k
      (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1480
6.39k
      try_next = FALSE;
1481
6.39k
      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
1.94k
      case OP_HSPACE:
1490
1.94k
      SET_BIT(CHAR_HT);
1491
1.94k
      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
1.94k
#ifdef SUPPORT_UNICODE
1504
1.94k
      if (utf)
1505
989
        {
1506
989
        SET_BIT(0xC2);  /* For U+00A0 */
1507
989
        SET_BIT(0xE1);  /* For U+1680, U+180E */
1508
989
        SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1509
989
        SET_BIT(0xE3);  /* For U+3000 */
1510
989
        }
1511
959
      else
1512
959
#endif
1513
      /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1514
959
        {
1515
959
        SET_BIT(CHAR_NBSP);
1516
959
        }
1517
1.94k
#endif  /* 8-bit support */
1518
1519
1.94k
      try_next = FALSE;
1520
1.94k
      break;
1521
1522
1.18k
      case OP_ANYNL:
1523
2.81k
      case OP_VSPACE:
1524
2.81k
      SET_BIT(CHAR_LF);
1525
2.81k
      SET_BIT(CHAR_VT);
1526
2.81k
      SET_BIT(CHAR_FF);
1527
2.81k
      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
2.81k
#ifdef SUPPORT_UNICODE
1540
2.81k
      if (utf)
1541
389
        {
1542
389
        SET_BIT(0xC2);  /* For U+0085 (NEL) */
1543
389
        SET_BIT(0xE2);  /* For U+2028, U+2029 */
1544
389
        }
1545
2.42k
      else
1546
2.42k
#endif
1547
      /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1548
2.42k
        {
1549
2.42k
        SET_BIT(CHAR_NEL);
1550
2.42k
        }
1551
2.81k
#endif  /* 8-bit support */
1552
1553
2.81k
      try_next = FALSE;
1554
2.81k
      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.81k
      case OP_NOT_DIGIT:
1562
1.81k
      set_nottype_bits(re, cbit_digit, table_limit);
1563
1.81k
      try_next = FALSE;
1564
1.81k
      break;
1565
1566
3.99k
      case OP_DIGIT:
1567
3.99k
      set_type_bits(re, cbit_digit, table_limit);
1568
3.99k
      try_next = FALSE;
1569
3.99k
      break;
1570
1571
1.98k
      case OP_NOT_WHITESPACE:
1572
1.98k
      set_nottype_bits(re, cbit_space, table_limit);
1573
1.98k
      try_next = FALSE;
1574
1.98k
      break;
1575
1576
4.78k
      case OP_WHITESPACE:
1577
4.78k
      set_type_bits(re, cbit_space, table_limit);
1578
4.78k
      try_next = FALSE;
1579
4.78k
      break;
1580
1581
1.19k
      case OP_NOT_WORDCHAR:
1582
1.19k
      set_nottype_bits(re, cbit_word, table_limit);
1583
1.19k
      try_next = FALSE;
1584
1.19k
      break;
1585
1586
3.36k
      case OP_WORDCHAR:
1587
3.36k
      set_type_bits(re, cbit_word, table_limit);
1588
3.36k
      try_next = FALSE;
1589
3.36k
      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
859
      case OP_TYPEPLUS:
1595
1.97k
      case OP_TYPEMINPLUS:
1596
2.39k
      case OP_TYPEPOSPLUS:
1597
2.39k
      tcode++;
1598
2.39k
      break;
1599
1600
418
      case OP_TYPEEXACT:
1601
418
      tcode += 1 + IMM2_SIZE;
1602
418
      break;
1603
1604
      /* Zero or more repeats of character types set the bits and then
1605
      try again. */
1606
1607
384
      case OP_TYPEUPTO:
1608
719
      case OP_TYPEMINUPTO:
1609
1.02k
      case OP_TYPEPOSUPTO:
1610
1.02k
      tcode += IMM2_SIZE;
1611
1.02k
      PCRE2_FALLTHROUGH /* Fall through */
1612
1613
10.4k
      case OP_TYPESTAR:
1614
11.2k
      case OP_TYPEMINSTAR:
1615
12.0k
      case OP_TYPEPOSSTAR:
1616
17.7k
      case OP_TYPEQUERY:
1617
18.1k
      case OP_TYPEMINQUERY:
1618
18.6k
      case OP_TYPEPOSQUERY:
1619
18.6k
      switch(tcode[1])
1620
18.6k
        {
1621
296
        default:
1622
449
        case OP_ANY:
1623
481
        case OP_ALLANY:
1624
481
        return SSB_FAIL;
1625
1626
2.10k
        case OP_HSPACE:
1627
2.10k
        SET_BIT(CHAR_HT);
1628
2.10k
        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.10k
#ifdef SUPPORT_UNICODE
1641
2.10k
        if (utf)
1642
1.03k
          {
1643
1.03k
          SET_BIT(0xC2);  /* For U+00A0 */
1644
1.03k
          SET_BIT(0xE1);  /* For U+1680, U+180E */
1645
1.03k
          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1646
1.03k
          SET_BIT(0xE3);  /* For U+3000 */
1647
1.03k
          }
1648
1.06k
        else
1649
1.06k
#endif
1650
        /* For the 8-bit library not in UTF-8 mode, set the bit for NBSP. */
1651
1.06k
          {
1652
1.06k
          SET_BIT(CHAR_NBSP);
1653
1.06k
          }
1654
2.10k
#endif  /* 8-bit support */
1655
2.10k
        break;
1656
1657
2.58k
        case OP_ANYNL:
1658
3.74k
        case OP_VSPACE:
1659
3.74k
        SET_BIT(CHAR_LF);
1660
3.74k
        SET_BIT(CHAR_VT);
1661
3.74k
        SET_BIT(CHAR_FF);
1662
3.74k
        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.74k
#ifdef SUPPORT_UNICODE
1675
3.74k
        if (utf)
1676
1.09k
          {
1677
1.09k
          SET_BIT(0xC2);  /* For U+0085 (NEL) */
1678
1.09k
          SET_BIT(0xE2);  /* For U+2028, U+2029 */
1679
1.09k
          }
1680
2.64k
        else
1681
2.64k
#endif
1682
        /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1683
2.64k
          {
1684
2.64k
          SET_BIT(CHAR_NEL);
1685
2.64k
          }
1686
3.74k
#endif  /* 8-bit support */
1687
3.74k
        break;
1688
1689
1.12k
        case OP_NOT_DIGIT:
1690
1.12k
        set_nottype_bits(re, cbit_digit, table_limit);
1691
1.12k
        break;
1692
1693
2.96k
        case OP_DIGIT:
1694
2.96k
        set_type_bits(re, cbit_digit, table_limit);
1695
2.96k
        break;
1696
1697
1.18k
        case OP_NOT_WHITESPACE:
1698
1.18k
        set_nottype_bits(re, cbit_space, table_limit);
1699
1.18k
        break;
1700
1701
2.50k
        case OP_WHITESPACE:
1702
2.50k
        set_type_bits(re, cbit_space, table_limit);
1703
2.50k
        break;
1704
1705
1.76k
        case OP_NOT_WORDCHAR:
1706
1.76k
        set_nottype_bits(re, cbit_word, table_limit);
1707
1.76k
        break;
1708
1709
2.75k
        case OP_WORDCHAR:
1710
2.75k
        set_type_bits(re, cbit_word, table_limit);
1711
2.75k
        break;
1712
18.6k
        }
1713
1714
18.1k
      tcode += 2;
1715
18.1k
      break;
1716
1717
      /* Set-based ECLASS: treat it the same as a "complex" XCLASS; give up. */
1718
1719
0
#ifdef SUPPORT_WIDE_CHARS
1720
102
      case OP_ECLASS:
1721
102
      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
7.04k
      case OP_XCLASS:
1733
7.04k
      xclassflags = tcode[1 + LINK_SIZE];
1734
7.04k
      if ((xclassflags & XCL_HASPROP) != 0 ||
1735
6.15k
          (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT)
1736
897
        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
6.15k
      classmap = ((xclassflags & XCL_MAP) == 0)? NULL :
1742
6.15k
        (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
6.15k
#if PCRE2_CODE_UNIT_WIDTH == 8
1748
6.15k
      if (utf && (xclassflags & XCL_NOT) == 0)
1749
5.91k
        {
1750
5.91k
        PCRE2_UCHAR b, e;
1751
5.91k
        PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32);
1752
5.91k
        tcode += GET(tcode, 1);
1753
1754
5.91k
        if (*p >= XCL_LIST)
1755
4.07k
          {
1756
4.07k
          study_char_list(p, re->start_bitmap,
1757
4.07k
            ((const uint8_t *)re + re->code_start));
1758
4.07k
          goto HANDLE_CLASSMAP;
1759
4.07k
          }
1760
1761
4.02k
        for (;;) switch (*p++)
1762
4.02k
          {
1763
1.07k
          case XCL_SINGLE:
1764
1.07k
          b = *p++;
1765
2.53k
          while ((*p & 0xc0) == 0x80) p++;
1766
1.07k
          re->start_bitmap[b/8] |= (1u << (b&7));
1767
1.07k
          break;
1768
1769
1.10k
          case XCL_RANGE:
1770
1.10k
          b = *p++;
1771
3.16k
          while ((*p & 0xc0) == 0x80) p++;
1772
1.10k
          e = *p++;
1773
3.38k
          while ((*p & 0xc0) == 0x80) p++;
1774
7.80k
          for (; b <= e; b++)
1775
6.70k
            re->start_bitmap[b/8] |= (1u << (b&7));
1776
1.10k
          break;
1777
1778
1.83k
          case XCL_END:
1779
1.83k
          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
4.02k
          }
1787
1.83k
        }
1788
232
#endif  /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
1789
232
#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
232
      PCRE2_FALLTHROUGH /* Fall through */
1795
232
1796
232
      /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
1797
232
      in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
1798
232
      because it starts a character with a value > 255. In 8-bit non-UTF mode,
1799
232
      there is no difference between CLASS and NCLASS. In all other wide
1800
232
      character modes, set the 0xFF bit to indicate code units >= 255. */
1801
232
1802
1.95k
      case OP_NCLASS:
1803
1.95k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1804
1.95k
      if (utf)
1805
637
        {
1806
637
        re->start_bitmap[24] |= 0xf0;            /* Bits for 0xc4 - 0xc8 */
1807
637
        memset(re->start_bitmap+25, 0xff, 7);    /* Bits for 0xc9 - 0xff */
1808
637
        }
1809
1.95k
      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
11.2k
      case OP_CLASS:
1820
11.2k
      if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
1821
10.9k
        {
1822
10.9k
        classmap = (const uint8_t *)(++tcode);
1823
10.9k
        tcode += 32 / sizeof(PCRE2_UCHAR);
1824
10.9k
        }
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
11.2k
#if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8
1834
17.1k
      HANDLE_CLASSMAP:
1835
17.1k
#endif
1836
17.1k
      if (classmap != NULL)
1837
16.7k
        {
1838
16.7k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1839
16.7k
        if (utf)
1840
6.48k
          {
1841
110k
          for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
1842
398k
          for (c = 128; c < 256; c++)
1843
392k
            {
1844
392k
            if ((classmap[c/8] & (1u << (c&7))) != 0)
1845
8.14k
              {
1846
8.14k
              int d = (c >> 6) | 0xc0;                 /* Set bit for this starter */
1847
8.14k
              re->start_bitmap[d/8] |= (1u << (d&7));  /* and then skip on to the */
1848
8.14k
              c = (c & 0xc0) + 0x40 - 1;               /* next relevant character. */
1849
8.14k
              }
1850
392k
            }
1851
6.48k
          }
1852
10.2k
        else
1853
10.2k
#endif
1854
        /* In all modes except UTF-8, the two bit maps are compatible. */
1855
1856
10.2k
          {
1857
338k
          for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
1858
10.2k
          }
1859
16.7k
        }
1860
1861
      /* Act on what follows the class. For a zero minimum repeat, continue;
1862
      otherwise stop processing. */
1863
1864
17.1k
      switch (*tcode)
1865
17.1k
        {
1866
3.94k
        case OP_CRSTAR:
1867
4.54k
        case OP_CRMINSTAR:
1868
6.91k
        case OP_CRQUERY:
1869
7.29k
        case OP_CRMINQUERY:
1870
9.07k
        case OP_CRPOSSTAR:
1871
9.56k
        case OP_CRPOSQUERY:
1872
9.56k
        tcode++;
1873
9.56k
        break;
1874
1875
622
        case OP_CRRANGE:
1876
872
        case OP_CRMINRANGE:
1877
1.20k
        case OP_CRPOSRANGE:
1878
1.20k
        if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1879
637
          else try_next = FALSE;
1880
1.20k
        break;
1881
1882
6.36k
        default:
1883
6.36k
        try_next = FALSE;
1884
6.36k
        break;
1885
17.1k
        }
1886
17.1k
      break; /* End of class handling case */
1887
521k
      }      /* End of switch for opcodes */
1888
521k
    }        /* End of try_next loop */
1889
1890
70.8k
  code += GET(code, 1);   /* Advance to next branch */
1891
70.8k
  }
1892
200k
while (*code == OP_ALT);
1893
1894
19.5k
return yield;
1895
200k
}
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
28.2k
{
1918
28.2k
int count = 0;
1919
28.2k
PCRE2_UCHAR *code;
1920
28.2k
BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
1921
28.2k
BOOL ucp = (re->overall_options & PCRE2_UCP) != 0;
1922
1923
/* Find start of compiled code */
1924
1925
28.2k
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
28.2k
if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
1932
12.3k
  {
1933
12.3k
  int depth = 0;
1934
12.3k
  int rc = set_start_bits(re, code, utf, ucp, &depth);
1935
  /* LCOV_EXCL_START */
1936
12.3k
  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
12.3k
  if (rc == SSB_DONE)
1952
3.98k
    {
1953
3.98k
    int i;
1954
3.98k
    int a = -1;
1955
3.98k
    int b = -1;
1956
3.98k
    uint8_t *p = re->start_bitmap;
1957
3.98k
    uint32_t flags = PCRE2_FIRSTMAPSET;
1958
1959
40.3k
    for (i = 0; i < 256; p++, i += 8)
1960
40.0k
      {
1961
40.0k
      uint8_t x = *p;
1962
40.0k
      if (x != 0)
1963
5.44k
        {
1964
5.44k
        int c;
1965
5.44k
        uint8_t y = x & (~x + 1);   /* Least significant bit */
1966
5.44k
        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.11k
        c = i;
1978
3.11k
        switch (x)
1979
3.11k
          {
1980
674
          case 1:   break;
1981
599
          case 2:   c += 1; break;  case 4:  c += 2; break;
1982
440
          case 8:   c += 3; break;  case 16: c += 4; break;
1983
316
          case 32:  c += 5; break;  case 64: c += 6; break;
1984
403
          case 128: c += 7; break;
1985
3.11k
          }
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.11k
#if PCRE2_CODE_UNIT_WIDTH == 8
1992
3.11k
        if (utf && c > 127) goto DONE;
1993
2.90k
#endif
1994
2.90k
        if (a < 0) a = c;   /* First one found, save in a */
1995
1.22k
        else if (b < 0)     /* Second one found */
1996
1.19k
          {
1997
1.19k
          int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c);
1998
1999
1.19k
#ifdef SUPPORT_UNICODE
2000
1.19k
          if (utf || ucp)
2001
562
            {
2002
562
            if (UCD_CASESET(c) != 0) goto DONE;     /* Multiple case set */
2003
481
            if (c > 127) d = UCD_OTHERCASE(c);
2004
481
            }
2005
1.11k
#endif  /* SUPPORT_UNICODE */
2006
2007
1.11k
          if (d != a) goto DONE;   /* Not the other case of a */
2008
60
          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
60
          }
2020
29
        else goto DONE;   /* More than two characters found */
2021
2.90k
        }
2022
40.0k
      }
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
286
    if (a >= 0) {
2032
228
      if ((re->flags & PCRE2_LASTSET) && (re->last_codeunit == (uint32_t)a || (b >= 0 && re->last_codeunit == (uint32_t)b))) {
2033
54
        re->flags &= ~(PCRE2_LASTSET | PCRE2_LASTCASELESS);
2034
54
        re->last_codeunit = 0;
2035
54
      }
2036
228
      re->first_codeunit = a;
2037
228
      flags = PCRE2_FIRSTSET;
2038
228
      if (b >= 0) flags |= PCRE2_FIRSTCASELESS;
2039
228
    }
2040
2041
3.98k
    DONE:
2042
3.98k
    re->flags |= flags;
2043
3.98k
    }
2044
12.3k
  }
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
28.2k
if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 &&
2055
23.0k
     re->top_backref <= MAX_CACHE_BACKREF)
2056
22.9k
  {
2057
22.9k
  int min;
2058
22.9k
  int backref_cache[MAX_CACHE_BACKREF+1];
2059
22.9k
  backref_cache[0] = 0;    /* Highest one that is set */
2060
22.9k
  min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
2061
22.9k
  switch(min)
2062
22.9k
    {
2063
65
    case -1:  /* \C in UTF mode or over-complex regex */
2064
65
    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
22.8k
    default:
2079
22.8k
    re->minlength = (min > (int)UINT16_MAX)? (int)UINT16_MAX : min;
2080
22.8k
    break;
2081
22.9k
    }
2082
22.9k
  }
2083
2084
28.2k
return 0;
2085
28.2k
}
2086
2087
/* End of pcre2_study.c */