Coverage Report

Created: 2026-12-01 06:16

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