Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/pcre/pcre2lib/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-2023 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
/* This module contains functions for scanning a compiled pattern and
42
collecting data (e.g. minimum matching length). */
43
44
45
#ifdef HAVE_CONFIG_H
46
#include "config.h"
47
#endif
48
49
#include "pcre2_internal.h"
50
51
/* The maximum remembered capturing brackets minimum. */
52
53
2.13k
#define MAX_CACHE_BACKREF 128
54
55
/* Set a bit in the starting code unit bit map. */
56
57
22.9k
#define SET_BIT(c) re->start_bitmap[(c)/8] |= (1u << ((c)&7))
58
59
/* Returns from set_start_bits() */
60
61
enum { SSB_FAIL, SSB_DONE, SSB_CONTINUE, SSB_UNKNOWN, SSB_TOODEEP };
62
63
64
/*************************************************
65
*   Find the minimum subject length for a group  *
66
*************************************************/
67
68
/* Scan a parenthesized group and compute the minimum length of subject that
69
is needed to match it. This is a lower bound; it does not mean there is a
70
string of that length that matches. In UTF mode, the result is in characters
71
rather than code units. The field in a compiled pattern for storing the minimum
72
length is 16-bits long (on the grounds that anything longer than that is
73
pathological), so we give up when we reach that amount. This also means that
74
integer overflow for really crazy patterns cannot happen.
75
76
Backreference minimum lengths are cached to speed up multiple references. This
77
function is called only when the highest back reference in the pattern is less
78
than or equal to MAX_CACHE_BACKREF, which is one less than the size of the
79
caching vector. The zeroth element contains the number of the highest set
80
value.
81
82
Arguments:
83
  re              compiled pattern block
84
  code            pointer to start of group (the bracket)
85
  startcode       pointer to start of the whole pattern's code
86
  utf             UTF flag
87
  recurses        chain of recurse_check to catch mutual recursion
88
  countptr        pointer to call count (to catch over complexity)
89
  backref_cache   vector for caching back references.
90
91
This function is no longer called when the pattern contains (*ACCEPT); however,
92
the old code for returning -1 is retained, just in case.
93
94
Returns:   the minimum length
95
           -1 \C in UTF-8 mode
96
              or (*ACCEPT)
97
              or pattern too complicated
98
           -2 internal error (missing capturing bracket)
99
           -3 internal error (opcode not listed)
100
*/
101
102
static int
103
find_minlength(const pcre2_real_code *re, PCRE2_SPTR code,
104
  PCRE2_SPTR startcode, BOOL utf, recurse_check *recurses, int *countptr,
105
  int *backref_cache)
