Coverage Report

Created: 2025-09-27 06:26

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