Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/pcre/pcre2lib/pcre2_auto_possess.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-2022 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 that scan a compiled pattern and change
42
repeats into possessive repeats where possible. */
43
44
45
#ifdef HAVE_CONFIG_H
46
#include "config.h"
47
#endif
48
49
50
#include "pcre2_internal.h"
51
52
53
/*************************************************
54
*        Tables for auto-possessification        *
55
*************************************************/
56
57
/* This table is used to check whether auto-possessification is possible
58
between adjacent character-type opcodes. The left-hand (repeated) opcode is
59
used to select the row, and the right-hand opcode is use to select the column.
60
A value of 1 means that auto-possessification is OK. For example, the second
61
value in the first row means that \D+\d can be turned into \D++\d.
62
63
The Unicode property types (\P and \p) have to be present to fill out the table
64
because of what their opcode values are, but the table values should always be
65
zero because property types are handled separately in the code. The last four
66
columns apply to items that cannot be repeated, so there is no need to have
67
rows for them. Note that OP_DIGIT etc. are generated only when PCRE_UCP is
68
*not* set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
69
70
#define APTROWS (LAST_AUTOTAB_LEFT_OP - FIRST_AUTOTAB_OP + 1)
71
#define APTCOLS (LAST_AUTOTAB_RIGHT_OP - FIRST_AUTOTAB_OP + 1)
72
73
static const uint8_t autoposstab[APTROWS][APTCOLS] = {
74
/* \D \d \S \s \W \w  . .+ \C \P \p \R \H \h \V \v \X \Z \z  $ $M */
75
  { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \D */
76
  { 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \d */
77
  { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \S */
78
  { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \s */
79
  { 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \W */
80
  { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1 },  /* \w */
81
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .  */
82
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* .+ */
83
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },  /* \C */
84
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \P */
85
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* \p */
86
  { 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \R */
87
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 },  /* \H */
88
  { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \h */
89
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0 },  /* \V */
90
  { 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0 },  /* \v */
91
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }   /* \X */
92
};
93
94
#ifdef SUPPORT_UNICODE
95
/* This table is used to check whether auto-possessification is possible
96
between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP). The
97
left-hand (repeated) opcode is used to select the row, and the right-hand
98
opcode is used to select the column. The values are as follows:
99
100
  0   Always return FALSE (never auto-possessify)
101
  1   Character groups are distinct (possessify if both are OP_PROP)
102
  2   Check character categories in the same group (general or particular)
103
  3   TRUE if the two opcodes are not the same (PROP vs NOTPROP)
104
105
  4   Check left general category vs right particular category
106
  5   Check right general category vs left particular category
107
108
  6   Left alphanum vs right general category
109
  7   Left space vs right general category
110
  8   Left word vs right general category
111
112
  9   Right alphanum vs left general category
113
 10   Right space vs left general category
114
 11   Right word vs left general category
115
116
 12   Left alphanum vs right particular category
117
 13   Left space vs right particular category
118
 14   Left word vs right particular category
119
120
 15   Right alphanum vs left particular category
121
 16   Right space vs left particular category
122
 17   Right word vs left particular category
123
*/
124
125
static const uint8_t propposstab[PT_TABSIZE][PT_TABSIZE] = {
126
/* ANY LAMP GC  PC  SC  SCX ALNUM SPACE PXSPACE WORD CLIST UCNC BIDICL BOOL */
127
  { 0,  0,  0,  0,  0,   0,    0,    0,      0,   0,    0,   0,    0,    0 },  /* PT_ANY */
128
  { 0,  3,  0,  0,  0,   0,    3,    1,      1,   0,    0,   0,    0,    0 },  /* PT_LAMP */
129
  { 0,  0,  2,  4,  0,   0,    9,   10,     10,  11,    0,   0,    0,    0 },  /* PT_GC */
130
  { 0,  0,  5,  2,  0,   0,   15,   16,     16,  17,    0,   0,    0,    0 },  /* PT_PC */
131
  { 0,  0,  0,  0,  2,   2,    0,    0,      0,   0,    0,   0,    0,    0 },  /* PT_SC */
132
  { 0,  0,  0,  0,  2,   2,    0,    0,      0,   0,    0,   0,    0,    0 },  /* PT_SCX */
133
  { 0,  3,  6, 12,  0,   0,    3,    1,      1,   0,    0,   0,    0,    0 },  /* PT_ALNUM */
134
  { 0,  1,  7, 13,  0,   0,    1,    3,      3,   1,    0,   0,    0,    0 },  /* PT_SPACE */
135
  { 0,  1,  7, 13,  0,   0,    1,    3,      3,   1,    0,   0,    0,    0 },  /* PT_PXSPACE */
136
  { 0,  0,  8, 14,  0,   0,    0,    1,      1,   3,    0,   0,    0,    0 },  /* PT_WORD */
137
  { 0,  0,  0,  0,  0,   0,    0,    0,      0,   0,    0,   0,    0,    0 },  /* PT_CLIST */
138
  { 0,  0,  0,  0,  0,   0,    0,    0,      0,   0,    0,   3,    0,    0 },  /* PT_UCNC */
139
  { 0,  0,  0,  0,  0,   0,    0,    0,      0,   0,    0,   0,    0,    0 },  /* PT_BIDICL */
140
  { 0,  0,  0,  0,  0,   0,    0,    0,      0,   0,    0,   0,    0,    0 }   /* PT_BOOL */
141
};
142
143
/* This table is used to check whether auto-possessification is possible
144
between adjacent Unicode property opcodes (OP_PROP and OP_NOTPROP) when one
145
specifies a general category and the other specifies a particular category. The
146
row is selected by the general category and the column by the particular
147
category. The value is 1 if the particular category is not part of the general
148
category. */
149
150
static const uint8_t catposstab[7][30] = {
151
/* Cc Cf Cn Co Cs Ll Lm Lo Lt Lu Mc Me Mn Nd Nl No Pc Pd Pe Pf Pi Po Ps Sc Sk Sm So Zl Zp Zs */
152
  { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* C */
153
  { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* L */
154
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* M */
155
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },  /* N */
156
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },  /* P */
157
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1 },  /* S */
158
  { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 }   /* Z */
159
};
160
161
/* This table is used when checking ALNUM, (PX)SPACE, SPACE, and WORD against
162
a general or particular category. The properties in each row are those
163
that apply to the character set in question. Duplication means that a little
164
unnecessary work is done when checking, but this keeps things much simpler
165
because they can all use the same code. For more details see the comment where
166
this table is used.
167
168
Note: SPACE and PXSPACE used to be different because Perl excluded VT from
169
"space", but from Perl 5.18 it's included, so both categories are treated the
170
same here. */
171
172
static const uint8_t posspropstab[3][4] = {
173
  { ucp_L, ucp_N, ucp_N, ucp_Nl },  /* ALNUM, 3rd and 4th values redundant */
174
  { ucp_Z, ucp_Z, ucp_C, ucp_Cc },  /* SPACE and PXSPACE, 2nd value redundant */
175
  { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
176
};
177
#endif  /* SUPPORT_UNICODE */
178
179
180
181
#ifdef SUPPORT_UNICODE
182
/*************************************************
183
*        Check a character and a property        *
184
*************************************************/
185
186
/* This function is called by compare_opcodes() when a property item is
187
adjacent to a fixed character.
188
189
Arguments:
190
  c            the character
191
  ptype        the property type
192
  pdata        the data for the type
193
  negated      TRUE if it's a negated property (\P or \p{^)
194
195
Returns:       TRUE if auto-possessifying is OK
196
*/
197
198
static BOOL
199
check_char_prop(uint32_t c, unsigned int ptype, unsigned int pdata,
200
  BOOL negated)
201
397
{
202
397
BOOL ok;
203
397
const uint32_t *p;
204
397
const ucd_record *prop = GET_UCD(c);
205
206
397
switch(ptype)
207
397
  {
208
0
  case PT_LAMP:
209
0
  return (prop->chartype == ucp_Lu ||
210
0
          prop->chartype == ucp_Ll ||
211
0
          prop->chartype == ucp_Lt) == negated;
212
213
36
  case PT_GC:
214
36
  return (pdata == PRIV(ucp_gentype)[prop->chartype]) == negated;
215
216
35
  case PT_PC:
217
35
  return (pdata == prop->chartype) == negated;
218
219
0
  case PT_SC:
220
0
  return (pdata == prop->script) == negated;
221
222
0
  case PT_SCX:
223
0
  ok = (pdata == prop->script
224
0
        || MAPBIT(PRIV(ucd_script_sets) + UCD_SCRIPTX_PROP(prop), pdata) != 0);
225
0
  return ok == negated;
226
227
  /* These are specials */
228
229
0
  case PT_ALNUM:
230
0
  return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
231
0
          PRIV(ucp_gentype)[prop->chartype] == ucp_N) == negated;
232
233
  /* Perl space used to exclude VT, but from Perl 5.18 it is included, which
234
  means that Perl space and POSIX space are now identical. PCRE was changed
235
  at release 8.34. */
236
237
36
  case PT_SPACE:    /* Perl space */
238
36
  case PT_PXSPACE:  /* POSIX space */
239
36
  switch(c)
240
36
    {
241
0
    HSPACE_CASES:
242
0
    VSPACE_CASES:
243
0
    return negated;
244
245
36
    default:
246
36
    return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
247
36
    }
248
0
  break;  /* Control never reaches here */
249
250
290
  case PT_WORD:
251
290
  return (PRIV(ucp_gentype)[prop->chartype] == ucp_L ||
252
223
          PRIV(ucp_gentype)[prop->chartype] == ucp_N ||
253
210
          c == CHAR_UNDERSCORE) == negated;
254
255
0
  case PT_CLIST:
256
0
  p = PRIV(ucd_caseless_sets) + prop->caseset;
257
0
  for (;;)
258
0
    {
259
0
    if (c < *p) return !negated;
260
0
    if (c == *p++) return negated;
261
0
    }
262
0
  break;  /* Control never reaches here */
263
264
  /* Haven't yet thought these through. */
265
266
0
  case PT_BIDICL:
267
0
  return FALSE;
268
269
0
  case PT_BOOL:
270
0
  return FALSE;
271
397
  }
272
273
0
return FALSE;
274
397
}
275
#endif  /* SUPPORT_UNICODE */
276
277
278
279
/*************************************************
280
*        Base opcode of repeated opcodes         *
281
*************************************************/
282
283
/* Returns the base opcode for repeated single character type opcodes. If the
284
opcode is not a repeated character type, it returns with the original value.
285
286
Arguments:  c opcode
287
Returns:    base opcode for the type
288
*/
289
290
static PCRE2_UCHAR
291
get_repeat_base(PCRE2_UCHAR c)
292
50.1k
{
293
50.1k
return (c > OP_TYPEPOSUPTO)? c :
294
50.1k
       (c >= OP_TYPESTAR)?   OP_TYPESTAR :
295
50.1k
       (c >= OP_NOTSTARI)?   OP_NOTSTARI :
296
33.5k
       (c >= OP_NOTSTAR)?    OP_NOTSTAR :
297
32.6k
       (c >= OP_STARI)?      OP_STARI :
298
32.3k
                             OP_STAR;
299
50.1k
}
300
301
302
/*************************************************
303
*        Fill the character property list        *
304
*************************************************/
305
306
/* Checks whether the code points to an opcode that can take part in auto-
307
possessification, and if so, fills a list with its properties.
308
309
Arguments:
310
  code        points to start of expression
311
  utf         TRUE if in UTF mode
312
  ucp         TRUE if in UCP mode
313
  fcc         points to the case-flipping table
314
  list        points to output list
315
              list[0] will be filled with the opcode
316
              list[1] will be non-zero if this opcode
317
                can match an empty character string
318
              list[2..7] depends on the opcode
319
320
Returns:      points to the start of the next opcode if *code is accepted
321
              NULL if *code is not accepted
322
*/
323
324
static PCRE2_SPTR
325
get_chr_property_list(PCRE2_SPTR code, BOOL utf, BOOL ucp, const uint8_t *fcc,
326
  uint32_t *list)
327
52.3k
{
328
52.3k
PCRE2_UCHAR c = *code;
329
52.3k
PCRE2_UCHAR base;
330
52.3k
PCRE2_SPTR end;
331
52.3k
uint32_t chr;
332
333
52.3k
#ifdef SUPPORT_UNICODE
334
52.3k
uint32_t *clist_dest;
335
52.3k
const uint32_t *clist_src;
336
#else
337
(void)utf;    /* Suppress "unused parameter" compiler warnings */
338
(void)ucp;
339
#endif
340
341
52.3k
list[0] = c;
342
52.3k
list[1] = FALSE;
343
52.3k
code++;
344
345
52.3k
if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
346
28.6k
  {
347
28.6k
  base = get_repeat_base(c);
348
28.6k
  c -= (base - OP_STAR);
349
350
28.6k
  if (c == OP_UPTO || c == OP_MINUPTO || c == OP_EXACT || c == OP_POSUPTO)
351
0
    code += IMM2_SIZE;
352
353
28.6k
  list[1] = (c != OP_PLUS && c != OP_MINPLUS && c != OP_EXACT &&
354
20.6k
             c != OP_POSPLUS);
355
356
28.6k
  switch(base)
357
28.6k
    {
358
11.6k
    case OP_STAR:
359
11.6k
    list[0] = OP_CHAR;
360
11.6k
    break;
361
362
6.77k
    case OP_STARI:
363
6.77k
    list[0] = OP_CHARI;
364
6.77k
    break;
365
366
158
    case OP_NOTSTAR:
367
158
    list[0] = OP_NOT;
368
158
    break;
369
370
495
    case OP_NOTSTARI:
371
495
    list[0] = OP_NOTI;
372
495
    break;
373
374
9.60k
    case OP_TYPESTAR:
375
9.60k
    list[0] = *code;
376
9.60k
    code++;
377
9.60k
    break;
378
28.6k
    }
379
28.6k
  c = list[0];
380
28.6k
  }
381
382
52.3k
switch(c)
383
52.3k
  {
384
103
  case OP_NOT_DIGIT:
385
1.02k
  case OP_DIGIT:
386
1.09k
  case OP_NOT_WHITESPACE:
387
1.67k
  case OP_WHITESPACE:
388
2.19k
  case OP_NOT_WORDCHAR:
389
5.45k
  case OP_WORDCHAR:
390
7.56k
  case OP_ANY:
391
7.77k
  case OP_ALLANY:
392
10.7k
  case OP_ANYNL:
393
11.0k
  case OP_NOT_HSPACE:
394
11.0k
  case OP_HSPACE:
395
11.5k
  case OP_NOT_VSPACE:
396
11.8k
  case OP_VSPACE:
397
11.9k
  case OP_EXTUNI:
398
11.9k
  case OP_EODN:
399
11.9k
  case OP_EOD:
400
12.0k
  case OP_DOLL:
401
12.1k
  case OP_DOLLM:
402
12.1k
  return code;
403
404
20.7k
  case OP_CHAR:
405
20.9k
  case OP_NOT:
406
20.9k
  GETCHARINCTEST(chr, code);
407
20.9k
  list[2] = chr;
408
20.9k
  list[3] = NOTACHAR;
409
20.9k
  return code;
410
411
11.2k
  case OP_CHARI:
412
11.7k
  case OP_NOTI:
413
11.7k
  list[0] = (c == OP_CHARI) ? OP_CHAR : OP_NOT;
414
11.7k
  GETCHARINCTEST(chr, code);
415
11.7k
  list[2] = chr;
416
417
11.7k
#ifdef SUPPORT_UNICODE
418
11.7k
  if (chr < 128 || (chr < 256 && !utf && !ucp))
419
11.7k
    list[3] = fcc[chr];
420
69
  else
421
69
    list[3] = UCD_OTHERCASE(chr);
422
#elif defined SUPPORT_WIDE_CHARS
423
  list[3] = (chr < 256) ? fcc[chr] : chr;
424
#else
425
  list[3] = fcc[chr];
426
#endif
427
428
  /* The othercase might be the same value. */
429
430
11.7k
  if (chr == list[3])
431
7.61k
    list[3] = NOTACHAR;
432
4.15k
  else
433
4.15k
    list[4] = NOTACHAR;
434
11.7k
  return code;
435
436
0
#ifdef SUPPORT_UNICODE
437
576
  case OP_PROP:
438
779
  case OP_NOTPROP:
439
779
  if (code[0] != PT_CLIST)
440
659
    {
441
659
    list[2] = code[0];
442
659
    list[3] = code[1];
443
659
    return code + 2;
444
659
    }
445
446
  /* Convert only if we have enough space. */
447
448
120
  clist_src = PRIV(ucd_caseless_sets) + code[1];
449
120
  clist_dest = list + 2;
450
120
  code += 2;
451
452
480
  do {
453
480
     if (clist_dest >= list + 8)
454
0
       {
455
       /* Early return if there is not enough space. This should never
456
       happen, since all clists are shorter than 5 character now. */
457
0
       list[2] = code[0];
458
0
       list[3] = code[1];
459
0
       return code;
460
0
       }
461
480
     *clist_dest++ = *clist_src;
462
480
     }
463
480
  while(*clist_src++ != NOTACHAR);
464
465
  /* All characters are stored. The terminating NOTACHAR is copied from the
466
  clist itself. */
467
468
120
  list[0] = (c == OP_PROP) ? OP_CHAR : OP_NOT;
469
120
  return code;
470
0
#endif
471
472
1.17k
  case OP_NCLASS:
473
5.72k
  case OP_CLASS:
474
5.72k
#ifdef SUPPORT_WIDE_CHARS
475
6.25k
  case OP_XCLASS:
476
6.25k
  if (c == OP_XCLASS)
477
531
    end = code + GET(code, 0) - 1;
478
5.72k
  else
479
5.72k
#endif
480
5.72k
    end = code + 32 / sizeof(PCRE2_UCHAR);
481
482
6.25k
  switch(*end)
483
6.25k
    {
484
2.63k
    case OP_CRSTAR:
485
3.45k
    case OP_CRMINSTAR:
486
4.25k
    case OP_CRQUERY:
487
4.56k
    case OP_CRMINQUERY:
488
4.68k
    case OP_CRPOSSTAR:
489
4.68k
    case OP_CRPOSQUERY:
490
4.68k
    list[1] = TRUE;
491
4.68k
    end++;
492
4.68k
    break;
493
494
919
    case OP_CRPLUS:
495
1.06k
    case OP_CRMINPLUS:
496
1.06k
    case OP_CRPOSPLUS:
497
1.06k
    end++;
498
1.06k
    break;
499
500
0
    case OP_CRRANGE:
501
0
    case OP_CRMINRANGE:
502
0
    case OP_CRPOSRANGE:
503
0
    list[1] = (GET2(end, 1) == 0);
504
0
    end += 1 + 2 * IMM2_SIZE;
505
0
    break;
506
6.25k
    }
507
6.25k
  list[2] = (uint32_t)(end - code);
508
6.25k
  return end;
509
52.3k
  }
510
511
436
return NULL;    /* Opcode not accepted */
512
52.3k
}
513
514
515
516
/*************************************************
517
*    Scan further character sets for match       *
518
*************************************************/
519
520
/* Checks whether the base and the current opcode have a common character, in
521
which case the base cannot be possessified.
522
523
Arguments:
524
  code        points to the byte code
525
  utf         TRUE in UTF mode
526
  ucp         TRUE in UCP mode
527
  cb          compile data block
528
  base_list   the data list of the base opcode
529
  base_end    the end of the base opcode
530
  rec_limit   points to recursion depth counter
531
532
Returns:      TRUE if the auto-possessification is possible
533
*/
534
535
static BOOL
536
compare_opcodes(PCRE2_SPTR code, BOOL utf, BOOL ucp, const compile_block *cb,
537
  const uint32_t *base_list, PCRE2_SPTR base_end, int *rec_limit)
538
23.7k
{
539
23.7k
PCRE2_UCHAR c;
540
23.7k
uint32_t list[8];
541
23.7k
const uint32_t *chr_ptr;
542
23.7k
const uint32_t *ochr_ptr;
543
23.7k
const uint32_t *list_ptr;
544
23.7k
PCRE2_SPTR next_code;
545
23.7k
#ifdef SUPPORT_WIDE_CHARS
546
23.7k
PCRE2_SPTR xclass_flags;
547
23.7k
#endif
548
23.7k
const uint8_t *class_bitset;
549
23.7k
const uint8_t *set1, *set2, *set_end;
550
23.7k
uint32_t chr;
551
23.7k
BOOL accepted, invert_bits;
552
23.7k
BOOL entered_a_group = FALSE;
553
554
23.7k
if (--(*rec_limit) <= 0) return FALSE;  /* Recursion has gone too deep */
555
556
/* Note: the base_list[1] contains whether the current opcode has a greedy
557
(represented by a non-zero value) quantifier. This is a different from
558
other character type lists, which store here that the character iterator
559
matches to an empty string (also represented by a non-zero value). */
560
561
23.7k
for(;;)
562
29.3k
  {
563
29.3k
  PCRE2_SPTR bracode;
564
565
  /* All operations move the code pointer forward.
566
  Therefore infinite recursions are not possible. */
567
568
29.3k
  c = *code;
569
570
  /* Skip over callouts */
571
572
29.3k
  if (c == OP_CALLOUT)
573
0
    {
574
0
    code += PRIV(OP_lengths)[c];
575
0
    continue;
576
0
    }
577
578
29.3k
  if (c == OP_CALLOUT_STR)
579
0
    {
580
0
    code += GET(code, 1 + 2*LINK_SIZE);
581
0
    continue;
582
0
    }
583
584
  /* At the end of a branch, skip to the end of the group. */
585
586
29.3k
  if (c == OP_ALT)
587
359
    {
588
6.38k
    do code += GET(code, 1); while (*code == OP_ALT);
589
359
    c = *code;
590
359
    }
591
592
  /* Inspect the next opcode. */
593
594
29.3k
  switch(c)
595
29.3k
    {
596
    /* We can always possessify a greedy iterator at the end of the pattern,
597
    which is reached after skipping over the final OP_KET. A non-greedy
598
    iterator must never be possessified. */
599
600
341
    case OP_END:
601
341
    return base_list[1] != 0;
602
603
    /* When an iterator is at the end of certain kinds of group we can inspect
604
    what follows the group by skipping over the closing ket. Note that this
605
    does not apply to OP_KETRMAX or OP_KETRMIN because what follows any given
606
    iteration is variable (could be another iteration or could be the next
607
    item). As these two opcodes are not listed in the next switch, they will
608
    end up as the next code to inspect, and return FALSE by virtue of being
609
    unsupported. */
610
611
410
    case OP_KET:
612
410
    case OP_KETRPOS:
613
    /* The non-greedy case cannot be converted to a possessive form. */
614
615
410
    if (base_list[1] == 0) return FALSE;
616
617
    /* If the bracket is capturing it might be referenced by an OP_RECURSE
618
    so its last iterator can never be possessified if the pattern contains
619
    recursions. (This could be improved by keeping a list of group numbers that
620
    are called by recursion.) */
621
622
401
    bracode = code - GET(code, 1);
623
401
    switch(*bracode)
624
401
      {
625
37
      case OP_CBRA:
626
37
      case OP_SCBRA:
627
37
      case OP_CBRAPOS:
628
37
      case OP_SCBRAPOS:
629
37
      if (cb->had_recurse) return FALSE;
630
37
      break;
631
632
      /* A script run might have to backtrack if the iterated item can match
633
      characters from more than one script. So give up unless repeating an
634
      explicit character. */
635
636
37
      case OP_SCRIPT_RUN:
637
0
      if (base_list[0] != OP_CHAR && base_list[0] != OP_CHARI)
638
0
        return FALSE;
639
0
      break;
640
641
      /* Atomic sub-patterns and assertions can always auto-possessify their
642
      last iterator except for variable length lookbehinds. However, if the
643
      group was entered as a result of checking a previous iterator, this is
644
      not possible. */
645
646
3
      case OP_ASSERT:
647
4
      case OP_ASSERT_NOT:
648
5
      case OP_ONCE:
649
5
      return !entered_a_group;
650
651
0
      case OP_ASSERTBACK:
652
0
      case OP_ASSERTBACK_NOT:
653
0
      return (bracode[1+LINK_SIZE] == OP_VREVERSE)? FALSE : !entered_a_group;
654
655
      /* Non-atomic assertions - don't possessify last iterator. This needs
656
      more thought. */
657
658
2
      case OP_ASSERT_NA:
659
2
      case OP_ASSERTBACK_NA:
660
2
      return FALSE;
661
401
      }
662
663
    /* Skip over the bracket and inspect what comes next. */
664
665
394
    code += PRIV(OP_lengths)[c];
666
394
    continue;
667
668
    /* Handle cases where the next item is a group. */
669
670
2
    case OP_ONCE:
671
2
    case OP_BRA:
672
12
    case OP_CBRA:
673
12
    next_code = code + GET(code, 1);
674
12
    code += PRIV(OP_lengths)[c];
675
676
    /* Check each branch. We have to recurse a level for all but the last
677
    branch. */
678
679
23
    while (*next_code == OP_ALT)
680
12
      {
681
12
      if (!compare_opcodes(code, utf, ucp, cb, base_list, base_end, rec_limit))
682
1
        return FALSE;
683
11
      code = next_code + 1 + LINK_SIZE;
684
11
      next_code += GET(next_code, 1);
685
11
      }
686
687
11
    entered_a_group = TRUE;
688
11
    continue;
689
690
0
    case OP_BRAZERO:
691
0
    case OP_BRAMINZERO:
692
693
0
    next_code = code + 1;
694
0
    if (*next_code != OP_BRA && *next_code != OP_CBRA &&
695
0
        *next_code != OP_ONCE) return FALSE;
696
697
0
    do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
698
699
    /* The bracket content will be checked by the OP_BRA/OP_CBRA case above. */
700
701
0
    next_code += 1 + LINK_SIZE;
702
0
    if (!compare_opcodes(next_code, utf, ucp, cb, base_list, base_end,
703
0
         rec_limit))
704
0
      return FALSE;
705
706
0
    code += PRIV(OP_lengths)[c];
707
0
    continue;
708
709
    /* The next opcode does not need special handling; fall through and use it
710
    to see if the base can be possessified. */
711
712
28.5k
    default:
713
28.5k
    break;
714
29.3k
    }
715
716
  /* We now have the next appropriate opcode to compare with the base. Check
717
  for a supported opcode, and load its properties. */
718
719
28.5k
  code = get_chr_property_list(code, utf, ucp, cb->fcc, list);
720
28.5k
  if (code == NULL) return FALSE;    /* Unsupported */
721
722
  /* If either opcode is a small character list, set pointers for comparing
723
  characters from that list with another list, or with a property. */
724
725
28.1k
  if (base_list[0] == OP_CHAR)
726
16.7k
    {
727
16.7k
    chr_ptr = base_list + 2;
728
16.7k
    list_ptr = list;
729
16.7k
    }
730
11.4k
  else if (list[0] == OP_CHAR)
731
7.36k
    {
732
7.36k
    chr_ptr = list + 2;
733
7.36k
    list_ptr = base_list;
734
7.36k
    }
735
736
  /* Character bitsets can also be compared to certain opcodes. */
737
738
4.07k
  else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
739
3.14k
#if PCRE2_CODE_UNIT_WIDTH == 8
740
      /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
741
3.14k
      || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
742
4.07k
#endif
743
4.07k
      )
744
1.25k
    {
745
1.25k
#if PCRE2_CODE_UNIT_WIDTH == 8
746
1.25k
    if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
747
#else
748
    if (base_list[0] == OP_CLASS)
749
#endif
750
748
      {
751
748
      set1 = (uint8_t *)(base_end - base_list[2]);
752
748
      list_ptr = list;
753
748
      }
754
503
    else
755
503
      {
756
503
      set1 = (uint8_t *)(code - list[2]);
757
503
      list_ptr = base_list;
758
503
      }
759
760
1.25k
    invert_bits = FALSE;
761
1.25k
    switch(list_ptr[0])
762
1.25k
      {
763
193
      case OP_CLASS:
764
254
      case OP_NCLASS:
765
254
      set2 = (uint8_t *)
766
254
        ((list_ptr == list ? code : base_end) - list_ptr[2]);
767
254
      break;
768
769
0
#ifdef SUPPORT_WIDE_CHARS
770
103
      case OP_XCLASS:
771
103
      xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
772
103
      if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
773
3
      if ((*xclass_flags & XCL_MAP) == 0)
774
0
        {
775
        /* No bits are set for characters < 256. */
776
0
        if (list[1] == 0) return (*xclass_flags & XCL_NOT) == 0;
777
        /* Might be an empty repeat. */
778
0
        continue;
779
0
        }
780
3
      set2 = (uint8_t *)(xclass_flags + 1);
781
3
      break;
782
0
#endif
783
784
3
      case OP_NOT_DIGIT:
785
3
      invert_bits = TRUE;
786
      /* Fall through */
787
341
      case OP_DIGIT:
788
341
      set2 = (uint8_t *)(cb->cbits + cbit_digit);
789
341
      break;
790
791
0
      case OP_NOT_WHITESPACE:
792
0
      invert_bits = TRUE;
793
      /* Fall through */
794
49
      case OP_WHITESPACE:
795
49
      set2 = (uint8_t *)(cb->cbits + cbit_space);
796
49
      break;
797
798
90
      case OP_NOT_WORDCHAR:
799
90
      invert_bits = TRUE;
800
      /* Fall through */
801
237
      case OP_WORDCHAR:
802
237
      set2 = (uint8_t *)(cb->cbits + cbit_word);
803
237
      break;
804
805
267
      default:
806
267
      return FALSE;
807
1.25k
      }
808
809
    /* Because the bit sets are unaligned bytes, we need to perform byte
810
    comparison here. */
811
812
884
    set_end = set1 + 32;
813
884
    if (invert_bits)
814
93
      {
815
93
      do
816
111
        {
817
111
        if ((*set1++ & ~(*set2++)) != 0) return FALSE;
818
111
        }
819
93
      while (set1 < set_end);
820
93
      }
821
791
    else
822
791
      {
823
791
      do
824
12.0k
        {
825
12.0k
        if ((*set1++ & *set2++) != 0) return FALSE;
826
12.0k
        }
827
11.5k
      while (set1 < set_end);
828
791
      }
829
830
228
    if (list[1] == 0) return TRUE;
831
    /* Might be an empty repeat. */
832
143
    continue;
833
228
    }
834
835
  /* Some property combinations also acceptable. Unicode property opcodes are
836
  processed specially; the rest can be handled with a lookup table. */
837
838
2.82k
  else
839
2.82k
    {
840
2.82k
    uint32_t leftop, rightop;
841
842
2.82k
    leftop = base_list[0];
843
2.82k
    rightop = list[0];
844
845
2.82k
#ifdef SUPPORT_UNICODE
846
2.82k
    accepted = FALSE; /* Always set in non-unicode case. */
847
2.82k
    if (leftop == OP_PROP || leftop == OP_NOTPROP)
848
91
      {
849
91
      if (rightop == OP_EOD)
850
0
        accepted = TRUE;
851
91
      else if (rightop == OP_PROP || rightop == OP_NOTPROP)
852
21
        {
853
21
        int n;
854
21
        const uint8_t *p;
855
21
        BOOL same = leftop == rightop;
856
21
        BOOL lisprop = leftop == OP_PROP;
857
21
        BOOL risprop = rightop == OP_PROP;
858
21
        BOOL bothprop = lisprop && risprop;
859
860
        /* There's a table that specifies how each combination is to be
861
        processed:
862
          0   Always return FALSE (never auto-possessify)
863
          1   Character groups are distinct (possessify if both are OP_PROP)
864
          2   Check character categories in the same group (general or particular)
865
          3   Return TRUE if the two opcodes are not the same
866
          ... see comments below
867
        */
868
869
21
        n = propposstab[base_list[2]][list[2]];
870
21
        switch(n)
871
21
          {
872
0
          case 0: break;
873
1
          case 1: accepted = bothprop; break;
874
2
          case 2: accepted = (base_list[3] == list[3]) != same; break;
875
10
          case 3: accepted = !same; break;
876
877
0
          case 4:  /* Left general category, right particular category */
878
0
          accepted = risprop && catposstab[base_list[3]][list[3]] == same;
879
0
          break;
880
881
0
          case 5:  /* Right general category, left particular category */
882
0
          accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
883
0
          break;
884
885
          /* This code is logically tricky. Think hard before fiddling with it.
886
          The posspropstab table has four entries per row. Each row relates to
887
          one of PCRE's special properties such as ALNUM or SPACE or WORD.
888
          Only WORD actually needs all four entries, but using repeats for the
889
          others means they can all use the same code below.
890
891
          The first two entries in each row are Unicode general categories, and
892
          apply always, because all the characters they include are part of the
893
          PCRE character set. The third and fourth entries are a general and a
894
          particular category, respectively, that include one or more relevant
895
          characters. One or the other is used, depending on whether the check
896
          is for a general or a particular category. However, in both cases the
897
          category contains more characters than the specials that are defined
898
          for the property being tested against. Therefore, it cannot be used
899
          in a NOTPROP case.
900
901
          Example: the row for WORD contains ucp_L, ucp_N, ucp_P, ucp_Po.
902
          Underscore is covered by ucp_P or ucp_Po. */
903
904
0
          case 6:  /* Left alphanum vs right general category */
905
0
          case 7:  /* Left space vs right general category */
906
0
          case 8:  /* Left word vs right general category */
907
0
          p = posspropstab[n-6];
908
0
          accepted = risprop && lisprop ==
909
0
            (list[3] != p[0] &&
910
0
             list[3] != p[1] &&
911
0
            (list[3] != p[2] || !lisprop));
912
0
          break;
913
914
0
          case 9:   /* Right alphanum vs left general category */
915
0
          case 10:  /* Right space vs left general category */
916
0
          case 11:  /* Right word vs left general category */
917
0
          p = posspropstab[n-9];
918
0
          accepted = lisprop && risprop ==
919
0
            (base_list[3] != p[0] &&
920
0
             base_list[3] != p[1] &&
921
0
            (base_list[3] != p[2] || !risprop));
922
0
          break;
923
924
0
          case 12:  /* Left alphanum vs right particular category */
925
7
          case 13:  /* Left space vs right particular category */
926
8
          case 14:  /* Left word vs right particular category */
927
8
          p = posspropstab[n-12];
928
8
          accepted = risprop && lisprop ==
929
6
            (catposstab[p[0]][list[3]] &&
930
6
             catposstab[p[1]][list[3]] &&
931
6
            (list[3] != p[3] || !lisprop));
932
8
          break;
933
934
0
          case 15:  /* Right alphanum vs left particular category */
935
0
          case 16:  /* Right space vs left particular category */
936
0
          case 17:  /* Right word vs left particular category */
937
0
          p = posspropstab[n-15];
938
0
          accepted = lisprop && risprop ==
939
0
            (catposstab[p[0]][base_list[3]] &&
940
0
             catposstab[p[1]][base_list[3]] &&
941
0
            (base_list[3] != p[3] || !risprop));
942
0
          break;
943
21
          }
944
21
        }
945
91
      }
946
947
2.73k
    else
948
2.73k
#endif  /* SUPPORT_UNICODE */
949
950
2.73k
    accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
951
2.60k
           rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
952
2.59k
           autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
953
954
2.82k
    if (!accepted) return FALSE;
955
956
986
    if (list[1] == 0) return TRUE;
957
    /* Might be an empty repeat. */
958
568
    continue;
959
986
    }
960
961
  /* Control reaches here only if one of the items is a small character list.
962
  All characters are checked against the other side. */
963
964
24.0k
  do
965
26.0k
    {
966
26.0k
    chr = *chr_ptr;
967
968
26.0k
    switch(list_ptr[0])
969
26.0k
      {
970
12.6k
      case OP_CHAR:
971
12.6k
      ochr_ptr = list_ptr + 2;
972
12.6k
      do
973
14.6k
        {
974
14.6k
        if (chr == *ochr_ptr) return FALSE;
975
14.2k
        ochr_ptr++;
976
14.2k
        }
977
14.2k
      while(*ochr_ptr != NOTACHAR);
978
12.2k
      break;
979
980
12.2k
      case OP_NOT:
981
503
      ochr_ptr = list_ptr + 2;
982
503
      do
983
670
        {
984
670
        if (chr == *ochr_ptr)
985
98
          break;
986
572
        ochr_ptr++;
987
572
        }
988
572
      while(*ochr_ptr != NOTACHAR);
989
503
      if (*ochr_ptr == NOTACHAR) return FALSE;   /* Not found */
990
98
      break;
991
992
      /* Note that OP_DIGIT etc. are generated only when PCRE2_UCP is *not*
993
      set. When it is set, \d etc. are converted into OP_(NOT_)PROP codes. */
994
995
713
      case OP_DIGIT:
996
713
      if (chr < 256 && (cb->ctypes[chr] & ctype_digit) != 0) return FALSE;
997
702
      break;
998
999
702
      case OP_NOT_DIGIT:
1000
76
      if (chr > 255 || (cb->ctypes[chr] & ctype_digit) == 0) return FALSE;
1001
1
      break;
1002
1003
293
      case OP_WHITESPACE:
1004
293
      if (chr < 256 && (cb->ctypes[chr] & ctype_space) != 0) return FALSE;
1005
288
      break;
1006
1007
288
      case OP_NOT_WHITESPACE:
1008
24
      if (chr > 255 || (cb->ctypes[chr] & ctype_space) == 0) return FALSE;
1009
1
      break;
1010
1011
1.61k
      case OP_WORDCHAR:
1012
1.61k
      if (chr < 255 && (cb->ctypes[chr] & ctype_word) != 0) return FALSE;
1013
1.34k
      break;
1014
1015
1.34k
      case OP_NOT_WORDCHAR:
1016
591
      if (chr > 255 || (cb->ctypes[chr] & ctype_word) == 0) return FALSE;
1017
440
      break;
1018
1019
440
      case OP_HSPACE:
1020
44
      switch(chr)
1021
44
        {
1022
0
        HSPACE_CASES: return FALSE;
1023
44
        default: break;
1024
44
        }
1025
44
      break;
1026
1027
251
      case OP_NOT_HSPACE:
1028
251
      switch(chr)
1029
251
        {
1030
4
        HSPACE_CASES: break;
1031
247
        default: return FALSE;
1032
251
        }
1033
4
      break;
1034
1035
2.21k
      case OP_ANYNL:
1036
2.53k
      case OP_VSPACE:
1037
2.53k
      switch(chr)
1038
2.53k
        {
1039
150
        VSPACE_CASES: return FALSE;
1040
2.38k
        default: break;
1041
2.53k
        }
1042
2.38k
      break;
1043
1044
2.38k
      case OP_NOT_VSPACE:
1045
349
      switch(chr)
1046
349
        {
1047
4
        VSPACE_CASES: break;
1048
345
        default: return FALSE;
1049
349
        }
1050
4
      break;
1051
1052
67
      case OP_DOLL:
1053
68
      case OP_EODN:
1054
68
      switch (chr)
1055
68
        {
1056
1
        case CHAR_CR:
1057
1
        case CHAR_LF:
1058
3
        case CHAR_VT:
1059
3
        case CHAR_FF:
1060
3
        case CHAR_NEL:
1061
3
#ifndef EBCDIC
1062
3
        case 0x2028:
1063
3
        case 0x2029:
1064
3
#endif  /* Not EBCDIC */
1065
3
        return FALSE;
1066
68
        }
1067
65
      break;
1068
1069
65
      case OP_EOD:    /* Can always possessify before \z */
1070
2
      break;
1071
1072
0
#ifdef SUPPORT_UNICODE
1073
338
      case OP_PROP:
1074
397
      case OP_NOTPROP:
1075
397
      if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
1076
397
            list_ptr[0] == OP_NOTPROP))
1077
123
        return FALSE;
1078
274
      break;
1079
274
#endif
1080
1081
858
      case OP_NCLASS:
1082
858
      if (chr > 255) return FALSE;
1083
      /* Fall through */
1084
1085
4.73k
      case OP_CLASS:
1086
4.73k
      if (chr > 255) break;
1087
4.70k
      class_bitset = (uint8_t *)
1088
4.70k
        ((list_ptr == list ? code : base_end) - list_ptr[2]);
1089
4.70k
      if ((class_bitset[chr >> 3] & (1u << (chr & 7))) != 0) return FALSE;
1090
2.59k
      break;
1091
1092
2.59k
#ifdef SUPPORT_WIDE_CHARS
1093
2.59k
      case OP_XCLASS:
1094
369
      if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
1095
369
          list_ptr[2] + LINK_SIZE, utf)) return FALSE;