106
5.32k
{
107
5.32k
int length = -1;
108
5.32k
int branchlength = 0;
109
5.32k
int prev_cap_recno = -1;
110
5.32k
int prev_cap_d = 0;
111
5.32k
int prev_recurse_recno = -1;
112
5.32k
int prev_recurse_d = 0;
113
5.32k
uint32_t once_fudge = 0;
114
5.32k
BOOL had_recurse = FALSE;
115
5.32k
BOOL dupcapused = (re->flags & PCRE2_DUPCAPUSED) != 0;
116
5.32k
PCRE2_SPTR nextbranch = code + GET(code, 1);
117
5.32k
PCRE2_UCHAR *cc = (PCRE2_UCHAR *)code + 1 + LINK_SIZE;
118
5.32k
recurse_check this_recurse;
119
120
/* If this is a "could be empty" group, its minimum length is 0. */
121
122
5.32k
if (*code >= OP_SBRA && *code <= OP_SCOND) return 0;
123
124
/* Skip over capturing bracket number */
125
126
4.44k
if (*code == OP_CBRA || *code == OP_CBRAPOS) cc += IMM2_SIZE;
127
128
/* A large and/or complex regex can take too long to process. */
129
130
4.44k
if ((*countptr)++ > 1000) return -1;
131
132
/* Scan along the opcodes for this branch. If we get to the end of the branch,
133
check the length against that of the other branches. If the accumulated length
134
passes 16-bits, reset to that value and skip the rest of the branch. */
135
136
4.44k
for (;;)
137
413k
  {
138
413k
  int d, min, recno;
139
413k
  PCRE2_UCHAR op, *cs, *ce;
140
141
413k
  if (branchlength >= UINT16_MAX)
142
0
    {
143
0
    branchlength = UINT16_MAX;
144
0
    cc = (PCRE2_UCHAR *)nextbranch;
145
0
    }
146
147
413k
  op = *cc;
148
413k
  switch (op)
149
413k
    {
150
0
    case OP_COND:
151
0
    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
0
    cs = cc + GET(cc, 1);
159
0
    if (*cs != OP_ALT)
160
0
      {
161
0
      cc = cs + 1 + LINK_SIZE;
162
0
      break;
163
0
      }
164
0
    goto PROCESS_NON_CAPTURE;
165
166
89
    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
89
    if (cc[1+LINK_SIZE] == OP_RECURSE && cc[2*(1+LINK_SIZE)] == OP_KET)
173
0
      {
174
0
      once_fudge = 1 + LINK_SIZE;
175
0
      cc += 1 + LINK_SIZE;
176
0
      break;
177
0
      }
178
    /* Fall through */
179
180
135
    case OP_ONCE:
181
135
    case OP_SCRIPT_RUN:
182
143
    case OP_SBRA:
183
144
    case OP_BRAPOS:
184
146
    case OP_SBRAPOS:
185
146
    PROCESS_NON_CAPTURE:
186
146
    d = find_minlength(re, cc, startcode, utf, recurses, countptr,
187
146
      backref_cache);
188
146
    if (d < 0) return d;
189
146
    branchlength += d;
190
864
    do cc += GET(cc, 1); while (*cc == OP_ALT);
191
146
    cc += 1 + LINK_SIZE;
192
146
    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
1.84k
    case OP_CBRA:
200
2.18k
    case OP_SCBRA:
201
2.36k
    case OP_CBRAPOS:
202
2.79k
    case OP_SCBRAPOS:
203
2.79k
    recno = (int)GET2(cc, 1+LINK_SIZE);
204
2.79k
    if (dupcapused || recno != prev_cap_recno)
205
2.79k
      {
206
2.79k
      prev_cap_recno = recno;
207
2.79k
      prev_cap_d = find_minlength(re, cc, startcode, utf, recurses, countptr,
208
2.79k
        backref_cache);
209
2.79k
      if (prev_cap_d < 0) return prev_cap_d;
210
2.79k
      }
211
2.79k
    branchlength += prev_cap_d;
212
3.62k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
213
2.79k
    cc += 1 + LINK_SIZE;
214
2.79k
    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
13.0k
    case OP_ALT:
231
17.1k
    case OP_KET:
232
17.2k
    case OP_KETRMAX:
233
17.2k
    case OP_KETRMIN:
234
17.4k
    case OP_KETRPOS:
235
17.4k
    case OP_END:
236
17.4k
    if (length < 0 || (!had_recurse && branchlength < length))
237
6.59k
      length = branchlength;
238
17.4k
    if (op != OP_ALT || length == 0) return length;
239
12.9k
    nextbranch = cc + GET(cc, 1);
240
12.9k
    cc += 1 + LINK_SIZE;
241
12.9k
    branchlength = 0;
242
12.9k
    had_recurse = FALSE;
243
12.9k
    break;
244
245
    /* Skip over assertive subpatterns */
246
247
91
    case OP_ASSERT:
248
98
    case OP_ASSERT_NOT:
249
106
    case OP_ASSERTBACK:
250
106
    case OP_ASSERTBACK_NOT:
251
163
    case OP_ASSERT_NA:
252
173
    case OP_ASSERTBACK_NA:
253
1.07k
    do cc += GET(cc, 1); while (*cc == OP_ALT);
254
    /* Fall through */
255
256
    /* Skip over things that don't match chars */
257
258
173
    case OP_REVERSE:
259
173
    case OP_VREVERSE:
260
173
    case OP_CREF:
261
173
    case OP_DNCREF:
262
173
    case OP_RREF:
263
173
    case OP_DNRREF:
264
173
    case OP_FALSE:
265
173
    case OP_TRUE:
266
173
    case OP_CALLOUT:
267
270
    case OP_SOD:
268
330
    case OP_SOM:
269
357
    case OP_EOD:
270
378
    case OP_EODN:
271
3.57k
    case OP_CIRC:
272
3.97k
    case OP_CIRCM:
273
5.06k
    case OP_DOLL:
274
5.17k
    case OP_DOLLM:
275
5.40k
    case OP_NOT_WORD_BOUNDARY:
276
5.67k
    case OP_WORD_BOUNDARY:
277
5.81k
    case OP_NOT_UCP_WORD_BOUNDARY:
278
5.90k
    case OP_UCP_WORD_BOUNDARY:
279
5.90k
    cc += PRIV(OP_lengths)[*cc];
280
5.90k
    break;
281
282
0
    case OP_CALLOUT_STR:
283
0
    cc += GET(cc, 1 + 2*LINK_SIZE);
284
0
    break;
285
286
    /* Skip over a subpattern that has a {0} or {0,x} quantifier */
287
288
81
    case OP_BRAZERO:
289
82
    case OP_BRAMINZERO:
290
85
    case OP_BRAPOSZERO:
291
85
    case OP_SKIPZERO:
292
85
    cc += PRIV(OP_lengths)[*cc];
293
156
    do cc += GET(cc, 1); while (*cc == OP_ALT);
294
85
    cc += 1 + LINK_SIZE;
295
85
    break;
296
297
    /* Handle literal characters and + repetitions */
298
299
208k
    case OP_CHAR:
300
330k
    case OP_CHARI:
301
330k
    case OP_NOT:
302
330k
    case OP_NOTI:
303
331k
    case OP_PLUS:
304
331k
    case OP_PLUSI:
305
331k
    case OP_MINPLUS:
306
331k
    case OP_MINPLUSI:
307
335k
    case OP_POSPLUS:
308
337k
    case OP_POSPLUSI:
309
337k
    case OP_NOTPLUS:
310
337k
    case OP_NOTPLUSI:
311
337k
    case OP_NOTMINPLUS:
312
337k
    case OP_NOTMINPLUSI:
313
337k
    case OP_NOTPOSPLUS:
314
337k
    case OP_NOTPOSPLUSI:
315
337k
    branchlength++;
316
337k
    cc += 2;
317
337k
#ifdef SUPPORT_UNICODE
318
337k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
319
337k
#endif
320
337k
    break;
321
322
1.34k
    case OP_TYPEPLUS:
323
2.05k
    case OP_TYPEMINPLUS:
324
3.06k
    case OP_TYPEPOSPLUS:
325
3.06k
    branchlength++;
326
3.06k
    cc += (cc[1] == OP_PROP || cc[1] == OP_NOTPROP)? 4 : 2;
327
3.06k
    break;
328
329
    /* Handle exact repetitions. The count is already in characters, but we
330
    may need to skip over a multibyte character in UTF mode.  */
331
332
35
    case OP_EXACT:
333
63
    case OP_EXACTI:
334
63
    case OP_NOTEXACT:
335
63
    case OP_NOTEXACTI:
336
63
    branchlength += GET2(cc,1);
337
63
    cc += 2 + IMM2_SIZE;
338
63
#ifdef SUPPORT_UNICODE
339
63
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
340
63
#endif
341
63
    break;
342
343
102
    case OP_TYPEEXACT:
344
102
    branchlength += GET2(cc,1);
345
102
    cc += 2 + IMM2_SIZE + ((cc[1 + IMM2_SIZE] == OP_PROP
346
102
      || cc[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
347
102
    break;
348
349
    /* Handle single-char non-literal matchers */
350
351
955
    case OP_PROP:
352
1.20k
    case OP_NOTPROP:
353
1.20k
    cc += 2;
354
    /* Fall through */
355
356
1.38k
    case OP_NOT_DIGIT:
357
1.73k
    case OP_DIGIT:
358
1.83k
    case OP_NOT_WHITESPACE:
359
2.02k
    case OP_WHITESPACE:
360
2.17k
    case OP_NOT_WORDCHAR:
361
5.01k
    case OP_WORDCHAR:
362
12.3k
    case OP_ANY:
363
12.5k
    case OP_ALLANY:
364
12.8k
    case OP_EXTUNI:
365
12.9k
    case OP_HSPACE:
366
13.3k
    case OP_NOT_HSPACE:
367
13.8k
    case OP_VSPACE:
368
14.1k
    case OP_NOT_VSPACE:
369
14.1k
    branchlength++;
370
14.1k
    cc++;
371
14.1k
    break;
372
373
    /* "Any newline" might match two characters, but it also might match just
374
    one. */
375
376
1.53k
    case OP_ANYNL:
377
1.53k
    branchlength += 1;
378
1.53k
    cc++;
379
1.53k
    break;
380
381
    /* The single-byte matcher means we can't proceed in UTF mode. (In
382
    non-UTF mode \C will actually be turned into OP_ALLANY, so won't ever
383
    appear, but leave the code, just in case.) */
384
385
0
    case OP_ANYBYTE:
386
0
#ifdef SUPPORT_UNICODE
387
0
    if (utf) return -1;
388
0
#endif
389
0
    branchlength++;
390
0
    cc++;
391
0
    break;
392
393
    /* For repeated character types, we have to test for \p and \P, which have
394
    an extra two bytes of parameters. */
395
396
1.23k
    case OP_TYPESTAR:
397
1.26k
    case OP_TYPEMINSTAR:
398
2.93k
    case OP_TYPEQUERY:
399
3.26k
    case OP_TYPEMINQUERY:
400
3.32k
    case OP_TYPEPOSSTAR:
401
6.44k
    case OP_TYPEPOSQUERY:
402
6.44k
    if (cc[1] == OP_PROP || cc[1] == OP_NOTPROP) cc += 2;
403
6.44k
    cc += PRIV(OP_lengths)[op];
404
6.44k
    break;
405
406
0
    case OP_TYPEUPTO:
407
0
    case OP_TYPEMINUPTO:
408
0
    case OP_TYPEPOSUPTO:
409
0
    if (cc[1 + IMM2_SIZE] == OP_PROP
410
0
      || cc[1 + IMM2_SIZE] == OP_NOTPROP) cc += 2;
411
0
    cc += PRIV(OP_lengths)[op];
412
0
    break;
413
414
    /* Check a class for variable quantification */
415
416
3.92k
    case OP_CLASS:
417
5.80k
    case OP_NCLASS:
418
5.80k
#ifdef SUPPORT_WIDE_CHARS
419
6.70k
    case OP_XCLASS:
420
    /* The original code caused an unsigned overflow in 64 bit systems,
421
    so now we use a conditional statement. */
422
6.70k
    if (op == OP_XCLASS)
423
902
      cc += GET(cc, 1);
424
5.80k
    else
425
5.80k
      cc += PRIV(OP_lengths)[OP_CLASS];
426
#else
427
    cc += PRIV(OP_lengths)[OP_CLASS];
428
#endif
429
430
6.70k
    switch (*cc)
431
6.70k
      {
432
470
      case OP_CRPLUS:
433
555
      case OP_CRMINPLUS:
434
1.09k
      case OP_CRPOSPLUS:
435
1.09k
      branchlength++;
436
      /* Fall through */
437
438
1.76k
      case OP_CRSTAR:
439
2.35k
      case OP_CRMINSTAR:
440
2.81k
      case OP_CRQUERY:
441
3.33k
      case OP_CRMINQUERY:
442
4.19k
      case OP_CRPOSSTAR:
443
4.57k
      case OP_CRPOSQUERY:
444
4.57k
      cc++;
445
4.57k
      break;
446
447
0
      case OP_CRRANGE:
448
0
      case OP_CRMINRANGE:
449
0
      case OP_CRPOSRANGE:
450
0
      branchlength += GET2(cc,1);
451
0
      cc += 1 + 2 * IMM2_SIZE;
452
0
      break;
453
454
2.12k
      default:
455
2.12k
      branchlength++;
456
2.12k
      break;
457
6.70k
      }
458
6.70k
    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
6.70k
    case OP_DNREF:
478
0
    case OP_DNREFI:
479
0
    if (!dupcapused && (re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
480
0
      {
481
0
      int count = GET2(cc, 1+IMM2_SIZE);
482
0
      PCRE2_UCHAR *slot =
483
0
        (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
484
0
          GET2(cc, 1) * re->name_entry_size;
485
486
0
      d = INT_MAX;
487
488
      /* Scan all groups with the same name; find the shortest. */
489
490
0
      while (count-- > 0)
491
0
        {
492
0
        int dd, i;
493
0
        recno = GET2(slot, 0);
494
495
0
        if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
496
0
          dd = backref_cache[recno];
497
0
        else
498
0
          {
499
0
          ce = cs = (PCRE2_UCHAR *)PRIV(find_bracket)(startcode, utf, recno);
500
0
          if (cs == NULL) return -2;
501
0
          do ce += GET(ce, 1); while (*ce == OP_ALT);
502
503
0
          dd = 0;
504
0
          if (!dupcapused ||
505
0
              (PCRE2_UCHAR *)PRIV(find_bracket)(ce, utf, recno) == NULL)
506
0
            {
507
0
            if (cc > cs && cc < ce)    /* Simple recursion */
508
0
              {
509
0
              had_recurse = TRUE;
510
0
              }
511
0
            else
512
0
              {
513
0
              recurse_check *r = recurses;
514
0
              for (r = recurses; r != NULL; r = r->prev)
515
0
                if (r->group == cs) break;
516
0
              if (r != NULL)           /* Mutual recursion */
517
0
                {
518
0
                had_recurse = TRUE;
519
0
                }
520
0
              else
521
0
                {
522
0
                this_recurse.prev = recurses;  /* No recursion */
523
0
                this_recurse.group = cs;
524
0
                dd = find_minlength(re, cs, startcode, utf, &this_recurse,
525
0
                  countptr, backref_cache);
526
0
                if (dd < 0) return dd;
527
0
                }
528
0
              }
529
0
            }
530
531
0
          backref_cache[recno] = dd;
532
0
          for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
533
0
          backref_cache[0] = recno;
534
0
          }
535
536
0
        if (dd < d) d = dd;
537
0
        if (d <= 0) break;    /* No point looking at any more */
538
0
        slot += re->name_entry_size;
539
0
        }
540
0
      }
541
0
    else d = 0;
542
0
    cc += 1 + 2*IMM2_SIZE;
543
0
    goto REPEAT_BACK_REFERENCE;
544
545
    /* Single back reference by number. References by name are converted to by
546
    number when there is no duplication. */
547
548
741
    case OP_REF:
549
1.19k
    case OP_REFI:
550
1.19k
    recno = GET2(cc, 1);
551
1.19k
    if (recno <= backref_cache[0] && backref_cache[recno] >= 0)
552
930
      d = backref_cache[recno];
553
265
    else
554
265
      {
555
265
      int i;
556
265
      d = 0;
557
558
265
      if ((re->overall_options & PCRE2_MATCH_UNSET_BACKREF) == 0)
559
265
        {
560
265
        ce = cs = (PCRE2_UCHAR *)PRIV(find_bracket)(startcode, utf, recno);
561
265
        if (cs == NULL) return -2;
562
454
        do ce += GET(ce, 1); while (*ce == OP_ALT);
563
564
265
        if (!dupcapused ||
565
1
            (PCRE2_UCHAR *)PRIV(find_bracket)(ce, utf, recno) == NULL)
566
265
          {
567
265
          if (cc > cs && cc < ce)    /* Simple recursion */
568
18
            {
569
18
            had_recurse = TRUE;
570
18
            }
571
247
          else
572
247
            {
573
247
            recurse_check *r = recurses;
574
247
            for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
575
247
            if (r != NULL)           /* Mutual recursion */
576
0
              {
577
0
              had_recurse = TRUE;
578
0
              }
579
247
            else                     /* No recursion */
580
247
              {
581
247
              this_recurse.prev = recurses;
582
247
              this_recurse.group = cs;
583
247
              d = find_minlength(re, cs, startcode, utf, &this_recurse, countptr,
584
247
                backref_cache);
585
247
              if (d < 0) return d;
586
247
              }
587
247
            }
588
265
          }
589
265
        }
590
591
265
      backref_cache[recno] = d;
592
1.21k
      for (i = backref_cache[0] + 1; i < recno; i++) backref_cache[i] = -1;
593
265
      backref_cache[0] = recno;
594
265
      }
595
596
1.19k
    cc += 1 + IMM2_SIZE;
597
598
    /* Handle repeated back references */
599
600
1.19k
    REPEAT_BACK_REFERENCE:
601
1.19k
    switch (*cc)
602
1.19k
      {
603
0
      case OP_CRSTAR:
604
0
      case OP_CRMINSTAR:
605
9
      case OP_CRQUERY:
606
9
      case OP_CRMINQUERY:
607
9
      case OP_CRPOSSTAR:
608
9
      case OP_CRPOSQUERY:
609
9
      min = 0;
610
9
      cc++;
611
9
      break;
612
613
2
      case OP_CRPLUS:
614
2
      case OP_CRMINPLUS:
615
2
      case OP_CRPOSPLUS:
616
2
      min = 1;
617
2
      cc++;
618
2
      break;
619
620
0
      case OP_CRRANGE:
621
0
      case OP_CRMINRANGE:
622
0
      case OP_CRPOSRANGE:
623
0
      min = GET2(cc, 1);
624
0
      cc += 1 + 2 * IMM2_SIZE;
625
0
      break;
626
627
1.18k
      default:
628
1.18k
      min = 1;
629
1.18k
      break;
630
1.19k
      }
631
632
     /* Take care not to overflow: (1) min and d are ints, so check that their
633
     product is not greater than INT_MAX. (2) branchlength is limited to
634
     UINT16_MAX (checked at the top of the loop). */
635
636
1.19k
    if ((d > 0 && (INT_MAX/d) < min) || UINT16_MAX - branchlength < min*d)
637
0
      branchlength = UINT16_MAX;
638
1.19k
    else branchlength += min * d;
639
1.19k
    break;
640
641
    /* Recursion always refers to the first occurrence of a subpattern with a
642
    given number. Therefore, we can always make use of caching, even when the
643
    pattern contains multiple subpatterns with the same number. */
644
645
3
    case OP_RECURSE:
646
3
    cs = ce = (PCRE2_UCHAR *)startcode + GET(cc, 1);
647
3
    recno = GET2(cs, 1+LINK_SIZE);
648
3
    if (recno == prev_recurse_recno)
649
0
      {
650
0
      branchlength += prev_recurse_d;
651
0
      }
652
3
    else
653
3
      {
654
5
      do ce += GET(ce, 1); while (*ce == OP_ALT);
655
3
      if (cc > cs && cc < ce)    /* Simple recursion */
656
1
        had_recurse = TRUE;
657
2
      else
658
2
        {
659
2
        recurse_check *r = recurses;
660
2
        for (r = recurses; r != NULL; r = r->prev) if (r->group == cs) break;
661
2
        if (r != NULL)          /* Mutual recursion */
662
0
          had_recurse = TRUE;
663
2
        else
664
2
          {
665
2
          this_recurse.prev = recurses;
666
2
          this_recurse.group = cs;
667
2
          prev_recurse_d = find_minlength(re, cs, startcode, utf, &this_recurse,
668
2
            countptr, backref_cache);
669
2
          if (prev_recurse_d < 0) return prev_recurse_d;
670
2
          prev_recurse_recno = recno;
671
2
          branchlength += prev_recurse_d;
672
2
          }
673
2
        }
674
3
      }
675
3
    cc += 1 + LINK_SIZE + once_fudge;
676
3
    once_fudge = 0;
677
3
    break;
678
679
    /* Anything else does not or need not match a character. We can get the
680
    item's length from the table, but for those that can match zero occurrences
681
    of a character, we must take special action for UTF-8 characters. As it
682
    happens, the "NOT" versions of these opcodes are used at present only for
683
    ASCII characters, so they could be omitted from this list. However, in
684
    future that may change, so we include them here so as not to leave a
685
    gotcha for a future maintainer. */
686
687
0
    case OP_UPTO:
688
0
    case OP_UPTOI:
689
0
    case OP_NOTUPTO:
690
0
    case OP_NOTUPTOI:
691
0
    case OP_MINUPTO:
692
0
    case OP_MINUPTOI:
693
0
    case OP_NOTMINUPTO:
694
0
    case OP_NOTMINUPTOI:
695
0
    case OP_POSUPTO:
696
1
    case OP_POSUPTOI:
697
1
    case OP_NOTPOSUPTO:
698
1
    case OP_NOTPOSUPTOI:
699
700
487
    case OP_STAR:
701
591
    case OP_STARI:
702
599
    case OP_NOTSTAR:
703
599
    case OP_NOTSTARI:
704
635
    case OP_MINSTAR:
705
742
    case OP_MINSTARI:
706
743
    case OP_NOTMINSTAR:
707
746
    case OP_NOTMINSTARI:
708
2.26k
    case OP_POSSTAR:
709
3.39k
    case OP_POSSTARI:
710
3.39k
    case OP_NOTPOSSTAR:
711
3.39k
    case OP_NOTPOSSTARI:
712
713
4.57k
    case OP_QUERY:
714
5.59k
    case OP_QUERYI:
715
5.71k
    case OP_NOTQUERY:
716
5.90k
    case OP_NOTQUERYI:
717
6.45k
    case OP_MINQUERY:
718
7.12k
    case OP_MINQUERYI:
719
7.17k
    case OP_NOTMINQUERY:
720
7.26k
    case OP_NOTMINQUERYI:
721
12.5k
    case OP_POSQUERY:
722
16.2k
    case OP_POSQUERYI:
723
16.2k
    case OP_NOTPOSQUERY:
724
16.2k
    case OP_NOTPOSQUERYI:
725
726
16.2k
    cc += PRIV(OP_lengths)[op];
727
16.2k
#ifdef SUPPORT_UNICODE
728
16.2k
    if (utf && HAS_EXTRALEN(cc[-1])) cc += GET_EXTRALEN(cc[-1]);
729
16.2k
#endif
730
16.2k
    break;
731
732
    /* Skip these, but we need to add in the name length. */
733
734
14
    case OP_MARK:
735
14
    case OP_COMMIT_ARG:
736
14
    case OP_PRUNE_ARG:
737
14
    case OP_SKIP_ARG:
738
14
    case OP_THEN_ARG:
739
14
    cc += PRIV(OP_lengths)[op] + cc[1];
740
14
    break;
741
742
    /* The remaining opcodes are just skipped over. */
743
744
0
    case OP_CLOSE:
745
0
    case OP_COMMIT:
746
0
    case OP_FAIL:
747
0
    case OP_PRUNE:
748
15
    case OP_SET_SOM:
749
15
    case OP_SKIP:
750
15
    case OP_THEN:
751
15
    cc += PRIV(OP_lengths)[op];
752
15
    break;
753
754
    /* This should not occur: we list all opcodes explicitly so that when
755
    new ones get added they are properly considered. */
756
757
0
    default:
758
0
    return -3;
759
413k
    }
760
413k
  }
761
/* Control never gets here */
762
4.44k
}
763
764
765
766
/*************************************************
767
*      Set a bit and maybe its alternate case    *
768
*************************************************/
769
770
/* Given a character, set its first code unit's bit in the table, and also the
771
corresponding bit for the other version of a letter if we are caseless.
772
773
Arguments:
774
  re            points to the regex block
775
  p             points to the first code unit of the character
776
  caseless      TRUE if caseless
777
  utf           TRUE for UTF mode
778
  ucp           TRUE for UCP mode
779
780
Returns:        pointer after the character
781
*/
782
783
static PCRE2_SPTR
784
set_table_bit(pcre2_real_code *re, PCRE2_SPTR p, BOOL caseless, BOOL utf,
785
  BOOL ucp)
786
11.9k
{
787
11.9k
uint32_t c = *p++;   /* First code unit */
788
789
11.9k
(void)utf;           /* Stop compiler warnings when UTF not supported */
790
11.9k
(void)ucp;
791
792
/* In 16-bit and 32-bit modes, code units greater than 0xff set the bit for
793
0xff. */
794
795
#if PCRE2_CODE_UNIT_WIDTH != 8
796
if (c > 0xff) SET_BIT(0xff); else
797
#endif
798
799
11.9k
SET_BIT(c);
800
801
/* In UTF-8 or UTF-16 mode, pick up the remaining code units in order to find
802
the end of the character, even when caseless. */
803
804
11.9k
#ifdef SUPPORT_UNICODE
805
11.9k
if (utf)
806
1.22k
  {
807
1.22k
#if PCRE2_CODE_UNIT_WIDTH == 8
808
1.22k
  if (c >= 0xc0) GETUTF8INC(c, p);
809
#elif PCRE2_CODE_UNIT_WIDTH == 16
810
  if ((c & 0xfc00) == 0xd800) GETUTF16INC(c, p);
811
#endif
812
1.22k
  }
813
11.9k
#endif  /* SUPPORT_UNICODE */
814
815
/* If caseless, handle the other case of the character. */
816
817
11.9k
if (caseless)
818
5.73k
  {
819
5.73k
#ifdef SUPPORT_UNICODE
820
5.73k
  if (utf || ucp)
821
1.04k
    {
822
1.04k
    c = UCD_OTHERCASE(c);
823
1.04k
#if PCRE2_CODE_UNIT_WIDTH == 8
824
1.04k
    if (utf)
825
1.04k
      {
826
1.04k
      PCRE2_UCHAR buff[6];
827
1.04k
      (void)PRIV(ord2utf)(c, buff);
828
1.04k
      SET_BIT(buff[0]);
829
1.04k
      }
830
0
    else if (c < 256) SET_BIT(c);
831
#else  /* 16-bit or 32-bit mode */
832
    if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
833
#endif
834
1.04k
    }
835
836
4.69k
  else
837
4.69k
#endif  /* SUPPORT_UNICODE */
838
839
  /* Not UTF or UCP */
840
841
4.69k
  if (MAX_255(c)) SET_BIT(re->tables[fcc_offset + c]);
842
5.73k
  }
843
844
11.9k
return p;
845
11.9k
}
846
847
848
849
/*************************************************
850
*     Set bits for a positive character type     *
851
*************************************************/
852
853
/* This function sets starting bits for a character type. In UTF-8 mode, we can
854
only do a direct setting for bytes less than 128, as otherwise there can be
855
confusion with bytes in the middle of UTF-8 characters. In a "traditional"
856
environment, the tables will only recognize ASCII characters anyway, but in at
857
least one Windows environment, some higher bytes bits were set in the tables.
858
So we deal with that case by considering the UTF-8 encoding.
859
860
Arguments:
861
  re             the regex block
862
  cbit type      the type of character wanted
863
  table_limit    32 for non-UTF-8; 16 for UTF-8
864
865
Returns:         nothing
866
*/
867
868
static void
869
set_type_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
870
3.04k
{
871
3.04k
uint32_t c;
872
100k
for (c = 0; c < table_limit; c++)
873
97.3k
  re->start_bitmap[c] |= re->tables[c+cbits_offset+cbit_type];
874
3.04k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
875
3.04k
if (table_limit == 32) return;
876
0
for (c = 128; c < 256; c++)
877
0
  {
878
0
  if ((re->tables[cbits_offset + c/8] & (1u << (c&7))) != 0)
879
0
    {
880
0
    PCRE2_UCHAR buff[6];
881
0
    (void)PRIV(ord2utf)(c, buff);
882
0
    SET_BIT(buff[0]);
883
0
    }
884
0
  }
885
0
#endif  /* UTF-8 */
886
0
}
887
888
889
/*************************************************
890
*     Set bits for a negative character type     *
891
*************************************************/
892
893
/* This function sets starting bits for a negative character type such as \D.
894
In UTF-8 mode, we can only do a direct setting for bytes less than 128, as
895
otherwise there can be confusion with bytes in the middle of UTF-8 characters.
896
Unlike in the positive case, where we can set appropriate starting bits for
897
specific high-valued UTF-8 characters, in this case we have to set the bits for
898
all high-valued characters. The lowest is 0xc2, but we overkill by starting at
899
0xc0 (192) for simplicity.
900
901
Arguments:
902
  re             the regex block
903
  cbit type      the type of character wanted
904
  table_limit    32 for non-UTF-8; 16 for UTF-8
905
906
Returns:         nothing
907
*/
908
909
static void
910
set_nottype_bits(pcre2_real_code *re, int cbit_type, unsigned int table_limit)
911
190
{
912
190
uint32_t c;
913
6.27k
for (c = 0; c < table_limit; c++)
914
6.08k
  re->start_bitmap[c] |= (uint8_t)(~(re->tables[c+cbits_offset+cbit_type]));
915
190
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
916
190
if (table_limit != 32) for (c = 24; c < 32; c++) re->start_bitmap[c] = 0xff;
917
190
#endif
918
190
}
919
920
921
922
/*************************************************
923
*      Create bitmap of starting code units      *
924
*************************************************/
925
926
/* This function scans a compiled unanchored expression recursively and
927
attempts to build a bitmap of the set of possible starting code units whose
928
values are less than 256. In 16-bit and 32-bit mode, values above 255 all cause
929
the 255 bit to be set. When calling set[_not]_type_bits() in UTF-8 (sic) mode
930
we pass a value of 16 rather than 32 as the final argument. (See comments in
931
those functions for the reason.)
932
933
The SSB_CONTINUE return is useful for parenthesized groups in patterns such as
934
(a*)b where the group provides some optional starting code units but scanning
935
must continue at the outer level to find at least one mandatory code unit. At
936
the outermost level, this function fails unless the result is SSB_DONE.
937
938
We restrict recursion (for nested groups) to 1000 to avoid stack overflow
939
issues.
940
941
Arguments:
942
  re           points to the compiled regex block
943
  code         points to an expression
944
  utf          TRUE if in UTF mode
945
  ucp          TRUE if in UCP mode
946
  depthptr     pointer to recurse depth
947
948
Returns:       SSB_FAIL     => Failed to find any starting code units
949
               SSB_DONE     => Found mandatory starting code units
950
               SSB_CONTINUE => Found optional starting code units
951
               SSB_UNKNOWN  => Hit an unrecognized opcode
952
               SSB_TOODEEP  => Recursion is too deep
953
*/
954
955
static int
956
set_start_bits(pcre2_real_code *re, PCRE2_SPTR code, BOOL utf, BOOL ucp,
957
  int *depthptr)
958
3.34k
{
959
3.34k
uint32_t c;
960
3.34k
int yield = SSB_DONE;
961
962
3.34k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
963
3.34k
int table_limit = utf? 16:32;
964
#else
965
int table_limit = 32;
966
#endif
967
968
3.34k
*depthptr += 1;
969
3.34k
if (*depthptr > 1000) return SSB_TOODEEP;
970
971
3.34k
do
972
15.7k
  {
973
15.7k
  BOOL try_next = TRUE;
974
15.7k
  PCRE2_SPTR tcode = code + 1 + LINK_SIZE;
975
976
15.7k
  if (*code == OP_CBRA || *code == OP_SCBRA ||
977
14.8k
      *code == OP_CBRAPOS || *code == OP_SCBRAPOS) tcode += IMM2_SIZE;
978
979
38.7k
  while (try_next)    /* Loop for items in this branch */
980
25.0k
    {
981
25.0k
    int rc;
982
25.0k
    PCRE2_SPTR ncode;
983
25.0k
    uint8_t *classmap = NULL;
984
25.0k
#ifdef SUPPORT_WIDE_CHARS
985
25.0k
    PCRE2_UCHAR xclassflags;
986
25.0k
#endif
987
988
25.0k
    switch(*tcode)
989
25.0k
      {
990
      /* If we reach something we don't understand, it means a new opcode has
991
      been created that hasn't been added to this function. Hopefully this
992
      problem will be discovered during testing. */
993
994
0
      default:
995
0
      return SSB_UNKNOWN;
996
997
      /* Fail for a valid opcode that implies no starting bits. */
998
999
0
      case OP_ACCEPT:
1000
0
      case OP_ASSERT_ACCEPT:
1001
114
      case OP_ALLANY:
1002
244
      case OP_ANY:
1003
244
      case OP_ANYBYTE:
1004
255
      case OP_CIRCM:
1005
255
      case OP_CLOSE:
1006
255
      case OP_COMMIT:
1007
255
      case OP_COMMIT_ARG:
1008
255
      case OP_COND:
1009
255
      case OP_CREF:
1010
255
      case OP_FALSE:
1011
255
      case OP_TRUE:
1012
255
      case OP_DNCREF:
1013
255
      case OP_DNREF:
1014
255
      case OP_DNREFI:
1015
255
      case OP_DNRREF:
1016
291
      case OP_DOLL:
1017
299
      case OP_DOLLM:
1018
299
      case OP_END:
1019
307
      case OP_EOD:
1020
310
      case OP_EODN:
1021
363
      case OP_EXTUNI:
1022
363
      case OP_FAIL:
1023
365
      case OP_MARK:
1024
382
      case OP_NOT:
1025
382
      case OP_NOTEXACT:
1026
382
      case OP_NOTEXACTI:
1027
403
      case OP_NOTI:
1028
403
      case OP_NOTMINPLUS:
1029
405
      case OP_NOTMINPLUSI:
1030
409
      case OP_NOTMINQUERY:
1031
409
      case OP_NOTMINQUERYI:
1032
409
      case OP_NOTMINSTAR:
1033
412
      case OP_NOTMINSTARI:
1034
412
      case OP_NOTMINUPTO:
1035
412
      case OP_NOTMINUPTOI:
1036
424
      case OP_NOTPLUS:
1037
434
      case OP_NOTPLUSI:
1038
435
      case OP_NOTPOSPLUS:
1039
437
      case OP_NOTPOSPLUSI:
1040
437
      case OP_NOTPOSQUERY:
1041
439
      case OP_NOTPOSQUERYI:
1042
439
      case OP_NOTPOSSTAR:
1043
439
      case OP_NOTPOSSTARI:
1044
439
      case OP_NOTPOSUPTO:
1045
439
      case OP_NOTPOSUPTOI:
1046
521
      case OP_NOTPROP:
1047
522
      case OP_NOTQUERY:
1048
570
      case OP_NOTQUERYI:
1049
572
      case OP_NOTSTAR:
1050
572
      case OP_NOTSTARI:
1051
572
      case OP_NOTUPTO:
1052
572
      case OP_NOTUPTOI:
1053
663
      case OP_NOT_HSPACE:
1054
771
      case OP_NOT_VSPACE:
1055
771
      case OP_PRUNE:
1056
771
      case OP_PRUNE_ARG:
1057
772
      case OP_RECURSE:
1058
773
      case OP_REF:
1059
773
      case OP_REFI:
1060
776
      case OP_REVERSE:
1061
776
      case OP_VREVERSE:
1062
776
      case OP_RREF:
1063
776
      case OP_SCOND:
1064
785
      case OP_SET_SOM:
1065
785
      case OP_SKIP:
1066
785
      case OP_SKIP_ARG:
1067
795
      case OP_SOD:
1068
808
      case OP_SOM:
1069
808
      case OP_THEN:
1070
808
      case OP_THEN_ARG:
1071
808
      return SSB_FAIL;
1072
1073
      /* OP_CIRC happens only at the start of an anchored branch (multiline ^
1074
      uses OP_CIRCM). Skip over it. */
1075
1076
313
      case OP_CIRC:
1077
313
      tcode += PRIV(OP_lengths)[OP_CIRC];
1078
313
      break;
1079
1080
      /* A "real" property test implies no starting bits, but the fake property
1081
      PT_CLIST identifies a list of characters. These lists are short, as they
1082
      are used for characters with more than one "other case", so there is no
1083
      point in recognizing them for OP_NOTPROP. */
1084
1085
68
      case OP_PROP:
1086
68
      if (tcode[1] != PT_CLIST) return SSB_FAIL;
1087
27
        {
1088
27
        const uint32_t *p = PRIV(ucd_caseless_sets) + tcode[2];
1089
108
        while ((c = *p++) < NOTACHAR)
1090
81
          {
1091
81
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1092
81
          if (utf)
1093
81
            {
1094
81
            PCRE2_UCHAR buff[6];
1095
81
            (void)PRIV(ord2utf)(c, buff);
1096
81
            c = buff[0];
1097
81
            }
1098
81
#endif
1099
81
          if (c > 0xff) SET_BIT(0xff); else SET_BIT(c);
1100
81
          }
1101
27
        }
1102
27
      try_next = FALSE;
1103
27
      break;
1104
1105
      /* We can ignore word boundary tests. */
1106
1107
31
      case OP_WORD_BOUNDARY:
1108
97
      case OP_NOT_WORD_BOUNDARY:
1109
153
      case OP_UCP_WORD_BOUNDARY:
1110
258
      case OP_NOT_UCP_WORD_BOUNDARY:
1111
258
      tcode++;
1112
258
      break;
1113
1114
      /* For a positive lookahead assertion, inspect what immediately follows,
1115
      ignoring intermediate assertions and callouts. If the next item is one
1116
      that sets a mandatory character, skip this assertion. Otherwise, treat it
1117
      the same as other bracket groups. */
1118
1119
112
      case OP_ASSERT:
1120
156
      case OP_ASSERT_NA:
1121
156
      ncode = tcode + GET(tcode, 1);
1122
987
      while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1123
156
      ncode += 1 + LINK_SIZE;
1124
1125
      /* Skip irrelevant items */
1126
1127
312
      for (BOOL done = FALSE; !done;)
1128
156
        {
1129
156
        switch (*ncode)
1130
156
          {
1131
0
          case OP_ASSERT:
1132
0
          case OP_ASSERT_NOT:
1133
0
          case OP_ASSERTBACK:
1134
0
          case OP_ASSERTBACK_NOT:
1135
0
          case OP_ASSERT_NA:
1136
0
          case OP_ASSERTBACK_NA:
1137
0
          ncode += GET(ncode, 1);
1138
0
          while (*ncode == OP_ALT) ncode += GET(ncode, 1);
1139
0
          ncode += 1 + LINK_SIZE;
1140
0
          break;
1141
1142
0
          case OP_WORD_BOUNDARY:
1143
0
          case OP_NOT_WORD_BOUNDARY:
1144
0
          case OP_UCP_WORD_BOUNDARY:
1145
0
          case OP_NOT_UCP_WORD_BOUNDARY:
1146
0
          ncode++;
1147
0
          break;
1148
1149
0
          case OP_CALLOUT:
1150
0
          ncode += PRIV(OP_lengths)[OP_CALLOUT];
1151
0
          break;
1152
1153
0
          case OP_CALLOUT_STR:
1154
0
          ncode += GET(ncode, 1 + 2*LINK_SIZE);
1155
0
          break;
1156
1157
156
          default:
1158
156
          done = TRUE;
1159
156
          break;
1160
156
          }
1161
156
        }
1162
1163
      /* Now check the next significant item. */
1164
1165
156
      switch(*ncode)
1166
156
        {
1167
33
        default:
1168
33
        break;
1169
1170
33
        case OP_PROP:
1171
0
        if (ncode[1] != PT_CLIST) break;
1172
        /* Fall through */
1173
0
        case OP_ANYNL:
1174
123
        case OP_CHAR:
1175
123
        case OP_CHARI:
1176
123
        case OP_EXACT:
1177
123
        case OP_EXACTI:
1178
123
        case OP_HSPACE:
1179
123
        case OP_MINPLUS:
1180
123
        case OP_MINPLUSI:
1181
123
        case OP_PLUS:
1182
123
        case OP_PLUSI:
1183
123
        case OP_POSPLUS:
1184
123
        case OP_POSPLUSI:
1185
123
        case OP_VSPACE:
1186
        /* Note that these types will only be present in non-UCP mode. */
1187
123
        case OP_DIGIT:
1188
123
        case OP_NOT_DIGIT:
1189
123
        case OP_WORDCHAR:
1190
123
        case OP_NOT_WORDCHAR:
1191
123
        case OP_WHITESPACE:
1192
123
        case OP_NOT_WHITESPACE:
1193
123
        tcode = ncode;
1194
123
        continue;   /* With the following significant opcode */
1195
156
        }
1196
      /* Fall through */
1197
1198
      /* For a group bracket or a positive assertion without an immediately
1199
      following mandatory setting, recurse to set bits from within the
1200
      subpattern. If it can't find anything, we have to give up. If it finds
1201
      some mandatory character(s), we are done for this branch. Otherwise,
1202
      carry on scanning after the subpattern. */
1203
1204
109
      case OP_BRA:
1205
116
      case OP_SBRA:
1206
731
      case OP_CBRA:
1207
999
      case OP_SCBRA:
1208
1.00k
      case OP_BRAPOS:
1209
1.00k
      case OP_SBRAPOS:
1210
1.07k
      case OP_CBRAPOS:
1211
1.32k
      case OP_SCBRAPOS:
1212
1.36k
      case OP_ONCE:
1213
1.36k
      case OP_SCRIPT_RUN:
1214
1.36k
      rc = set_start_bits(re, tcode, utf, ucp, depthptr);
1215
1.36k
      if (rc == SSB_DONE)
1216
300
        {
1217
300
        try_next = FALSE;
1218
300
        }
1219
1.06k
      else if (rc == SSB_CONTINUE)
1220
1.04k
        {
1221
1.49k
        do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1222
1.04k
        tcode += 1 + LINK_SIZE;
1223
1.04k
        }
1224
18
      else return rc;   /* FAIL, UNKNOWN, or TOODEEP */
1225
1.34k
      break;
1226
1227
      /* If we hit ALT or KET, it means we haven't found anything mandatory in
1228
      this branch, though we might have found something optional. For ALT, we
1229
      continue with the next alternative, but we have to arrange that the final
1230
      result from subpattern is SSB_CONTINUE rather than SSB_DONE. For KET,
1231
      return SSB_CONTINUE: if this is the top level, that indicates failure,
1232
      but after a nested subpattern, it causes scanning to continue. */
1233
1234
1.34k
      case OP_ALT:
1235
999
      yield = SSB_CONTINUE;
1236
999
      try_next = FALSE;
1237
999
      break;
1238
1239
478
      case OP_KET:
1240
749
      case OP_KETRMAX:
1241
750
      case OP_KETRMIN:
1242
993
      case OP_KETRPOS:
1243
993
      return SSB_CONTINUE;
1244
1245
      /* Skip over callout */
1246
1247
0
      case OP_CALLOUT:
1248
0
      tcode += PRIV(OP_lengths)[OP_CALLOUT];
1249
0
      break;
1250
1251
0
      case OP_CALLOUT_STR:
1252
0
      tcode += GET(tcode, 1 + 2*LINK_SIZE);
1253
0
      break;
1254
1255
      /* Skip over lookbehind and negative lookahead assertions */
1256
1257
7
      case OP_ASSERT_NOT:
1258
9
      case OP_ASSERTBACK:
1259
9
      case OP_ASSERTBACK_NOT:
1260
13
      case OP_ASSERTBACK_NA:
1261
99
      do tcode += GET(tcode, 1); while (*tcode == OP_ALT);
1262
13
      tcode += 1 + LINK_SIZE;
1263
13
      break;
1264
1265
      /* BRAZERO does the bracket, but carries on. */
1266
1267
14
      case OP_BRAZERO:
1268
15
      case OP_BRAMINZERO:
1269
18
      case OP_BRAPOSZERO:
1270
18
      rc = set_start_bits(re, ++tcode, utf, ucp, depthptr);
1271
18
      if (rc == SSB_FAIL || rc == SSB_UNKNOWN || rc == SSB_TOODEEP) return rc;
1272
26
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1273
14
      tcode += 1 + LINK_SIZE;
1274
14
      break;
1275
1276
      /* SKIPZERO skips the bracket. */
1277
1278
0
      case OP_SKIPZERO:
1279
0
      tcode++;
1280
0
      do tcode += GET(tcode,1); while (*tcode == OP_ALT);
1281
0
      tcode += 1 + LINK_SIZE;
1282
0
      break;
1283
1284
      /* Single-char * or ? sets the bit and tries the next item */
1285
1286
67
      case OP_STAR:
1287
76
      case OP_MINSTAR:
1288
349
      case OP_POSSTAR:
1289
674
      case OP_QUERY:
1290
939
      case OP_MINQUERY:
1291
1.38k
      case OP_POSQUERY:
1292
1.38k
      tcode = set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1293
1.38k
      break;
1294
1295
23
      case OP_STARI:
1296
75
      case OP_MINSTARI:
1297
777
      case OP_POSSTARI:
1298
1.27k
      case OP_QUERYI:
1299
1.55k
      case OP_MINQUERYI:
1300
2.03k
      case OP_POSQUERYI:
1301
2.03k
      tcode = set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1302
2.03k
      break;
1303
1304
      /* Single-char upto sets the bit and tries the next */
1305
1306
0
      case OP_UPTO:
1307
0
      case OP_MINUPTO:
1308
0
      case OP_POSUPTO:
1309
0
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, FALSE, utf, ucp);
1310
0
      break;
1311
1312
0
      case OP_UPTOI:
1313
0
      case OP_MINUPTOI:
1314
0
      case OP_POSUPTOI:
1315
0
      tcode = set_table_bit(re, tcode + 1 + IMM2_SIZE, TRUE, utf, ucp);
1316
0
      break;
1317
1318
      /* At least one single char sets the bit and stops */
1319
1320
0
      case OP_EXACT:
1321
0
      tcode += IMM2_SIZE;
1322
      /* Fall through */
1323
4.45k
      case OP_CHAR:
1324
4.46k
      case OP_PLUS:
1325
4.49k
      case OP_MINPLUS:
1326
4.85k
      case OP_POSPLUS:
1327
4.85k
      (void)set_table_bit(re, tcode + 1, FALSE, utf, ucp);
1328
4.85k
      try_next = FALSE;
1329
4.85k
      break;
1330
1331
12
      case OP_EXACTI:
1332
12
      tcode += IMM2_SIZE;
1333
      /* Fall through */
1334
3.60k
      case OP_CHARI:
1335
3.66k
      case OP_PLUSI:
1336
3.66k
      case OP_MINPLUSI:
1337
3.70k
      case OP_POSPLUSI:
1338
3.70k
      (void)set_table_bit(re, tcode + 1, TRUE, utf, ucp);
1339
3.70k
      try_next = FALSE;
1340
3.70k
      break;
1341
1342
      /* Special spacing and line-terminating items. These recognize specific
1343
      lists of characters. The difference between VSPACE and ANYNL is that the
1344
      latter can match the two-character CRLF sequence, but that is not
1345
      relevant for finding the first character, so their code here is
1346
      identical. */
1347
1348
117
      case OP_HSPACE:
1349
117
      SET_BIT(CHAR_HT);
1350
117
      SET_BIT(CHAR_SPACE);
1351
1352
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1353
      the bits for 0xA0 and for code units >= 255, independently of UTF. */
1354
1355
#if PCRE2_CODE_UNIT_WIDTH != 8
1356
      SET_BIT(0xA0);
1357
      SET_BIT(0xFF);
1358
#else
1359
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1360
      units of horizontal space characters. */
1361
1362
117
#ifdef SUPPORT_UNICODE
1363
117
      if (utf)
1364
3
        {
1365
3
        SET_BIT(0xC2);  /* For U+00A0 */
1366
3
        SET_BIT(0xE1);  /* For U+1680, U+180E */
1367
3
        SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1368
3
        SET_BIT(0xE3);  /* For U+3000 */
1369
3
        }
1370
114
      else
1371
114
#endif
1372
      /* For the 8-bit library not in UTF-8 mode, set the bit for 0xA0, unless
1373
      the code is EBCDIC. */
1374
114
        {
1375
114
#ifndef EBCDIC
1376
114
        SET_BIT(0xA0);
1377
114
#endif  /* Not EBCDIC */
1378
114
        }
1379
117
#endif  /* 8-bit support */
1380
1381
117
      try_next = FALSE;
1382
117
      break;
1383
1384
361
      case OP_ANYNL:
1385
620
      case OP_VSPACE:
1386
620
      SET_BIT(CHAR_LF);
1387
620
      SET_BIT(CHAR_VT);
1388
620
      SET_BIT(CHAR_FF);
1389
620
      SET_BIT(CHAR_CR);
1390
1391
      /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1392
      the bits for NEL and for code units >= 255, independently of UTF. */
1393
1394
#if PCRE2_CODE_UNIT_WIDTH != 8
1395
      SET_BIT(CHAR_NEL);
1396
      SET_BIT(0xFF);
1397
#else
1398
      /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1399
      units of vertical space characters. */
1400
1401
620
#ifdef SUPPORT_UNICODE
1402
620
      if (utf)
1403
269
        {
1404
269
        SET_BIT(0xC2);  /* For U+0085 (NEL) */
1405
269
        SET_BIT(0xE2);  /* For U+2028, U+2029 */
1406
269
        }
1407
351
      else
1408
351
#endif
1409
      /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1410
351
        {
1411
351
        SET_BIT(CHAR_NEL);
1412
351
        }
1413
620
#endif  /* 8-bit support */
1414
1415
620
      try_next = FALSE;
1416
620
      break;
1417
1418
      /* Single character types set the bits and stop. Note that if PCRE2_UCP
1419
      is set, we do not see these opcodes because \d etc are converted to
1420
      properties. Therefore, these apply in the case when only characters less
1421
      than 256 are recognized to match the types. */
1422
1423
10
      case OP_NOT_DIGIT:
1424
10
      set_nottype_bits(re, cbit_digit, table_limit);
1425
10
      try_next = FALSE;
1426
10
      break;
1427
1428
62
      case OP_DIGIT:
1429
62
      set_type_bits(re, cbit_digit, table_limit);
1430
62
      try_next = FALSE;
1431
62
      break;
1432
1433
27
      case OP_NOT_WHITESPACE:
1434
27
      set_nottype_bits(re, cbit_space, table_limit);
1435
27
      try_next = FALSE;
1436
27
      break;
1437
1438
147
      case OP_WHITESPACE:
1439
147
      set_type_bits(re, cbit_space, table_limit);
1440
147
      try_next = FALSE;
1441
147
      break;
1442
1443
109
      case OP_NOT_WORDCHAR:
1444
109
      set_nottype_bits(re, cbit_word, table_limit);
1445
109
      try_next = FALSE;
1446
109
      break;
1447
1448
1.57k
      case OP_WORDCHAR:
1449
1.57k
      set_type_bits(re, cbit_word, table_limit);
1450
1.57k
      try_next = FALSE;
1451
1.57k
      break;
1452
1453
      /* One or more character type fudges the pointer and restarts, knowing
1454
      it will hit a single character type and stop there. */
1455
1456
356
      case OP_TYPEPLUS:
1457
527
      case OP_TYPEMINPLUS:
1458
822
      case OP_TYPEPOSPLUS:
1459
822
      tcode++;
1460
822
      break;
1461
1462
7
      case OP_TYPEEXACT:
1463
7
      tcode += 1 + IMM2_SIZE;
1464
7
      break;
1465
1466
      /* Zero or more repeats of character types set the bits and then
1467
      try again. */
1468
1469
0
      case OP_TYPEUPTO:
1470
0
      case OP_TYPEMINUPTO:
1471
0
      case OP_TYPEPOSUPTO:
1472
0
      tcode += IMM2_SIZE;  /* Fall through */
1473
1474
28
      case OP_TYPESTAR:
1475
50
      case OP_TYPEMINSTAR:
1476
72
      case OP_TYPEPOSSTAR:
1477
510
      case OP_TYPEQUERY:
1478
609
      case OP_TYPEMINQUERY:
1479
1.66k
      case OP_TYPEPOSQUERY:
1480
1.66k
      switch(tcode[1])
1481
1.66k
        {
1482
78
        default:
1483
90
        case OP_ANY:
1484
90
        case OP_ALLANY:
1485
90
        return SSB_FAIL;
1486
1487
21
        case OP_HSPACE:
1488
21
        SET_BIT(CHAR_HT);
1489
21
        SET_BIT(CHAR_SPACE);
1490
1491
        /* For the 16-bit and 32-bit libraries (which can never be EBCDIC), set
1492
        the bits for 0xA0 and for code units >= 255, independently of UTF. */
1493
1494
#if PCRE2_CODE_UNIT_WIDTH != 8
1495
        SET_BIT(0xA0);
1496
        SET_BIT(0xFF);
1497
#else
1498
        /* For the 8-bit library in UTF-8 mode, set the bits for the first code
1499
        units of horizontal space characters. */
1500
1501
21
#ifdef SUPPORT_UNICODE
1502
21
        if (utf)
1503
1
          {
1504
1
          SET_BIT(0xC2);  /* For U+00A0 */
1505
1
          SET_BIT(0xE1);  /* For U+1680, U+180E */
1506
1
          SET_BIT(0xE2);  /* For U+2000 - U+200A, U+202F, U+205F */
1507
1
          SET_BIT(0xE3);  /* For U+3000 */
1508
1
          }
1509
20
        else
1510
20
#endif
1511
        /* For the 8-bit library not in UTF-8 mode, set the bit for 0xA0, unless
1512
        the code is EBCDIC. */
1513
20
          {
1514
20
#ifndef EBCDIC
1515
20
          SET_BIT(0xA0);
1516
20
#endif  /* Not EBCDIC */
1517
20
          }
1518
21
#endif  /* 8-bit support */
1519
21
        break;
1520
1521
107
        case OP_ANYNL:
1522
251
        case OP_VSPACE:
1523
251
        SET_BIT(CHAR_LF);
1524
251
        SET_BIT(CHAR_VT);
1525
251
        SET_BIT(CHAR_FF);
1526
251
        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
#if PCRE2_CODE_UNIT_WIDTH != 8
1532
        SET_BIT(CHAR_NEL);
1533
        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
251
#ifdef SUPPORT_UNICODE
1539
251
        if (utf)
1540
143
          {
1541
143
          SET_BIT(0xC2);  /* For U+0085 (NEL) */
1542
143
          SET_BIT(0xE2);  /* For U+2028, U+2029 */
1543
143
          }
1544
108
        else
1545
108
#endif
1546
        /* For the 8-bit library not in UTF-8 mode, set the bit for NEL. */
1547
108
          {
1548
108
          SET_BIT(CHAR_NEL);
1549
108
          }
1550
251
#endif  /* 8-bit support */
1551
251
        break;
1552
1553
0
        case OP_NOT_DIGIT:
1554
0
        set_nottype_bits(re, cbit_digit, table_limit);
1555
0
        break;
1556
1557
298
        case OP_DIGIT:
1558
298
        set_type_bits(re, cbit_digit, table_limit);
1559
298
        break;
1560
1561
39
        case OP_NOT_WHITESPACE:
1562
39
        set_nottype_bits(re, cbit_space, table_limit);
1563
39
        break;
1564
1565
129
        case OP_WHITESPACE:
1566
129
        set_type_bits(re, cbit_space, table_limit);
1567
129
        break;
1568
1569
5
        case OP_NOT_WORDCHAR:
1570
5
        set_nottype_bits(re, cbit_word, table_limit);
1571
5
        break;
1572
1573
836
        case OP_WORDCHAR:
1574
836
        set_type_bits(re, cbit_word, table_limit);
1575
836
        break;
1576
1.66k
        }
1577
1578
1.57k
      tcode += 2;
1579
1.57k
      break;
1580
1581
      /* Extended class: if there are any property checks, or if this is a
1582
      negative XCLASS without a map, give up. If there are no property checks,
1583
      there must be wide characters on the XCLASS list, because otherwise an
1584
      XCLASS would not have been created. This means that code points >= 255
1585
      are potential starters. In the UTF-8 case we can scan them and set bits
1586
      for the relevant leading bytes. */
1587
1588
0
#ifdef SUPPORT_WIDE_CHARS
1589
219
      case OP_XCLASS:
1590
219
      xclassflags = tcode[1 + LINK_SIZE];
1591
219
      if ((xclassflags & XCL_HASPROP) != 0 ||
1592
158
          (xclassflags & (XCL_MAP|XCL_NOT)) == XCL_NOT)
1593
61
        return SSB_FAIL;
1594
1595
      /* We have a positive XCLASS or a negative one without a map. Set up the
1596
      map pointer if there is one, and fall through. */
1597
1598
158
      classmap = ((xclassflags & XCL_MAP) == 0)? NULL :
1599
158
        (uint8_t *)(tcode + 1 + LINK_SIZE + 1);
1600
1601
      /* In UTF-8 mode, scan the character list and set bits for leading bytes,
1602
      then jump to handle the map. */
1603
1604
158
#if PCRE2_CODE_UNIT_WIDTH == 8
1605
158
      if (utf && (xclassflags & XCL_NOT) == 0)
1606
84
        {
1607
84
        PCRE2_UCHAR b, e;
1608
84
        PCRE2_SPTR p = tcode + 1 + LINK_SIZE + 1 + ((classmap == NULL)? 0:32);
1609
84
        tcode += GET(tcode, 1);
1610
1611
250
        for (;;) switch (*p++)
1612
250
          {
1613
117
          case XCL_SINGLE:
1614
117
          b = *p++;
1615
322
          while ((*p & 0xc0) == 0x80) p++;
1616
117
          re->start_bitmap[b/8] |= (1u << (b&7));
1617
117
          break;
1618
1619
49
          case XCL_RANGE:
1620
49
          b = *p++;
1621
140
          while ((*p & 0xc0) == 0x80) p++;
1622
49
          e = *p++;
1623
152
          while ((*p & 0xc0) == 0x80) p++;
1624
381
          for (; b <= e; b++)
1625
332
            re->start_bitmap[b/8] |= (1u << (b&7));
1626
49
          break;
1627
1628
84
          case XCL_END:
1629
84
          goto HANDLE_CLASSMAP;
1630
1631
0
          default:
1632
0
          return SSB_UNKNOWN;   /* Internal error, should not occur */
1633
250
          }
1634
84
        }
1635
74
#endif  /* SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8 */
1636
74
#endif  /* SUPPORT_WIDE_CHARS */
1637
1638
      /* It seems that the fall through comment must be outside the #ifdef if
1639
      it is to avoid the gcc compiler warning. */
1640
1641
      /* Fall through */
1642
1643
      /* Enter here for a negative non-XCLASS. In the 8-bit library, if we are
1644
      in UTF mode, any byte with a value >= 0xc4 is a potentially valid starter
1645
      because it starts a character with a value > 255. In 8-bit non-UTF mode,
1646
      there is no difference between CLASS and NCLASS. In all other wide
1647
      character modes, set the 0xFF bit to indicate code units >= 255. */
1648
1649
939
      case OP_NCLASS:
1650
939
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1651
939
      if (utf)
1652
450
        {
1653
450
        re->start_bitmap[24] |= 0xf0;            /* Bits for 0xc4 - 0xc8 */
1654
450
        memset(re->start_bitmap+25, 0xff, 7);    /* Bits for 0xc9 - 0xff */
1655
450
        }
1656
#elif PCRE2_CODE_UNIT_WIDTH != 8
1657
      SET_BIT(0xFF);                             /* For characters >= 255 */
1658
#endif
1659
      /* Fall through */
1660
1661
      /* Enter here for a positive non-XCLASS. If we have fallen through from
1662
      an XCLASS, classmap will already be set; just advance the code pointer.
1663
      Otherwise, set up classmap for a a non-XCLASS and advance past it. */
1664
1665
2.84k
      case OP_CLASS:
1666
2.84k
      if (*tcode == OP_XCLASS) tcode += GET(tcode, 1); else
1667
2.76k
        {
1668
2.76k
        classmap = (uint8_t *)(++tcode);
1669
2.76k
        tcode += 32 / sizeof(PCRE2_UCHAR);
1670
2.76k
        }
1671
1672
      /* When wide characters are supported, classmap may be NULL. In UTF-8
1673
      (sic) mode, the bits in a class bit map correspond to character values,
1674
      not to byte values. However, the bit map we are constructing is for byte
1675
      values. So we have to do a conversion for characters whose code point is
1676
      greater than 127. In fact, there are only two possible starting bytes for
1677
      characters in the range 128 - 255. */
1678
1679
2.84k
#if defined SUPPORT_WIDE_CHARS && PCRE2_CODE_UNIT_WIDTH == 8
1680
2.92k
      HANDLE_CLASSMAP:
1681
2.92k
#endif
1682
2.92k
      if (classmap != NULL)
1683
2.92k
        {
1684
2.92k
#if defined SUPPORT_UNICODE && PCRE2_CODE_UNIT_WIDTH == 8
1685
2.92k
        if (utf)
1686
762
          {
1687
12.9k
          for (c = 0; c < 16; c++) re->start_bitmap[c] |= classmap[c];
1688
40.9k
          for (c = 128; c < 256; c++)
1689
40.1k
            {
1690
40.1k
            if ((classmap[c/8] & (1u << (c&7))) != 0)
1691
911
              {
1692
911
              int d = (c >> 6) | 0xc0;                 /* Set bit for this starter */
1693
911
              re->start_bitmap[d/8] |= (1u << (d&7));  /* and then skip on to the */
1694
911
              c = (c & 0xc0) + 0x40 - 1;               /* next relevant character. */
1695
911
              }
1696
40.1k
            }
1697
762
          }
1698
2.16k
        else
1699
2.16k
#endif
1700
        /* In all modes except UTF-8, the two bit maps are compatible. */
1701
1702
2.16k
          {
1703
71.3k
          for (c = 0; c < 32; c++) re->start_bitmap[c] |= classmap[c];
1704
2.16k
          }
1705
2.92k
        }
1706
1707
      /* Act on what follows the class. For a zero minimum repeat, continue;
1708
      otherwise stop processing. */
1709
1710
2.92k
      switch (*tcode)
1711
2.92k
        {
1712
396
        case OP_CRSTAR:
1713
680
        case OP_CRMINSTAR:
1714
847
        case OP_CRQUERY:
1715
1.11k
        case OP_CRMINQUERY:
1716
1.57k
        case OP_CRPOSSTAR:
1717
1.78k
        case OP_CRPOSQUERY:
1718
1.78k
        tcode++;
1719
1.78k
        break;
1720
1721
0
        case OP_CRRANGE:
1722
0
        case OP_CRMINRANGE:
1723
0
        case OP_CRPOSRANGE:
1724
0
        if (GET2(tcode, 1) == 0) tcode += 1 + 2 * IMM2_SIZE;
1725
0
          else try_next = FALSE;
1726
0
        break;
1727
1728
1.14k
        default:
1729
1.14k
        try_next = FALSE;
1730
1.14k
        break;
1731
2.92k
        }
1732
2.92k
      break; /* End of class handling case */
1733
25.0k
      }      /* End of switch for opcodes */
1734
25.0k
    }        /* End of try_next loop */
1735
1736
13.6k
  code += GET(code, 1);   /* Advance to next branch */
1737
13.6k
  }
1738
13.6k
while (*code == OP_ALT);
1739
1740
1.32k
return yield;
1741
3.34k
}
1742
1743
1744
1745
/*************************************************
1746
*          Study a compiled expression           *
1747
*************************************************/
1748
1749
/* This function is handed a compiled expression that it must study to produce
1750
information that will speed up the matching.
1751
1752
Argument:
1753
  re       points to the compiled expression
1754
1755
Returns:   0 normally; non-zero should never normally occur
1756
           1 unknown opcode in set_start_bits
1757
           2 missing capturing bracket
1758
           3 unknown opcode in find_minlength
1759
*/
1760
1761
int
1762
PRIV(study)(pcre2_real_code *re)
1763
2.22k
{
1764
2.22k
int count = 0;
1765
2.22k
PCRE2_UCHAR *code;
1766
2.22k
BOOL utf = (re->overall_options & PCRE2_UTF) != 0;
1767
2.22k
BOOL ucp = (re->overall_options & PCRE2_UCP) != 0;
1768
1769
/* Find start of compiled code */
1770
1771
2.22k
code = (PCRE2_UCHAR *)((uint8_t *)re + sizeof(pcre2_real_code)) +
1772
2.22k
  re->name_entry_size * re->name_count;
1773
1774
/* For a pattern that has a first code unit, or a multiline pattern that
1775
matches only at "line start", there is no point in seeking a list of starting
1776
code units. */
1777
1778
2.22k
if ((re->flags & (PCRE2_FIRSTSET|PCRE2_STARTLINE)) == 0)
1779
1.96k
  {
1780
1.96k
  int depth = 0;
1781
1.96k
  int rc = set_start_bits(re, code, utf, ucp, &depth);
1782
1.96k
  if (rc == SSB_UNKNOWN) return 1;
1783
1784
  /* If a list of starting code units was set up, scan the list to see if only
1785
  one or two were listed. Having only one listed is rare because usually a
1786
  single starting code unit will have been recognized and PCRE2_FIRSTSET set.
1787
  If two are listed, see if they are caseless versions of the same character;
1788
  if so we can replace the list with a caseless first code unit. This gives
1789
  better performance and is plausibly worth doing for patterns such as [Ww]ord
1790
  or (word|WORD). */
1791
1792
1.96k
  if (rc == SSB_DONE)
1793
921
    {
1794
921
    int i;
1795
921
    int a = -1;
1796
921
    int b = -1;
1797
921
    uint8_t *p = re->start_bitmap;
1798
921
    uint32_t flags = PCRE2_FIRSTMAPSET;
1799
1800
4.67k
    for (i = 0; i < 256; p++, i += 8)
1801
4.66k
      {
1802
4.66k
      uint8_t x = *p;
1803
4.66k
      if (x != 0)
1804
1.33k
        {
1805
1.33k
        int c;
1806
1.33k
        uint8_t y = x & (~x + 1);   /* Least significant bit */
1807
1.33k
        if (y != x) goto DONE;      /* More than one bit set */
1808
1809
        /* In the 16-bit and 32-bit libraries, the bit for 0xff means "0xff and
1810
        all wide characters", so we cannot use it here. */
1811
1812
#if PCRE2_CODE_UNIT_WIDTH != 8
1813
        if (i == 248 && x == 0x80) goto DONE;
1814
#endif
1815
1816
        /* Compute the character value */
1817
1818
729
        c = i;
1819
729
        switch (x)
1820
729
          {
1821
291
          case 1:   break;
1822
116
          case 2:   c += 1; break;  case 4:  c += 2; break;
1823
63
          case 8:   c += 3; break;  case 16: c += 4; break;
1824
77
          case 32:  c += 5; break;  case 64: c += 6; break;
1825
51
          case 128: c += 7; break;
1826
729
          }
1827
1828
        /* c contains the code unit value, in the range 0-255. In 8-bit UTF
1829
        mode, only values < 128 can be used. In all the other cases, c is a
1830
        character value. */
1831
1832
729
#if PCRE2_CODE_UNIT_WIDTH == 8
1833
729
        if (utf && c > 127) goto DONE;
1834
727
#endif
1835
727
        if (a < 0) a = c;   /* First one found, save in a */
1836
313
        else if (b < 0)     /* Second one found */
1837
304
          {
1838
304
          int d = TABLE_GET((unsigned int)c, re->tables + fcc_offset, c);
1839
1840
304
#ifdef SUPPORT_UNICODE
1841
304
          if (utf || ucp)
1842
44
            {
1843
44
            if (UCD_CASESET(c) != 0) goto DONE;     /* Multiple case set */
1844
41
            if (c > 127) d = UCD_OTHERCASE(c);
1845
41
            }
1846
301
#endif  /* SUPPORT_UNICODE */
1847
1848
301
          if (d != a) goto DONE;   /* Not the other case of a */
1849
13
          b = c;                   /* Save second in b */
1850
13
          }
1851
9
        else goto DONE;   /* More than two characters found */
1852
727
        }
1853
4.66k
      }
1854
1855
    /* Replace the start code unit bits with a first code unit, but only if it
1856
    is not the same as a required later code unit. This is because a search for
1857
    a required code unit starts after an explicit first code unit, but at a
1858
    code unit found from the bitmap. Patterns such as /a*a/ don't work
1859
    if both the start unit and required unit are the same. */
1860
1861
10
    if (a >= 0 &&
1862
10
        (
1863
10
        (re->flags & PCRE2_LASTSET) == 0 ||
1864
8
          (
1865
8
          re->last_codeunit != (uint32_t)a &&
1866
6
          (b < 0 || re->last_codeunit != (uint32_t)b)
1867
8
          )
1868
10
        ))
1869
8
      {
1870
8
      re->first_codeunit = a;
1871
8
      flags = PCRE2_FIRSTSET;
1872
8
      if (b >= 0) flags |= PCRE2_FIRSTCASELESS;
1873
8
      }
1874
1875
921
    DONE:
1876
921
    re->flags |= flags;
1877
921
    }
1878
1.96k
  }
1879
1880
/* Find the minimum length of subject string. If the pattern can match an empty
1881
string, the minimum length is already known. If the pattern contains (*ACCEPT)
1882
all bets are off, and we don't even try to find a minimum length. If there are
1883
more back references than the size of the vector we are going to cache them in,
1884
do nothing. A pattern that complicated will probably take a long time to
1885
analyze and may in any case turn out to be too complicated. Note that back
1886
reference minima are held as 16-bit numbers. */
1887
1888
2.22k
if ((re->flags & (PCRE2_MATCH_EMPTY|PCRE2_HASACCEPT)) == 0 &&
1889
2.13k
     re->top_backref <= MAX_CACHE_BACKREF)
1890
2.13k
  {
1891
2.13k
  int min;
1892
2.13k
  int backref_cache[MAX_CACHE_BACKREF+1];
1893
2.13k
  backref_cache[0] = 0;    /* Highest one that is set */
1894
2.13k
  min = find_minlength(re, code, code, utf, NULL, &count, backref_cache);
1895
2.13k
  switch(min)
1896
2.13k
    {
1897
0
    case -1:  /* \C in UTF mode or over-complex regex */
1898
0
    break;    /* Leave minlength unchanged (will be zero) */
1899
1900
0
    case -2:
1901
0
    return 2; /* missing capturing bracket */
1902
1903
0
    case -3:
1904
0
    return 3; /* unrecognized opcode */
1905
1906
2.13k
    default:
1907
2.13k
    re->minlength = (min > UINT16_MAX)? UINT16_MAX : min;
1908
2.13k
    break;
1909
2.13k
    }
1910
2.13k
  }
1911
1912
2.22k
return 0;
1913
2.22k
}
1914
1915
/* End of pcre2_study.c */