1096
108
      break;
1097
108
#endif
1098
1099
835
      default:
1100
835
      return FALSE;
1101
26.0k
      }
1102
1103
20.6k
    chr_ptr++;
1104
20.6k
    }
1105
24.0k
  while(*chr_ptr != NOTACHAR);
1106
1107
  /* At least one character must be matched from this opcode. */
1108
1109
18.7k
  if (list[1] == 0) return TRUE;
1110
18.7k
  }
1111
1112
/* Control never reaches here. There used to be a fail-save return FALSE; here,
1113
but some compilers complain about an unreachable statement. */
1114
23.7k
}
1115
1116
1117
1118
/*************************************************
1119
*    Scan compiled regex for auto-possession     *
1120
*************************************************/
1121
1122
/* Replaces single character iterations with their possessive alternatives
1123
if appropriate. This function modifies the compiled opcode! Hitting a
1124
non-existent opcode may indicate a bug in PCRE2, but it can also be caused if a
1125
bad UTF string was compiled with PCRE2_NO_UTF_CHECK. The rec_limit catches
1126
overly complicated or large patterns. In these cases, the check just stops,
1127
leaving the remainder of the pattern unpossessified.
1128
1129
Arguments:
1130
  code        points to start of the byte code
1131
  cb          compile data block
1132
1133
Returns:      0 for success
1134
              -1 if a non-existant opcode is encountered
1135
*/
1136
1137
int
1138
PRIV(auto_possessify)(PCRE2_UCHAR *code, const compile_block *cb)
1139
1.10k
{
1140
1.10k
PCRE2_UCHAR c;
1141
1.10k
PCRE2_SPTR end;
1142
1.10k
PCRE2_UCHAR *repeat_opcode;
1143
1.10k
uint32_t list[8];
1144
1.10k
int rec_limit = 1000;  /* Was 10,000 but clang+ASAN uses a lot of stack. */
1145
1.10k
BOOL utf = (cb->external_options & PCRE2_UTF) != 0;
1146
1.10k
BOOL ucp = (cb->external_options & PCRE2_UCP) != 0;
1147
1148
1.10k
for (;;)
1149
261k
  {
1150
261k
  c = *code;
1151
1152
261k
  if (c >= OP_TABLE_LENGTH) return -1;   /* Something gone wrong */
1153
1154
261k
  if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
1155
21.4k
    {
1156
21.4k
    c -= get_repeat_base(c) - OP_STAR;
1157
21.4k
    end = (c <= OP_MINUPTO) ?
1158
21.4k
      get_chr_property_list(code, utf, ucp, cb->fcc, list) : NULL;
1159
21.4k
    list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
1160
1161
21.4k
    if (end != NULL && compare_opcodes(end, utf, ucp, cb, list, end,
1162
20.7k
        &rec_limit))
1163
14.0k
      {
1164
14.0k
      switch(c)
1165
14.0k
        {
1166
1.12k
        case OP_STAR:
1167
1.12k
        *code += OP_POSSTAR - OP_STAR;
1168
1.12k
        break;
1169
1170
614
        case OP_MINSTAR:
1171
614
        *code += OP_POSSTAR - OP_MINSTAR;
1172
614
        break;
1173
1174
3.11k
        case OP_PLUS:
1175
3.11k
        *code += OP_POSPLUS - OP_PLUS;
1176
3.11k
        break;
1177
1178
1.08k
        case OP_MINPLUS:
1179
1.08k
        *code += OP_POSPLUS - OP_MINPLUS;
1180
1.08k
        break;
1181
1182
7.55k
        case OP_QUERY:
1183
7.55k
        *code += OP_POSQUERY - OP_QUERY;
1184
7.55k
        break;
1185
1186
588
        case OP_MINQUERY:
1187
588
        *code += OP_POSQUERY - OP_MINQUERY;
1188
588
        break;
1189
1190
0
        case OP_UPTO:
1191
0
        *code += OP_POSUPTO - OP_UPTO;
1192
0
        break;
1193
1194
0
        case OP_MINUPTO:
1195
0
        *code += OP_POSUPTO - OP_MINUPTO;
1196
0
        break;
1197
14.0k
        }
1198
14.0k
      }
1199
21.4k
    c = *code;
1200
21.4k
    }
1201
240k
  else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
1202
4.96k
    {
1203
4.96k
#ifdef SUPPORT_WIDE_CHARS
1204
4.96k
    if (c == OP_XCLASS)
1205
762
      repeat_opcode = code + GET(code, 1);
1206
4.19k
    else
1207
4.19k
#endif
1208
4.19k
      repeat_opcode = code + 1 + (32 / sizeof(PCRE2_UCHAR));
1209
1210
4.96k
    c = *repeat_opcode;
1211
4.96k
    if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
1212
3.03k
      {
1213
      /* The return from get_chr_property_list() will never be NULL when
1214
      *code (aka c) is one of the three class opcodes. However, gcc with
1215
      -fanalyzer notes that a NULL return is possible, and grumbles. Hence we
1216
      put in a check. */
1217
1218
3.03k
      end = get_chr_property_list(code, utf, ucp, cb->fcc, list);
1219
3.03k
      list[1] = (c & 1) == 0;
1220
1221
3.03k
      if (end != NULL &&
1222
3.03k
          compare_opcodes(end, utf, ucp, cb, list, end, &rec_limit))
1223
1.04k
        {
1224
1.04k
        switch (c)
1225
1.04k
          {
1226
415
          case OP_CRSTAR:
1227
454
          case OP_CRMINSTAR:
1228
454
          *repeat_opcode = OP_CRPOSSTAR;
1229
454
          break;
1230
1231
336
          case OP_CRPLUS:
1232
347
          case OP_CRMINPLUS:
1233
347
          *repeat_opcode = OP_CRPOSPLUS;
1234
347
          break;
1235
1236
217
          case OP_CRQUERY:
1237
239
          case OP_CRMINQUERY:
1238
239
          *repeat_opcode = OP_CRPOSQUERY;
1239
239
          break;
1240
1241
0
          case OP_CRRANGE:
1242
0
          case OP_CRMINRANGE:
1243
0
          *repeat_opcode = OP_CRPOSRANGE;
1244
0
          break;
1245
1.04k
          }
1246
1.04k
        }
1247
3.03k
      }
1248
4.96k
    c = *code;
1249
4.96k
    }
1250
1251
261k
  switch(c)
1252
261k
    {
1253
1.10k
    case OP_END:
1254
1.10k
    return 0;
1255
1256
524
    case OP_TYPESTAR:
1257
531
    case OP_TYPEMINSTAR:
1258
1.20k
    case OP_TYPEPLUS:
1259
1.55k
    case OP_TYPEMINPLUS:
1260
3.03k
    case OP_TYPEQUERY:
1261
3.20k
    case OP_TYPEMINQUERY:
1262
3.22k
    case OP_TYPEPOSSTAR:
1263
4.09k
    case OP_TYPEPOSPLUS:
1264
7.01k
    case OP_TYPEPOSQUERY:
1265
7.01k
    if (code[1] == OP_PROP || code[1] == OP_NOTPROP) code += 2;
1266
7.01k
    break;
1267
1268
0
    case OP_TYPEUPTO:
1269
0
    case OP_TYPEMINUPTO:
1270
0
    case OP_TYPEEXACT:
1271
0
    case OP_TYPEPOSUPTO:
1272
0
    if (code[1 + IMM2_SIZE] == OP_PROP || code[1 + IMM2_SIZE] == OP_NOTPROP)
1273
0
      code += 2;
1274
0
    break;
1275
1276
0
    case OP_CALLOUT_STR:
1277
0
    code += GET(code, 1 + 2*LINK_SIZE);
1278
0
    break;
1279
1280
0
#ifdef SUPPORT_WIDE_CHARS
1281
762
    case OP_XCLASS:
1282
762
    code += GET(code, 1);
1283
762
    break;
1284
0
#endif
1285
1286
0
    case OP_MARK:
1287
0
    case OP_COMMIT_ARG:
1288
0
    case OP_PRUNE_ARG:
1289
0
    case OP_SKIP_ARG:
1290
0
    case OP_THEN_ARG:
1291
0
    code += code[1];
1292
0
    break;
1293
261k
    }
1294
1295
  /* Add in the fixed length from the table */
1296
1297
260k
  code += PRIV(OP_lengths)[c];
1298
1299
  /* In UTF-8 and UTF-16 modes, opcodes that are followed by a character may be
1300
  followed by a multi-byte character. The length in the table is a minimum, so
1301
  we have to arrange to skip the extra code units. */
1302
1303
260k
#ifdef MAYBE_UTF_MULTI
1304
260k
  if (utf) switch(c)
1305
30.5k
    {
1306
5.66k
    case OP_CHAR:
1307
20.1k
    case OP_CHARI:
1308
20.1k
    case OP_NOT:
1309
20.1k
    case OP_NOTI:
1310
20.1k
    case OP_STAR:
1311
20.1k
    case OP_MINSTAR:
1312
20.2k
    case OP_PLUS:
1313
20.2k
    case OP_MINPLUS:
1314
20.2k
    case OP_QUERY:
1315
20.2k
    case OP_MINQUERY:
1316
20.2k
    case OP_UPTO:
1317
20.2k
    case OP_MINUPTO:
1318
20.2k
    case OP_EXACT:
1319
20.2k
    case OP_POSSTAR:
1320
20.4k
    case OP_POSPLUS:
1321
20.5k
    case OP_POSQUERY:
1322
20.5k
    case OP_POSUPTO:
1323
20.5k
    case OP_STARI:
1324
20.5k
    case OP_MINSTARI:
1325
20.5k
    case OP_PLUSI:
1326
20.5k
    case OP_MINPLUSI:
1327
20.6k
    case OP_QUERYI:
1328
20.6k
    case OP_MINQUERYI:
1329
20.6k
    case OP_UPTOI:
1330
20.6k
    case OP_MINUPTOI:
1331
20.6k
    case OP_EXACTI:
1332
20.7k
    case OP_POSSTARI:
1333
21.2k
    case OP_POSPLUSI:
1334
21.6k
    case OP_POSQUERYI:
1335
21.6k
    case OP_POSUPTOI:
1336
21.6k
    case OP_NOTSTAR:
1337
21.6k
    case OP_NOTMINSTAR:
1338
21.6k
    case OP_NOTPLUS:
1339
21.6k
    case OP_NOTMINPLUS:
1340
21.6k
    case OP_NOTQUERY:
1341
21.6k
    case OP_NOTMINQUERY:
1342
21.6k
    case OP_NOTUPTO:
1343
21.6k
    case OP_NOTMINUPTO:
1344
21.6k
    case OP_NOTEXACT:
1345
21.6k
    case OP_NOTPOSSTAR:
1346
21.6k
    case OP_NOTPOSPLUS:
1347
21.6k
    case OP_NOTPOSQUERY:
1348
21.6k
    case OP_NOTPOSUPTO:
1349
21.6k
    case OP_NOTSTARI:
1350
21.6k
    case OP_NOTMINSTARI:
1351
21.7k
    case OP_NOTPLUSI:
1352
21.7k
    case OP_NOTMINPLUSI:
1353
21.7k
    case OP_NOTQUERYI:
1354
21.7k
    case OP_NOTMINQUERYI:
1355
21.7k
    case OP_NOTUPTOI:
1356
21.7k
    case OP_NOTMINUPTOI:
1357
21.7k
    case OP_NOTEXACTI:
1358
21.7k
    case OP_NOTPOSSTARI:
1359
21.7k
    case OP_NOTPOSPLUSI:
1360
21.7k
    case OP_NOTPOSQUERYI:
1361
21.7k
    case OP_NOTPOSUPTOI:
1362
21.7k
    if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
1363
21.7k
    break;
1364
30.5k
    }
1365
#else
1366
  (void)(utf);  /* Keep compiler happy by referencing function argument */
1367
#endif  /* SUPPORT_WIDE_CHARS */
1368
260k
  }
1369
1.10k
}
1370
1371
/* End of pcre2_auto_possess.c */