/[pcre]/code/trunk/pcre_compile.c
ViewVC logotype

Diff of /code/trunk/pcre_compile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1376 by ph10, Sat Oct 12 18:02:11 2013 UTC revision 1538 by ph10, Sun Mar 29 11:22:24 2015 UTC
# Line 6  Line 6 
6  and semantics are as close as possible to those of the Perl 5 language.  and semantics are as close as possible to those of the Perl 5 language.
7    
8                         Written by Philip Hazel                         Written by Philip Hazel
9             Copyright (c) 1997-2013 University of Cambridge             Copyright (c) 1997-2014 University of Cambridge
10    
11  -----------------------------------------------------------------------------  -----------------------------------------------------------------------------
12  Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
# Line 47  supporting internal functions that are n Line 47  supporting internal functions that are n
47  #endif  #endif
48    
49  #define NLBLOCK cd             /* Block containing newline information */  #define NLBLOCK cd             /* Block containing newline information */
50  #define PSSTART start_pattern  /* Field containing processed string start */  #define PSSTART start_pattern  /* Field containing pattern start */
51  #define PSEND   end_pattern    /* Field containing processed string end */  #define PSEND   end_pattern    /* Field containing pattern end */
52    
53  #include "pcre_internal.h"  #include "pcre_internal.h"
54    
# Line 260  static const verbitem verbs[] = { Line 260  static const verbitem verbs[] = {
260  static const int verbcount = sizeof(verbs)/sizeof(verbitem);  static const int verbcount = sizeof(verbs)/sizeof(verbitem);
261    
262    
263    /* Substitutes for [[:<:]] and [[:>:]], which mean start and end of word in
264    another regex library. */
265    
266    static const pcre_uchar sub_start_of_word[] = {
267      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
268      CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w, CHAR_RIGHT_PARENTHESIS, '\0' };
269    
270    static const pcre_uchar sub_end_of_word[] = {
271      CHAR_BACKSLASH, CHAR_b, CHAR_LEFT_PARENTHESIS, CHAR_QUESTION_MARK,
272      CHAR_LESS_THAN_SIGN, CHAR_EQUALS_SIGN, CHAR_BACKSLASH, CHAR_w,
273      CHAR_RIGHT_PARENTHESIS, '\0' };
274    
275    
276  /* Tables of names of POSIX character classes and their lengths. The names are  /* Tables of names of POSIX character classes and their lengths. The names are
277  now all in a single string, to reduce the number of relocations when a shared  now all in a single string, to reduce the number of relocations when a shared
278  library is dynamically loaded. The list of lengths is terminated by a zero  library is dynamically loaded. The list of lengths is terminated by a zero
279  length entry. The first three must be alpha, lower, upper, as this is assumed  length entry. The first three must be alpha, lower, upper, as this is assumed
280  for handling case independence. */  for handling case independence. The indices for graph, print, and punct are
281    needed, so identify them. */
282    
283  static const char posix_names[] =  static const char posix_names[] =
284    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0    STRING_alpha0 STRING_lower0 STRING_upper0 STRING_alnum0
# Line 275  static const char posix_names[] = Line 289  static const char posix_names[] =
289  static const pcre_uint8 posix_name_lengths[] = {  static const pcre_uint8 posix_name_lengths[] = {
290    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 6, 0 };
291    
292    #define PC_GRAPH  8
293    #define PC_PRINT  9
294    #define PC_PUNCT 10
295    
296    
297  /* Table of class bit maps for each POSIX class. Each class is formed from a  /* Table of class bit maps for each POSIX class. Each class is formed from a
298  base map, with an optional addition or removal of another map. Then, for some  base map, with an optional addition or removal of another map. Then, for some
299  classes, there is some additional tweaking: for [:blank:] the vertical space  classes, there is some additional tweaking: for [:blank:] the vertical space
# Line 302  static const int posix_class_maps[] = { Line 321  static const int posix_class_maps[] = {
321    cbit_xdigit,-1,          0              /* xdigit */    cbit_xdigit,-1,          0              /* xdigit */
322  };  };
323    
324  /* Table of substitutes for \d etc when PCRE_UCP is set. The POSIX class  /* Table of substitutes for \d etc when PCRE_UCP is set. They are replaced by
325  substitutes must be in the order of the names, defined above, and there are  Unicode property escapes. */
 both positive and negative cases. NULL means no substitute. */  
326    
327  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
328  static const pcre_uchar string_PNd[]  = {  static const pcre_uchar string_PNd[]  = {
# Line 329  static const pcre_uchar string_pXwd[] = Line 347  static const pcre_uchar string_pXwd[] =
347  static const pcre_uchar *substitutes[] = {  static const pcre_uchar *substitutes[] = {
348    string_PNd,           /* \D */    string_PNd,           /* \D */
349    string_pNd,           /* \d */    string_pNd,           /* \d */
350    string_PXsp,          /* \S */       /* NOTE: Xsp is Perl space */    string_PXsp,          /* \S */   /* Xsp is Perl space, but from 8.34, Perl */
351    string_pXsp,          /* \s */    string_pXsp,          /* \s */   /* space and POSIX space are the same. */
352    string_PXwd,          /* \W */    string_PXwd,          /* \W */
353    string_pXwd           /* \w */    string_pXwd           /* \w */
354  };  };
355    
356    /* The POSIX class substitutes must be in the order of the POSIX class names,
357    defined above, and there are both positive and negative cases. NULL means no
358    general substitute of a Unicode property escape (\p or \P). However, for some
359    POSIX classes (e.g. graph, print, punct) a special property code is compiled
360    directly. */
361    
362  static const pcre_uchar string_pL[] =   {  static const pcre_uchar string_pL[] =   {
363    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,    CHAR_BACKSLASH, CHAR_p, CHAR_LEFT_CURLY_BRACKET,
364    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };    CHAR_L, CHAR_RIGHT_CURLY_BRACKET, '\0' };
# Line 382  static const pcre_uchar *posix_substitut Line 406  static const pcre_uchar *posix_substitut
406    NULL,                 /* graph */    NULL,                 /* graph */
407    NULL,                 /* print */    NULL,                 /* print */
408    NULL,                 /* punct */    NULL,                 /* punct */
409    string_pXps,          /* space */    /* NOTE: Xps is POSIX space */    string_pXps,          /* space */   /* Xps is POSIX space, but from 8.34 */
410    string_pXwd,          /* word */    string_pXwd,          /* word  */   /* Perl and POSIX space are the same */
411    NULL,                 /* xdigit */    NULL,                 /* xdigit */
412    /* Negated cases */    /* Negated cases */
413    string_PL,            /* ^alpha */    string_PL,            /* ^alpha */
# Line 397  static const pcre_uchar *posix_substitut Line 421  static const pcre_uchar *posix_substitut
421    NULL,                 /* ^graph */    NULL,                 /* ^graph */
422    NULL,                 /* ^print */    NULL,                 /* ^print */
423    NULL,                 /* ^punct */    NULL,                 /* ^punct */
424    string_PXps,          /* ^space */   /* NOTE: Xps is POSIX space */    string_PXps,          /* ^space */  /* Xps is POSIX space, but from 8.34 */
425    string_PXwd,          /* ^word */    string_PXwd,          /* ^word */   /* Perl and POSIX space are the same */
426    NULL                  /* ^xdigit */    NULL                  /* ^xdigit */
427  };  };
428  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))  #define POSIX_SUBSIZE (sizeof(posix_substitutes) / sizeof(pcre_uchar *))
# Line 520  static const char error_texts[] = Line 544  static const char error_texts[] =
544    /* 80 */    /* 80 */
545    "non-octal character in \\o{} (closing brace missing?)\0"    "non-octal character in \\o{} (closing brace missing?)\0"
546    "missing opening brace after \\o\0"    "missing opening brace after \\o\0"
547      "parentheses are too deeply nested\0"
548      "invalid range in character class\0"
549      "group name must start with a non-digit\0"
550      /* 85 */
551      "parentheses are too deeply nested (stack check)\0"
552      "digits missing in \\x{} or \\o{}\0"
553    ;    ;
554    
555  /* Table to identify digits and hex digits. This is used when compiling  /* Table to identify digits and hex digits. This is used when compiling
# Line 777  static const pcre_uint8 posspropstab[3][ Line 807  static const pcre_uint8 posspropstab[3][
807    { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */    { ucp_L, ucp_N, ucp_P, ucp_Po }   /* WORD */
808  };  };
809    
810    /* This table is used when converting repeating opcodes into possessified
811    versions as a result of an explicit possessive quantifier such as ++. A zero
812    value means there is no possessified version - in those cases the item in
813    question must be wrapped in ONCE brackets. The table is truncated at OP_CALLOUT
814    because all relevant opcodes are less than that. */
815    
816    static const pcre_uint8 opcode_possessify[] = {
817      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0 - 15  */
818      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 16 - 31 */
819    
820      0,                       /* NOTI */
821      OP_POSSTAR, 0,           /* STAR, MINSTAR */
822      OP_POSPLUS, 0,           /* PLUS, MINPLUS */
823      OP_POSQUERY, 0,          /* QUERY, MINQUERY */
824      OP_POSUPTO, 0,           /* UPTO, MINUPTO */
825      0,                       /* EXACT */
826      0, 0, 0, 0,              /* POS{STAR,PLUS,QUERY,UPTO} */
827    
828      OP_POSSTARI, 0,          /* STARI, MINSTARI */
829      OP_POSPLUSI, 0,          /* PLUSI, MINPLUSI */
830      OP_POSQUERYI, 0,         /* QUERYI, MINQUERYI */
831      OP_POSUPTOI, 0,          /* UPTOI, MINUPTOI */
832      0,                       /* EXACTI */
833      0, 0, 0, 0,              /* POS{STARI,PLUSI,QUERYI,UPTOI} */
834    
835      OP_NOTPOSSTAR, 0,        /* NOTSTAR, NOTMINSTAR */
836      OP_NOTPOSPLUS, 0,        /* NOTPLUS, NOTMINPLUS */
837      OP_NOTPOSQUERY, 0,       /* NOTQUERY, NOTMINQUERY */
838      OP_NOTPOSUPTO, 0,        /* NOTUPTO, NOTMINUPTO */
839      0,                       /* NOTEXACT */
840      0, 0, 0, 0,              /* NOTPOS{STAR,PLUS,QUERY,UPTO} */
841    
842      OP_NOTPOSSTARI, 0,       /* NOTSTARI, NOTMINSTARI */
843      OP_NOTPOSPLUSI, 0,       /* NOTPLUSI, NOTMINPLUSI */
844      OP_NOTPOSQUERYI, 0,      /* NOTQUERYI, NOTMINQUERYI */
845      OP_NOTPOSUPTOI, 0,       /* NOTUPTOI, NOTMINUPTOI */
846      0,                       /* NOTEXACTI */
847      0, 0, 0, 0,              /* NOTPOS{STARI,PLUSI,QUERYI,UPTOI} */
848    
849      OP_TYPEPOSSTAR, 0,       /* TYPESTAR, TYPEMINSTAR */
850      OP_TYPEPOSPLUS, 0,       /* TYPEPLUS, TYPEMINPLUS */
851      OP_TYPEPOSQUERY, 0,      /* TYPEQUERY, TYPEMINQUERY */
852      OP_TYPEPOSUPTO, 0,       /* TYPEUPTO, TYPEMINUPTO */
853      0,                       /* TYPEEXACT */
854      0, 0, 0, 0,              /* TYPEPOS{STAR,PLUS,QUERY,UPTO} */
855    
856      OP_CRPOSSTAR, 0,         /* CRSTAR, CRMINSTAR */
857      OP_CRPOSPLUS, 0,         /* CRPLUS, CRMINPLUS */
858      OP_CRPOSQUERY, 0,        /* CRQUERY, CRMINQUERY */
859      OP_CRPOSRANGE, 0,        /* CRRANGE, CRMINRANGE */
860      0, 0, 0, 0,              /* CRPOS{STAR,PLUS,QUERY,RANGE} */
861    
862      0, 0, 0,                 /* CLASS, NCLASS, XCLASS */
863      0, 0,                    /* REF, REFI */
864      0, 0,                    /* DNREF, DNREFI */
865      0, 0                     /* RECURSE, CALLOUT */
866    };
867    
868    
869    
870  /*************************************************  /*************************************************
# Line 1172  else Line 1260  else
1260    
1261      case CHAR_o:      case CHAR_o:
1262      if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else      if (ptr[1] != CHAR_LEFT_CURLY_BRACKET) *errorcodeptr = ERR81; else
1263        if (ptr[2] == CHAR_RIGHT_CURLY_BRACKET) *errorcodeptr = ERR86; else
1264        {        {
1265        ptr += 2;        ptr += 2;
1266        c = 0;        c = 0;
# Line 1241  else Line 1330  else
1330        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)        if (ptr[1] == CHAR_LEFT_CURLY_BRACKET)
1331          {          {
1332          ptr += 2;          ptr += 2;
1333            if (*ptr == CHAR_RIGHT_CURLY_BRACKET)
1334              {
1335              *errorcodeptr = ERR86;
1336              break;
1337              }
1338          c = 0;          c = 0;
1339          overflow = FALSE;          overflow = FALSE;
1340          while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)          while (MAX_255(*ptr) && (digitab[*ptr] & ctype_xdigit) != 0)
# Line 1496  read_repeat_counts(const pcre_uchar *p, Line 1590  read_repeat_counts(const pcre_uchar *p,
1590  int min = 0;  int min = 0;
1591  int max = -1;  int max = -1;
1592    
1593  /* Read the minimum value and do a paranoid check: a negative value indicates  while (IS_DIGIT(*p))
 an integer overflow. */  
   
 while (IS_DIGIT(*p)) min = min * 10 + (int)(*p++ - CHAR_0);  
 if (min < 0 || min > 65535)  
1594    {    {
1595    *errorcodeptr = ERR5;    min = min * 10 + (int)(*p++ - CHAR_0);
1596    return p;    if (min > 65535)
1597        {
1598        *errorcodeptr = ERR5;
1599        return p;
1600        }
1601    }    }
1602    
 /* Read the maximum value if there is one, and again do a paranoid on its size.  
 Also, max must not be less than min. */  
   
1603  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else  if (*p == CHAR_RIGHT_CURLY_BRACKET) max = min; else
1604    {    {
1605    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)    if (*(++p) != CHAR_RIGHT_CURLY_BRACKET)
1606      {      {
1607      max = 0;      max = 0;
1608      while(IS_DIGIT(*p)) max = max * 10 + (int)(*p++ - CHAR_0);      while(IS_DIGIT(*p))
     if (max < 0 || max > 65535)  
1609        {        {
1610        *errorcodeptr = ERR5;        max = max * 10 + (int)(*p++ - CHAR_0);
1611        return p;        if (max > 65535)
1612            {
1613            *errorcodeptr = ERR5;
1614            return p;
1615            }
1616        }        }
1617      if (max < min)      if (max < min)
1618        {        {
# Line 1528  if (*p == CHAR_RIGHT_CURLY_BRACKET) max Line 1622  if (*p == CHAR_RIGHT_CURLY_BRACKET) max
1622      }      }
1623    }    }
1624    
 /* Fill in the required variables, and pass back the pointer to the terminating  
 '}'. */  
   
1625  *minp = min;  *minp = min;
1626  *maxp = max;  *maxp = max;
1627  return p;  return p;
# Line 1817  for (;;) Line 1908  for (;;)
1908    
1909      switch (*cc)      switch (*cc)
1910        {        {
       case OP_CRPLUS:  
       case OP_CRMINPLUS:  
1911        case OP_CRSTAR:        case OP_CRSTAR:
1912        case OP_CRMINSTAR:        case OP_CRMINSTAR:
1913          case OP_CRPLUS:
1914          case OP_CRMINPLUS:
1915        case OP_CRQUERY:        case OP_CRQUERY:
1916        case OP_CRMINQUERY:        case OP_CRMINQUERY:
1917          case OP_CRPOSSTAR:
1918          case OP_CRPOSPLUS:
1919          case OP_CRPOSQUERY:
1920        return -1;        return -1;
1921    
1922        case OP_CRRANGE:        case OP_CRRANGE:
1923        case OP_CRMINRANGE:        case OP_CRMINRANGE:
1924          case OP_CRPOSRANGE:
1925        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;        if (GET2(cc,1) != GET2(cc,1+IMM2_SIZE)) return -1;
1926        branchlength += (int)GET2(cc,1);        branchlength += (int)GET2(cc,1);
1927        cc += 1 + 2 * IMM2_SIZE;        cc += 1 + 2 * IMM2_SIZE;
# Line 2034  for (;;) Line 2129  for (;;)
2129        {        {
2130        case OP_CHAR:        case OP_CHAR:
2131        case OP_CHARI:        case OP_CHARI:
2132          case OP_NOT:
2133          case OP_NOTI:
2134        case OP_EXACT:        case OP_EXACT:
2135        case OP_EXACTI:        case OP_EXACTI:
2136          case OP_NOTEXACT:
2137          case OP_NOTEXACTI:
2138        case OP_UPTO:        case OP_UPTO:
2139        case OP_UPTOI:        case OP_UPTOI:
2140          case OP_NOTUPTO:
2141          case OP_NOTUPTOI:
2142        case OP_MINUPTO:        case OP_MINUPTO:
2143        case OP_MINUPTOI:        case OP_MINUPTOI:
2144          case OP_NOTMINUPTO:
2145          case OP_NOTMINUPTOI:
2146        case OP_POSUPTO:        case OP_POSUPTO:
2147        case OP_POSUPTOI:        case OP_POSUPTOI:
2148          case OP_NOTPOSUPTO:
2149          case OP_NOTPOSUPTOI:
2150        case OP_STAR:        case OP_STAR:
2151        case OP_STARI:        case OP_STARI:
2152          case OP_NOTSTAR:
2153          case OP_NOTSTARI:
2154        case OP_MINSTAR:        case OP_MINSTAR:
2155        case OP_MINSTARI:        case OP_MINSTARI:
2156          case OP_NOTMINSTAR:
2157          case OP_NOTMINSTARI:
2158        case OP_POSSTAR:        case OP_POSSTAR:
2159        case OP_POSSTARI:        case OP_POSSTARI:
2160          case OP_NOTPOSSTAR:
2161          case OP_NOTPOSSTARI:
2162        case OP_PLUS:        case OP_PLUS:
2163        case OP_PLUSI:        case OP_PLUSI:
2164          case OP_NOTPLUS:
2165          case OP_NOTPLUSI:
2166        case OP_MINPLUS:        case OP_MINPLUS:
2167        case OP_MINPLUSI:        case OP_MINPLUSI:
2168          case OP_NOTMINPLUS:
2169          case OP_NOTMINPLUSI:
2170        case OP_POSPLUS:        case OP_POSPLUS:
2171        case OP_POSPLUSI:        case OP_POSPLUSI:
2172          case OP_NOTPOSPLUS:
2173          case OP_NOTPOSPLUSI:
2174        case OP_QUERY:        case OP_QUERY:
2175        case OP_QUERYI:        case OP_QUERYI:
2176          case OP_NOTQUERY:
2177          case OP_NOTQUERYI:
2178        case OP_MINQUERY:        case OP_MINQUERY:
2179        case OP_MINQUERYI:        case OP_MINQUERYI:
2180          case OP_NOTMINQUERY:
2181          case OP_NOTMINQUERYI:
2182        case OP_POSQUERY:        case OP_POSQUERY:
2183        case OP_POSQUERYI:        case OP_POSQUERYI:
2184          case OP_NOTPOSQUERY:
2185          case OP_NOTPOSQUERYI:
2186        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);        if (HAS_EXTRALEN(code[-1])) code += GET_EXTRALEN(code[-1]);
2187        break;        break;
2188        }        }
# Line 2279  for (code = first_significant_code(code Line 2402  for (code = first_significant_code(code
2402    if (c == OP_RECURSE)    if (c == OP_RECURSE)
2403      {      {
2404      const pcre_uchar *scode = cd->start_code + GET(code, 1);      const pcre_uchar *scode = cd->start_code + GET(code, 1);
2405        const pcre_uchar *endgroup = scode;
2406      BOOL empty_branch;      BOOL empty_branch;
2407    
2408      /* Test for forward reference or uncompleted reference. This is disabled      /* Test for forward reference or uncompleted reference. This is disabled
# Line 2293  for (code = first_significant_code(code Line 2417  for (code = first_significant_code(code
2417        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */        if (GET(scode, 1) == 0) return TRUE;    /* Unclosed */
2418        }        }
2419    
2420      /* If we are scanning a completed pattern, there are no forward references      /* If the reference is to a completed group, we need to detect whether this
2421      and all groups are complete. We need to detect whether this is a recursive      is a recursive call, as otherwise there will be an infinite loop. If it is
2422      call, as otherwise there will be an infinite loop. If it is a recursion,      a recursion, just skip over it. Simple recursions are easily detected. For
2423      just skip over it. Simple recursions are easily detected. For mutual      mutual recursions we keep a chain on the stack. */
     recursions we keep a chain on the stack. */  
2424    
2425        do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);
2426        if (code >= scode && code <= endgroup) continue;  /* Simple recursion */
2427      else      else
2428        {        {
2429        recurse_check *r = recurses;        recurse_check *r = recurses;
       const pcre_uchar *endgroup = scode;  
   
       do endgroup += GET(endgroup, 1); while (*endgroup == OP_ALT);  
       if (code >= scode && code <= endgroup) continue;  /* Simple recursion */  
   
2430        for (r = recurses; r != NULL; r = r->prev)        for (r = recurses; r != NULL; r = r->prev)
2431          if (r->group == scode) break;          if (r->group == scode) break;
2432        if (r != NULL) continue;   /* Mutual recursion */        if (r != NULL) continue;   /* Mutual recursion */
# Line 2377  for (code = first_significant_code(code Line 2497  for (code = first_significant_code(code
2497        empty_branch = FALSE;        empty_branch = FALSE;
2498        do        do
2499          {          {
2500          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd, NULL))          if (!empty_branch && could_be_empty_branch(code, endcode, utf, cd,
2501            empty_branch = TRUE;            recurses)) empty_branch = TRUE;
2502          code += GET(code, 1);          code += GET(code, 1);
2503          }          }
2504        while (*code == OP_ALT);        while (*code == OP_ALT);
# Line 2419  for (code = first_significant_code(code Line 2539  for (code = first_significant_code(code
2539        case OP_CRMINSTAR:        case OP_CRMINSTAR:
2540        case OP_CRQUERY:        case OP_CRQUERY:
2541        case OP_CRMINQUERY:        case OP_CRMINQUERY:
2542          case OP_CRPOSSTAR:
2543          case OP_CRPOSQUERY:
2544        break;        break;
2545    
2546        default:                   /* Non-repeat => class must match */        default:                   /* Non-repeat => class must match */
2547        case OP_CRPLUS:            /* These repeats aren't empty */        case OP_CRPLUS:            /* These repeats aren't empty */
2548        case OP_CRMINPLUS:        case OP_CRMINPLUS:
2549          case OP_CRPOSPLUS:
2550        return FALSE;        return FALSE;
2551    
2552        case OP_CRRANGE:        case OP_CRRANGE:
2553        case OP_CRMINRANGE:        case OP_CRMINRANGE:
2554          case OP_CRPOSRANGE:
2555        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */        if (GET2(ccode, 1) > 0) return FALSE;  /* Minimum > 0 */
2556        break;        break;
2557        }        }
# Line 2714  switch(ptype) Line 2838  switch(ptype)
2838      HSPACE_CASES:      HSPACE_CASES:
2839      VSPACE_CASES:      VSPACE_CASES:
2840      return negated;      return negated;
2841    
2842      default:      default:
2843      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;      return (PRIV(ucp_gentype)[prop->chartype] == ucp_Z) == negated;
2844      }      }
2845    break;  /* Control never reaches here */    break;  /* Control never reaches here */
2846    
2847    case PT_WORD:    case PT_WORD:
# Line 2767  get_chr_property_list(const pcre_uchar * Line 2891  get_chr_property_list(const pcre_uchar *
2891    const pcre_uint8 *fcc, pcre_uint32 *list)    const pcre_uint8 *fcc, pcre_uint32 *list)
2892  {  {
2893  pcre_uchar c = *code;  pcre_uchar c = *code;
2894    pcre_uchar base;
2895  const pcre_uchar *end;  const pcre_uchar *end;
 const pcre_uint32 *clist_src;  
 pcre_uint32 *clist_dest;  
2896  pcre_uint32 chr;  pcre_uint32 chr;
2897  pcre_uchar base;  
2898    #ifdef SUPPORT_UCP
2899    pcre_uint32 *clist_dest;
2900    const pcre_uint32 *clist_src;
2901    #else
2902    utf = utf;  /* Suppress "unused parameter" compiler warning */
2903    #endif
2904    
2905  list[0] = c;  list[0] = c;
2906  list[1] = FALSE;  list[1] = FALSE;
# Line 2907  switch(c) Line 3036  switch(c)
3036    case OP_CLASS:    case OP_CLASS:
3037  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3038    case OP_XCLASS:    case OP_XCLASS:
   
3039    if (c == OP_XCLASS)    if (c == OP_XCLASS)
3040      end = code + GET(code, 0);      end = code + GET(code, 0) - 1;
3041    else    else
3042  #endif  #endif
3043      end = code + 32 / sizeof(pcre_uchar);      end = code + 32 / sizeof(pcre_uchar);
# Line 2920  switch(c) Line 3048  switch(c)
3048      case OP_CRMINSTAR:      case OP_CRMINSTAR:
3049      case OP_CRQUERY:      case OP_CRQUERY:
3050      case OP_CRMINQUERY:      case OP_CRMINQUERY:
3051        case OP_CRPOSSTAR:
3052        case OP_CRPOSQUERY:
3053      list[1] = TRUE;      list[1] = TRUE;
3054      end++;      end++;
3055      break;      break;
3056    
3057        case OP_CRPLUS:
3058        case OP_CRMINPLUS:
3059        case OP_CRPOSPLUS:
3060        end++;
3061        break;
3062    
3063      case OP_CRRANGE:      case OP_CRRANGE:
3064      case OP_CRMINRANGE:      case OP_CRMINRANGE:
3065        case OP_CRPOSRANGE:
3066      list[1] = (GET2(end, 1) == 0);      list[1] = (GET2(end, 1) == 0);
3067      end += 1 + 2 * IMM2_SIZE;      end += 1 + 2 * IMM2_SIZE;
3068      break;      break;
3069      }      }
3070    list[2] = end - code;    list[2] = (pcre_uint32)(end - code);
3071    return end;    return end;
3072    }    }
3073  return NULL;    /* Opcode not accepted */  return NULL;    /* Opcode not accepted */
# Line 2956  Returns:      TRUE if the auto-possessif Line 3093  Returns:      TRUE if the auto-possessif
3093    
3094  static BOOL  static BOOL
3095  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,  compare_opcodes(const pcre_uchar *code, BOOL utf, const compile_data *cd,
3096    const pcre_uint32* base_list)    const pcre_uint32 *base_list, const pcre_uchar *base_end, int *rec_limit)
3097  {  {
3098  pcre_uchar c;  pcre_uchar c;
3099  pcre_uint32 list[8];  pcre_uint32 list[8];
3100  const pcre_uint32* chr_ptr;  const pcre_uint32 *chr_ptr;
3101  const pcre_uint32* ochr_ptr;  const pcre_uint32 *ochr_ptr;
3102  const pcre_uint32* list_ptr;  const pcre_uint32 *list_ptr;
3103  const pcre_uchar *next_code;  const pcre_uchar *next_code;
3104    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3105    const pcre_uchar *xclass_flags;
3106    #endif
3107    const pcre_uint8 *class_bitset;
3108    const pcre_uint8 *set1, *set2, *set_end;
3109  pcre_uint32 chr;  pcre_uint32 chr;
3110    BOOL accepted, invert_bits;
3111    BOOL entered_a_group = FALSE;
3112    
3113    if (*rec_limit == 0) return FALSE;
3114    --(*rec_limit);
3115    
3116  /* Note: the base_list[1] contains whether the current opcode has greedy  /* Note: the base_list[1] contains whether the current opcode has greedy
3117  (represented by a non-zero value) quantifier. This is a different from  (represented by a non-zero value) quantifier. This is a different from
# Line 2973  matches to an empty string (also represe Line 3120  matches to an empty string (also represe
3120    
3121  for(;;)  for(;;)
3122    {    {
3123      /* All operations move the code pointer forward.
3124      Therefore infinite recursions are not possible. */
3125    
3126    c = *code;    c = *code;
3127    
3128    /* Skip over callouts */    /* Skip over callouts */
# Line 3015  for(;;) Line 3165  for(;;)
3165        case OP_ONCE:        case OP_ONCE:
3166        case OP_ONCE_NC:        case OP_ONCE_NC:
3167        /* Atomic sub-patterns and assertions can always auto-possessify their        /* Atomic sub-patterns and assertions can always auto-possessify their
3168        last iterator. */        last iterator. However, if the group was entered as a result of checking
3169        return TRUE;        a previous iterator, this is not possible. */
3170    
3171          return !entered_a_group;
3172        }        }
3173    
3174      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
# Line 3026  for(;;) Line 3178  for(;;)
3178      case OP_ONCE_NC:      case OP_ONCE_NC:
3179      case OP_BRA:      case OP_BRA:
3180      case OP_CBRA:      case OP_CBRA:
     next_code = code;  
     do next_code += GET(next_code, 1); while (*next_code == OP_ALT);  
   
     /* We do not support repeated brackets, because they can lead to  
     infinite recursion. */  
   
     if (*next_code != OP_KET) return FALSE;  
   
3181      next_code = code + GET(code, 1);      next_code = code + GET(code, 1);
3182      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
3183    
3184      while (*next_code == OP_ALT)      while (*next_code == OP_ALT)
3185        {        {
3186        if (!compare_opcodes(code, utf, cd, base_list)) return FALSE;        if (!compare_opcodes(code, utf, cd, base_list, base_end, rec_limit))
3187            return FALSE;
3188        code = next_code + 1 + LINK_SIZE;        code = next_code + 1 + LINK_SIZE;
3189        next_code += GET(next_code, 1);        next_code += GET(next_code, 1);
3190        }        }
3191    
3192        entered_a_group = TRUE;
3193      continue;      continue;
3194    
3195      case OP_BRAZERO:      case OP_BRAZERO:
3196      case OP_BRAMINZERO:      case OP_BRAMINZERO:
3197    
3198      next_code = code + 1;      next_code = code + 1;
3199      if (*next_code != OP_BRA && *next_code != OP_CBRA)      if (*next_code != OP_BRA && *next_code != OP_CBRA
3200        return FALSE;          && *next_code != OP_ONCE && *next_code != OP_ONCE_NC) return FALSE;
3201    
3202      do next_code += GET(next_code, 1); while (*next_code == OP_ALT);      do next_code += GET(next_code, 1); while (*next_code == OP_ALT);
3203    
     /* We do not support repeated brackets, because they can lead to  
     infinite recursion. */  
     if (*next_code != OP_KET) return FALSE;  
   
3204      /* The bracket content will be checked by the      /* The bracket content will be checked by the
3205      OP_BRA/OP_CBRA case above. */      OP_BRA/OP_CBRA case above. */
3206      next_code += 1 + LINK_SIZE;      next_code += 1 + LINK_SIZE;
3207      if (!compare_opcodes(next_code, utf, cd, base_list)) return FALSE;      if (!compare_opcodes(next_code, utf, cd, base_list, base_end, rec_limit))
3208          return FALSE;
3209    
3210      code += PRIV(OP_lengths)[c];      code += PRIV(OP_lengths)[c];
3211      continue;      continue;
3212    
3213        default:
3214        break;
3215      }      }
3216    
3217    /* Check for a supported opcode, and load its properties. */    /* Check for a supported opcode, and load its properties. */
# Line 3086  for(;;) Line 3233  for(;;)
3233      list_ptr = base_list;      list_ptr = base_list;
3234      }      }
3235    
3236      /* Character bitsets can also be compared to certain opcodes. */
3237    
3238      else if (base_list[0] == OP_CLASS || list[0] == OP_CLASS
3239    #ifdef COMPILE_PCRE8
3240          /* In 8 bit, non-UTF mode, OP_CLASS and OP_NCLASS are the same. */
3241          || (!utf && (base_list[0] == OP_NCLASS || list[0] == OP_NCLASS))
3242    #endif
3243          )
3244        {
3245    #ifdef COMPILE_PCRE8
3246        if (base_list[0] == OP_CLASS || (!utf && base_list[0] == OP_NCLASS))
3247    #else
3248        if (base_list[0] == OP_CLASS)
3249    #endif
3250          {
3251          set1 = (pcre_uint8 *)(base_end - base_list[2]);
3252          list_ptr = list;
3253          }
3254        else
3255          {
3256          set1 = (pcre_uint8 *)(code - list[2]);
3257          list_ptr = base_list;
3258          }
3259    
3260        invert_bits = FALSE;
3261        switch(list_ptr[0])
3262          {
3263          case OP_CLASS:
3264          case OP_NCLASS:
3265          set2 = (pcre_uint8 *)
3266            ((list_ptr == list ? code : base_end) - list_ptr[2]);
3267          break;
3268    
3269    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3270          case OP_XCLASS:
3271          xclass_flags = (list_ptr == list ? code : base_end) - list_ptr[2] + LINK_SIZE;
3272          if ((*xclass_flags & XCL_HASPROP) != 0) return FALSE;
3273          if ((*xclass_flags & XCL_MAP) == 0)
3274            {
3275            /* No bits are set for characters < 256. */
3276            if (list[1] == 0) return TRUE;
3277            /* Might be an empty repeat. */
3278            continue;
3279            }
3280          set2 = (pcre_uint8 *)(xclass_flags + 1);
3281          break;
3282    #endif
3283    
3284          case OP_NOT_DIGIT:
3285          invert_bits = TRUE;
3286          /* Fall through */
3287          case OP_DIGIT:
3288          set2 = (pcre_uint8 *)(cd->cbits + cbit_digit);
3289          break;
3290    
3291          case OP_NOT_WHITESPACE:
3292          invert_bits = TRUE;
3293          /* Fall through */
3294          case OP_WHITESPACE:
3295          set2 = (pcre_uint8 *)(cd->cbits + cbit_space);
3296          break;
3297    
3298          case OP_NOT_WORDCHAR:
3299          invert_bits = TRUE;
3300          /* Fall through */
3301          case OP_WORDCHAR:
3302          set2 = (pcre_uint8 *)(cd->cbits + cbit_word);
3303          break;
3304    
3305          default:
3306          return FALSE;
3307          }
3308    
3309        /* Because the sets are unaligned, we need
3310        to perform byte comparison here. */
3311        set_end = set1 + 32;
3312        if (invert_bits)
3313          {
3314          do
3315            {
3316            if ((*set1++ & ~(*set2++)) != 0) return FALSE;
3317            }
3318          while (set1 < set_end);
3319          }
3320        else
3321          {
3322          do
3323            {
3324            if ((*set1++ & *set2++) != 0) return FALSE;
3325            }
3326          while (set1 < set_end);
3327          }
3328    
3329        if (list[1] == 0) return TRUE;
3330        /* Might be an empty repeat. */
3331        continue;
3332        }
3333    
3334    /* Some property combinations also acceptable. Unicode property opcodes are    /* Some property combinations also acceptable. Unicode property opcodes are
3335    processed specially; the rest can be handled with a lookup table. */    processed specially; the rest can be handled with a lookup table. */
3336    
# Line 3093  for(;;) Line 3338  for(;;)
3338      {      {
3339      pcre_uint32 leftop, rightop;      pcre_uint32 leftop, rightop;
3340    
     if (list[1] != 0) return FALSE;   /* Must match at least one character */  
3341      leftop = base_list[0];      leftop = base_list[0];
3342      rightop = list[0];      rightop = list[0];
3343    
3344  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
3345        accepted = FALSE; /* Always set in non-unicode case. */
3346      if (leftop == OP_PROP || leftop == OP_NOTPROP)      if (leftop == OP_PROP || leftop == OP_NOTPROP)
3347        {        {
3348        if (rightop == OP_EOD) return TRUE;        if (rightop == OP_EOD)
3349        if (rightop == OP_PROP || rightop == OP_NOTPROP)          accepted = TRUE;
3350          else if (rightop == OP_PROP || rightop == OP_NOTPROP)
3351          {          {
3352          int n;          int n;
3353          const pcre_uint8 *p;          const pcre_uint8 *p;
# Line 3122  for(;;) Line 3368  for(;;)
3368          n = propposstab[base_list[2]][list[2]];          n = propposstab[base_list[2]][list[2]];
3369          switch(n)          switch(n)
3370            {            {
3371            case 0: return FALSE;            case 0: break;
3372            case 1: return bothprop;            case 1: accepted = bothprop; break;
3373            case 2: return (base_list[3] == list[3]) != same;            case 2: accepted = (base_list[3] == list[3]) != same; break;
3374            case 3: return !same;            case 3: accepted = !same; break;
3375    
3376            case 4:  /* Left general category, right particular category */            case 4:  /* Left general category, right particular category */
3377            return risprop && catposstab[base_list[3]][list[3]] == same;            accepted = risprop && catposstab[base_list[3]][list[3]] == same;
3378              break;
3379    
3380            case 5:  /* Right general category, left particular category */            case 5:  /* Right general category, left particular category */
3381            return lisprop && catposstab[list[3]][base_list[3]] == same;            accepted = lisprop && catposstab[list[3]][base_list[3]] == same;
3382              break;
3383    
3384            /* This code is logically tricky. Think hard before fiddling with it.            /* This code is logically tricky. Think hard before fiddling with it.
3385            The posspropstab table has four entries per row. Each row relates to            The posspropstab table has four entries per row. Each row relates to
# Line 3156  for(;;) Line 3404  for(;;)
3404            case 7:  /* Left space vs right general category */            case 7:  /* Left space vs right general category */
3405            case 8:  /* Left word vs right general category */            case 8:  /* Left word vs right general category */
3406            p = posspropstab[n-6];            p = posspropstab[n-6];
3407            return risprop && lisprop ==            accepted = risprop && lisprop ==
3408              (list[3] != p[0] &&              (list[3] != p[0] &&
3409               list[3] != p[1] &&               list[3] != p[1] &&
3410              (list[3] != p[2] || !lisprop));              (list[3] != p[2] || !lisprop));
3411              break;
3412    
3413            case 9:   /* Right alphanum vs left general category */            case 9:   /* Right alphanum vs left general category */
3414            case 10:  /* Right space vs left general category */            case 10:  /* Right space vs left general category */
3415            case 11:  /* Right word vs left general category */            case 11:  /* Right word vs left general category */
3416            p = posspropstab[n-9];            p = posspropstab[n-9];
3417            return lisprop && risprop ==            accepted = lisprop && risprop ==
3418              (base_list[3] != p[0] &&              (base_list[3] != p[0] &&
3419               base_list[3] != p[1] &&               base_list[3] != p[1] &&
3420              (base_list[3] != p[2] || !risprop));              (base_list[3] != p[2] || !risprop));
3421              break;
3422    
3423            case 12:  /* Left alphanum vs right particular category */            case 12:  /* Left alphanum vs right particular category */
3424            case 13:  /* Left space vs right particular category */            case 13:  /* Left space vs right particular category */
3425            case 14:  /* Left word vs right particular category */            case 14:  /* Left word vs right particular category */
3426            p = posspropstab[n-12];            p = posspropstab[n-12];
3427            return risprop && lisprop ==            accepted = risprop && lisprop ==
3428              (catposstab[p[0]][list[3]] &&              (catposstab[p[0]][list[3]] &&
3429               catposstab[p[1]][list[3]] &&               catposstab[p[1]][list[3]] &&
3430              (list[3] != p[3] || !lisprop));              (list[3] != p[3] || !lisprop));
3431              break;
3432    
3433            case 15:  /* Right alphanum vs left particular category */            case 15:  /* Right alphanum vs left particular category */
3434            case 16:  /* Right space vs left particular category */            case 16:  /* Right space vs left particular category */
3435            case 17:  /* Right word vs left particular category */            case 17:  /* Right word vs left particular category */
3436            p = posspropstab[n-15];            p = posspropstab[n-15];
3437            return lisprop && risprop ==            accepted = lisprop && risprop ==
3438              (catposstab[p[0]][base_list[3]] &&              (catposstab[p[0]][base_list[3]] &&
3439               catposstab[p[1]][base_list[3]] &&               catposstab[p[1]][base_list[3]] &&
3440              (base_list[3] != p[3] || !risprop));              (base_list[3] != p[3] || !risprop));
3441              break;
3442            }            }
3443          }          }
       return FALSE;  
3444        }        }
3445    
3446      else      else
3447  #endif  /* SUPPORT_UCP */  #endif  /* SUPPORT_UCP */
3448    
3449      return leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&      accepted = leftop >= FIRST_AUTOTAB_OP && leftop <= LAST_AUTOTAB_LEFT_OP &&
3450             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&             rightop >= FIRST_AUTOTAB_OP && rightop <= LAST_AUTOTAB_RIGHT_OP &&
3451             autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];             autoposstab[leftop - FIRST_AUTOTAB_OP][rightop - FIRST_AUTOTAB_OP];
3452    
3453        if (!accepted) return FALSE;
3454    
3455        if (list[1] == 0) return TRUE;
3456        /* Might be an empty repeat. */
3457        continue;
3458      }      }
3459    
3460    /* Control reaches here only if one of the items is a small character list.    /* Control reaches here only if one of the items is a small character list.
# Line 3311  for(;;) Line 3568  for(;;)
3568        case OP_EOD:    /* Can always possessify before \z */        case OP_EOD:    /* Can always possessify before \z */
3569        break;        break;
3570    
3571    #ifdef SUPPORT_UCP
3572        case OP_PROP:        case OP_PROP:
3573        case OP_NOTPROP:        case OP_NOTPROP:
3574        if (!check_char_prop(chr, list_ptr[2], list_ptr[3],        if (!check_char_prop(chr, list_ptr[2], list_ptr[3],
3575              list_ptr[0] == OP_NOTPROP))              list_ptr[0] == OP_NOTPROP))
3576          return FALSE;          return FALSE;
3577        break;        break;
3578    #endif
       /* The class comparisons work only when the class is the second item  
       of the pair, because there are at present no possessive forms of the  
       class opcodes. Note also that the "code" variable that is used below  
       points after the second item, and that the pointer for the first item  
       is not available, so even if there were possessive forms of the class  
       opcodes, the correct comparison could not be done. */  
3579    
3580        case OP_NCLASS:        case OP_NCLASS:
3581        if (chr > 255) return FALSE;        if (chr > 255) return FALSE;
3582        /* Fall through */        /* Fall through */
3583    
3584        case OP_CLASS:        case OP_CLASS:
       if (list_ptr != list) return FALSE;   /* Class is first opcode */  
3585        if (chr > 255) break;        if (chr > 255) break;
3586        if ((((pcre_uint8 *)(code - list_ptr[2]))[chr >> 3] & (1 << (chr & 7))) != 0)        class_bitset = (pcre_uint8 *)
3587          return FALSE;          ((list_ptr == list ? code : base_end) - list_ptr[2]);
3588          if ((class_bitset[chr >> 3] & (1 << (chr & 7))) != 0) return FALSE;
3589        break;        break;
3590    
3591  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3592        case OP_XCLASS:        case OP_XCLASS:
3593        if (list_ptr != list) return FALSE;   /* Class is first opcode */        if (PRIV(xclass)(chr, (list_ptr == list ? code : base_end) -
3594        if (PRIV(xclass)(chr, code - list_ptr[2] + LINK_SIZE, utf))            list_ptr[2] + LINK_SIZE, utf)) return FALSE;
         return FALSE;  
3595        break;        break;
3596  #endif  #endif
3597    
# Line 3357  for(;;) Line 3608  for(;;)
3608    if (list[1] == 0) return TRUE;    if (list[1] == 0) return TRUE;
3609    }    }
3610    
3611  return FALSE;  /* Control never reaches here. There used to be a fail-save return FALSE; here,
3612    but some compilers complain about an unreachable statement. */
3613    
3614  }  }
3615    
3616    
# Line 3382  auto_possessify(pcre_uchar *code, BOOL u Line 3635  auto_possessify(pcre_uchar *code, BOOL u
3635  {  {
3636  register pcre_uchar c;  register pcre_uchar c;
3637  const pcre_uchar *end;  const pcre_uchar *end;
3638    pcre_uchar *repeat_opcode;
3639  pcre_uint32 list[8];  pcre_uint32 list[8];
3640    int rec_limit;
3641    
3642  for (;;)  for (;;)
3643    {    {
3644    c = *code;    c = *code;
3645    
3646      /* When a pattern with bad UTF-8 encoding is compiled with NO_UTF_CHECK,
3647      it may compile without complaining, but may get into a loop here if the code
3648      pointer points to a bad value. This is, of course a documentated possibility,
3649      when NO_UTF_CHECK is set, so it isn't a bug, but we can detect this case and
3650      just give up on this optimization. */
3651    
3652      if (c >= OP_TABLE_LENGTH) return;
3653    
3654    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)    if (c >= OP_STAR && c <= OP_TYPEPOSUPTO)
3655      {      {
3656      c -= get_repeat_base(c) - OP_STAR;      c -= get_repeat_base(c) - OP_STAR;
# Line 3395  for (;;) Line 3658  for (;;)
3658        get_chr_property_list(code, utf, cd->fcc, list) : NULL;        get_chr_property_list(code, utf, cd->fcc, list) : NULL;
3659      list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;      list[1] = c == OP_STAR || c == OP_PLUS || c == OP_QUERY || c == OP_UPTO;
3660    
3661      if (end != NULL && compare_opcodes(end, utf, cd, list))      rec_limit = 10000;
3662        if (end != NULL && compare_opcodes(end, utf, cd, list, end, &rec_limit))
3663        {        {
3664        switch(c)        switch(c)
3665          {          {
# Line 3428  for (;;) Line 3692  for (;;)
3692          break;          break;
3693    
3694          case OP_MINUPTO:          case OP_MINUPTO:
3695          *code += OP_MINUPTO - OP_UPTO;          *code += OP_POSUPTO - OP_MINUPTO;
3696          break;          break;
3697          }          }
3698        }        }
3699      c = *code;      c = *code;
3700      }      }
3701      else if (c == OP_CLASS || c == OP_NCLASS || c == OP_XCLASS)
3702        {
3703    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3704        if (c == OP_XCLASS)
3705          repeat_opcode = code + GET(code, 1);
3706        else
3707    #endif
3708          repeat_opcode = code + 1 + (32 / sizeof(pcre_uchar));
3709    
3710        c = *repeat_opcode;
3711        if (c >= OP_CRSTAR && c <= OP_CRMINRANGE)
3712          {
3713          /* end must not be NULL. */
3714          end = get_chr_property_list(code, utf, cd->fcc, list);
3715    
3716          list[1] = (c & 1) == 0;
3717    
3718          rec_limit = 10000;
3719          if (compare_opcodes(end, utf, cd, list, end, &rec_limit))
3720            {
3721            switch (c)
3722              {
3723              case OP_CRSTAR:
3724              case OP_CRMINSTAR:
3725              *repeat_opcode = OP_CRPOSSTAR;
3726              break;
3727    
3728              case OP_CRPLUS:
3729              case OP_CRMINPLUS:
3730              *repeat_opcode = OP_CRPOSPLUS;
3731              break;
3732    
3733              case OP_CRQUERY:
3734              case OP_CRMINQUERY:
3735              *repeat_opcode = OP_CRPOSQUERY;
3736              break;
3737    
3738              case OP_CRRANGE:
3739              case OP_CRMINRANGE:
3740              *repeat_opcode = OP_CRPOSRANGE;
3741              break;
3742              }
3743            }
3744          }
3745        c = *code;
3746        }
3747    
3748    switch(c)    switch(c)
3749      {      {
# Line 3460  for (;;) Line 3770  for (;;)
3770        code += 2;        code += 2;
3771      break;      break;
3772    
3773    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
3774      case OP_XCLASS:      case OP_XCLASS:
3775      code += GET(code, 1);      code += GET(code, 1);
3776      break;      break;
3777    #endif
3778    
3779      case OP_MARK:      case OP_MARK:
3780      case OP_PRUNE_ARG:      case OP_PRUNE_ARG:
# Line 3571  class, but [abc[:x\]pqr:]] is (so that a Line 3883  class, but [abc[:x\]pqr:]] is (so that a
3883  below handles the special case of \], but does not try to do any other escape  below handles the special case of \], but does not try to do any other escape
3884  processing. This makes it different from Perl for cases such as [:l\ower:]  processing. This makes it different from Perl for cases such as [:l\ower:]
3885  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize  where Perl recognizes it as the POSIX class "lower" but PCRE does not recognize
3886  "l\ower". This is a lesser evil that not diagnosing bad classes when Perl does,  "l\ower". This is a lesser evil than not diagnosing bad classes when Perl does,
3887  I think.  I think.
3888    
3889  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.  A user pointed out that PCRE was rejecting [:a[:digit:]] whereas Perl was not.
# Line 3678  Arguments: Line 3990  Arguments:
3990    adjust     the amount by which the group is to be moved    adjust     the amount by which the group is to be moved
3991    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode    utf        TRUE in UTF-8 / UTF-16 / UTF-32 mode
3992    cd         contains pointers to tables etc.    cd         contains pointers to tables etc.
3993    save_hwm   the hwm forward reference pointer at the start of the group    save_hwm_offset   the hwm forward reference offset at the start of the group
3994    
3995  Returns:     nothing  Returns:     nothing
3996  */  */
3997    
3998  static void  static void
3999  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,  adjust_recurse(pcre_uchar *group, int adjust, BOOL utf, compile_data *cd,
4000    pcre_uchar *save_hwm)    size_t save_hwm_offset)
4001  {  {
4002  pcre_uchar *ptr = group;  pcre_uchar *ptr = group;
4003    
# Line 3697  while ((ptr = (pcre_uchar *)find_recurse Line 4009  while ((ptr = (pcre_uchar *)find_recurse
4009    /* See if this recursion is on the forward reference list. If so, adjust the    /* See if this recursion is on the forward reference list. If so, adjust the
4010    reference. */    reference. */
4011    
4012    for (hc = save_hwm; hc < cd->hwm; hc += LINK_SIZE)    for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset; hc < cd->hwm;
4013           hc += LINK_SIZE)
4014      {      {
4015      offset = (int)GET(hc, 0);      offset = (int)GET(hc, 0);
4016      if (cd->start_code + offset == ptr + 1)      if (cd->start_code + offset == ptr + 1)
# Line 3820  for (c = *cptr; c <= d; c++) Line 4133  for (c = *cptr; c <= d; c++)
4133    
4134  if (c > d) return -1;  /* Reached end of range */  if (c > d) return -1;  /* Reached end of range */
4135    
4136    /* Found a character that has a single other case. Search for the end of the
4137    range, which is either the end of the input range, or a character that has zero
4138    or more than one other cases. */
4139    
4140  *ocptr = othercase;  *ocptr = othercase;
4141  next = othercase + 1;  next = othercase + 1;
4142    
4143  for (++c; c <= d; c++)  for (++c; c <= d; c++)
4144    {    {
4145    if (UCD_OTHERCASE(c) != next) break;    if ((co = UCD_CASESET(c)) != 0 || UCD_OTHERCASE(c) != next) break;
4146    next++;    next++;
4147    }    }
4148    
# Line 3863  add_to_class(pcre_uint8 *classbits, pcre Line 4180  add_to_class(pcre_uint8 *classbits, pcre
4180    compile_data *cd, pcre_uint32 start, pcre_uint32 end)    compile_data *cd, pcre_uint32 start, pcre_uint32 end)
4181  {  {
4182  pcre_uint32 c;  pcre_uint32 c;
4183    pcre_uint32 classbits_end = (end <= 0xff ? end : 0xff);
4184  int n8 = 0;  int n8 = 0;
4185    
4186  /* If caseless matching is required, scan the range and process alternate  /* If caseless matching is required, scan the range and process alternate
# Line 3897  if ((options & PCRE_CASELESS) != 0) Line 4215  if ((options & PCRE_CASELESS) != 0)
4215        range. Otherwise, use a recursive call to add the additional range. */        range. Otherwise, use a recursive call to add the additional range. */
4216    
4217        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */        else if (oc < start && od >= start - 1) start = oc; /* Extend downwards */
4218        else if (od > end && oc <= end + 1) end = od;       /* Extend upwards */        else if (od > end && oc <= end + 1)
4219            {
4220            end = od;       /* Extend upwards */
4221            if (end > classbits_end) classbits_end = (end <= 0xff ? end : 0xff);
4222            }
4223        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);        else n8 += add_to_class(classbits, uchardptr, options, cd, oc, od);
4224        }        }
4225      }      }
# Line 3906  if ((options & PCRE_CASELESS) != 0) Line 4228  if ((options & PCRE_CASELESS) != 0)
4228    
4229    /* Not UTF-mode, or no UCP */    /* Not UTF-mode, or no UCP */
4230    
4231    for (c = start; c <= end && c < 256; c++)    for (c = start; c <= classbits_end; c++)
4232      {      {
4233      SETBIT(classbits, cd->fcc[c]);      SETBIT(classbits, cd->fcc[c]);
4234      n8++;      n8++;
# Line 3931  in all cases. */ Line 4253  in all cases. */
4253    
4254  #endif /* COMPILE_PCRE[8|16] */  #endif /* COMPILE_PCRE[8|16] */
4255    
4256  /* If all characters are less than 256, use the bit map. Otherwise use extra  /* Use the bitmap for characters < 256. Otherwise use extra data.*/
 data. */  
4257    
4258  if (end < 0x100)  for (c = start; c <= classbits_end; c++)
4259    {    {
4260    for (c = start; c <= end; c++)    /* Regardless of start, c will always be <= 255. */
4261      {    SETBIT(classbits, c);
4262      n8++;    n8++;
     SETBIT(classbits, c);  
     }  
4263    }    }
4264    
4265  else  #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4266    if (start <= 0xff) start = 0xff + 1;
4267    
4268    if (end >= start)
4269    {    {
4270    pcre_uchar *uchardata = *uchardptr;    pcre_uchar *uchardata = *uchardptr;
   
4271  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4272    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */    if ((options & PCRE_UTF8) != 0)  /* All UTFs use the same flag bit */
4273      {      {
# Line 3986  else Line 4307  else
4307    
4308    *uchardptr = uchardata;   /* Updata extra data pointer */    *uchardptr = uchardata;   /* Updata extra data pointer */
4309    }    }
4310    #endif /* SUPPORT_UTF || !COMPILE_PCRE8 */
4311    
4312  return n8;    /* Number of 8-bit characters */  return n8;    /* Number of 8-bit characters */
4313  }  }
# Line 4137  const pcre_uchar *tempptr; Line 4459  const pcre_uchar *tempptr;
4459  const pcre_uchar *nestptr = NULL;  const pcre_uchar *nestptr = NULL;
4460  pcre_uchar *previous = NULL;  pcre_uchar *previous = NULL;
4461  pcre_uchar *previous_callout = NULL;  pcre_uchar *previous_callout = NULL;
4462  pcre_uchar *save_hwm = NULL;  size_t save_hwm_offset = 0;
4463  pcre_uint8 classbits[32];  pcre_uint8 classbits[32];
4464    
4465  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we  /* We can fish out the UTF-8 setting once and for all into a BOOL, but we
# Line 4207  for (;; ptr++) Line 4529  for (;; ptr++)
4529    BOOL reset_bracount;    BOOL reset_bracount;
4530    int class_has_8bitchar;    int class_has_8bitchar;
4531    int class_one_char;    int class_one_char;
4532    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4533      BOOL xclass_has_prop;
4534    #endif
4535    int newoptions;    int newoptions;
4536    int recno;    int recno;
4537    int refsign;    int refsign;
# Line 4324  for (;; ptr++) Line 4649  for (;; ptr++)
4649          }          }
4650        goto NORMAL_CHAR;        goto NORMAL_CHAR;
4651        }        }
4652        /* Control does not reach here. */
4653      }      }
4654    
4655    is_quantifier =    /* In extended mode, skip white space and comments. We need a loop in order
4656      c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||    to check for more white space and more comments after a comment. */
     (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));  
   
   /* Fill in length of a previous callout, except when the next thing is a  
   quantifier or when processing a property substitution string in UCP mode. */  
   
   if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&  
        after_manual_callout-- <= 0)  
     {  
     if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */  
       complete_callout(previous_callout, ptr, cd);  
     previous_callout = NULL;  
     }  
   
   /* In extended mode, skip white space and comments. */  
4657    
4658    if ((options & PCRE_EXTENDED) != 0)    if ((options & PCRE_EXTENDED) != 0)
4659      {      {
4660      if (MAX_255(*ptr) && (cd->ctypes[c] & ctype_space) != 0) continue;      for (;;)
     if (c == CHAR_NUMBER_SIGN)  
4661        {        {
4662          while (MAX_255(c) && (cd->ctypes[c] & ctype_space) != 0) c = *(++ptr);
4663          if (c != CHAR_NUMBER_SIGN) break;
4664        ptr++;        ptr++;
4665        while (*ptr != CHAR_NULL)        while (*ptr != CHAR_NULL)
4666          {          {
4667          if (IS_NEWLINE(ptr)) { ptr += cd->nllen - 1; break; }          if (IS_NEWLINE(ptr))         /* For non-fixed-length newline cases, */
4668              {                          /* IS_NEWLINE sets cd->nllen. */
4669              ptr += cd->nllen;
4670              break;
4671              }
4672          ptr++;          ptr++;
4673  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
4674          if (utf) FORWARDCHAR(ptr);          if (utf) FORWARDCHAR(ptr);
4675  #endif  #endif
4676          }          }
4677        if (*ptr != CHAR_NULL) continue;        c = *ptr;     /* Either NULL or the char after a newline */
   
       /* Else fall through to handle end of string */  
       c = 0;  
4678        }        }
4679      }      }
4680    
4681    /* No auto callout for quantifiers, or while processing property strings that    /* See if the next thing is a quantifier. */
4682    are substituted for \w etc in UCP mode. */  
4683      is_quantifier =
4684        c == CHAR_ASTERISK || c == CHAR_PLUS || c == CHAR_QUESTION_MARK ||
4685        (c == CHAR_LEFT_CURLY_BRACKET && is_counted_repeat(ptr+1));
4686    
4687      /* Fill in length of a previous callout, except when the next thing is a
4688      quantifier or when processing a property substitution string in UCP mode. */
4689    
4690      if (!is_quantifier && previous_callout != NULL && nestptr == NULL &&
4691           after_manual_callout-- <= 0)
4692        {
4693        if (lengthptr == NULL)      /* Don't attempt in pre-compile phase */
4694          complete_callout(previous_callout, ptr, cd);
4695        previous_callout = NULL;
4696        }
4697    
4698      /* Create auto callout, except for quantifiers, or while processing property
4699      strings that are substituted for \w etc in UCP mode. */
4700    
4701    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)    if ((options & PCRE_AUTO_CALLOUT) != 0 && !is_quantifier && nestptr == NULL)
4702      {      {
# Line 4373  for (;; ptr++) Line 4704  for (;; ptr++)
4704      code = auto_callout(code, ptr, cd);      code = auto_callout(code, ptr, cd);
4705      }      }
4706    
4707      /* Process the next pattern item. */
4708    
4709    switch(c)    switch(c)
4710      {      {
4711      /* ===================================================================*/      /* ===================================================================*/
4712      case 0:                        /* The branch terminates at string end */      case CHAR_NULL:                /* The branch terminates at string end */
4713      case CHAR_VERTICAL_LINE:       /* or | or ) */      case CHAR_VERTICAL_LINE:       /* or | or ) */
4714      case CHAR_RIGHT_PARENTHESIS:      case CHAR_RIGHT_PARENTHESIS:
4715      *firstcharptr = firstchar;      *firstcharptr = firstchar;
# Line 4406  for (;; ptr++) Line 4739  for (;; ptr++)
4739      previous = NULL;      previous = NULL;
4740      if ((options & PCRE_MULTILINE) != 0)      if ((options & PCRE_MULTILINE) != 0)
4741        {        {
4742        if (firstcharflags == REQ_UNSET) firstcharflags = REQ_NONE;        if (firstcharflags == REQ_UNSET)
4743            zerofirstcharflags = firstcharflags = REQ_NONE;
4744        *code++ = OP_CIRCM;        *code++ = OP_CIRCM;
4745        }        }
4746      else *code++ = OP_CIRC;      else *code++ = OP_CIRC;
# Line 4454  for (;; ptr++) Line 4788  for (;; ptr++)
4788        }        }
4789      goto NORMAL_CHAR;      goto NORMAL_CHAR;
4790    
4791        /* In another (POSIX) regex library, the ugly syntax [[:<:]] and [[:>:]] is
4792        used for "start of word" and "end of word". As these are otherwise illegal
4793        sequences, we don't break anything by recognizing them. They are replaced
4794        by \b(?=\w) and \b(?<=\w) respectively. Sequences like [a[:<:]] are
4795        erroneous and are handled by the normal code below. */
4796    
4797      case CHAR_LEFT_SQUARE_BRACKET:      case CHAR_LEFT_SQUARE_BRACKET:
4798        if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_STARTWORD, 6) == 0)
4799          {
4800          nestptr = ptr + 7;
4801          ptr = sub_start_of_word - 1;
4802          continue;
4803          }
4804    
4805        if (STRNCMP_UC_C8(ptr+1, STRING_WEIRD_ENDWORD, 6) == 0)
4806          {
4807          nestptr = ptr + 7;
4808          ptr = sub_end_of_word - 1;
4809          continue;
4810          }
4811    
4812        /* Handle a real character class. */
4813    
4814      previous = code;      previous = code;
4815    
4816      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if      /* PCRE supports POSIX class stuff inside a class. Perl gives an error if
# Line 4511  for (;; ptr++) Line 4867  for (;; ptr++)
4867    
4868      should_flip_negation = FALSE;      should_flip_negation = FALSE;
4869    
4870        /* Extended class (xclass) will be used when characters > 255
4871        might match. */
4872    
4873    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4874        xclass = FALSE;
4875        class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */
4876        class_uchardata_base = class_uchardata;   /* Save the start */
4877    #endif
4878    
4879      /* For optimization purposes, we track some properties of the class:      /* For optimization purposes, we track some properties of the class:
4880      class_has_8bitchar will be non-zero if the class contains at least one <      class_has_8bitchar will be non-zero if the class contains at least one <
4881      256 character; class_one_char will be 1 if the class contains just one      256 character; class_one_char will be 1 if the class contains just one
4882      character. */      character; xclass_has_prop will be TRUE if unicode property checks
4883        are present in the class. */
4884    
4885      class_has_8bitchar = 0;      class_has_8bitchar = 0;
4886      class_one_char = 0;      class_one_char = 0;
4887    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
4888        xclass_has_prop = FALSE;
4889    #endif
4890    
4891      /* Initialize the 32-char bit map to all zeros. We build the map in a      /* Initialize the 32-char bit map to all zeros. We build the map in a
4892      temporary bit of memory, in case the class contains fewer than two      temporary bit of memory, in case the class contains fewer than two
# Line 4526  for (;; ptr++) Line 4895  for (;; ptr++)
4895    
4896      memset(classbits, 0, 32 * sizeof(pcre_uint8));      memset(classbits, 0, 32 * sizeof(pcre_uint8));
4897    
 #if defined SUPPORT_UTF || !defined COMPILE_PCRE8  
     xclass = FALSE;  
     class_uchardata = code + LINK_SIZE + 2;   /* For XCLASS items */  
     class_uchardata_base = class_uchardata;   /* Save the start */  
 #endif  
   
4898      /* Process characters until ] is reached. By writing this as a "do" it      /* Process characters until ] is reached. By writing this as a "do" it
4899      means that an initial ] is taken as a data character. At the start of the      means that an initial ] is taken as a data character. At the start of the
4900      loop, c contains the first byte of the character. */      loop, c contains the first byte of the character. */
# Line 4557  for (;; ptr++) Line 4920  for (;; ptr++)
4920        if (lengthptr != NULL && class_uchardata > class_uchardata_base)        if (lengthptr != NULL && class_uchardata > class_uchardata_base)
4921          {          {
4922          xclass = TRUE;          xclass = TRUE;
4923          *lengthptr += class_uchardata - class_uchardata_base;          *lengthptr += (int)(class_uchardata - class_uchardata_base);
4924          class_uchardata = class_uchardata_base;          class_uchardata = class_uchardata_base;
4925          }          }
4926  #endif  #endif
# Line 4619  for (;; ptr++) Line 4982  for (;; ptr++)
4982            posix_class = 0;            posix_class = 0;
4983    
4984          /* When PCRE_UCP is set, some of the POSIX classes are converted to          /* When PCRE_UCP is set, some of the POSIX classes are converted to
4985          different escape sequences that use Unicode properties. */          different escape sequences that use Unicode properties \p or \P. Others
4986            that are not available via \p or \P generate XCL_PROP/XCL_NOTPROP
4987            directly. */
4988    
4989  #ifdef SUPPORT_UCP  #ifdef SUPPORT_UCP
4990          if ((options & PCRE_UCP) != 0)          if ((options & PCRE_UCP) != 0)
4991            {            {
4992              unsigned int ptype = 0;
4993            int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);            int pc = posix_class + ((local_negate)? POSIX_SUBSIZE/2 : 0);
4994    
4995              /* The posix_substitutes table specifies which POSIX classes can be
4996              converted to \p or \P items. */
4997    
4998            if (posix_substitutes[pc] != NULL)            if (posix_substitutes[pc] != NULL)
4999              {              {
5000              nestptr = tempptr + 1;              nestptr = tempptr + 1;
5001              ptr = posix_substitutes[pc] - 1;              ptr = posix_substitutes[pc] - 1;
5002              continue;              continue;
5003              }              }
5004    
5005              /* There are three other classes that generate special property calls
5006              that are recognized only in an XCLASS. */
5007    
5008              else switch(posix_class)
5009                {
5010                case PC_GRAPH:
5011                ptype = PT_PXGRAPH;
5012                /* Fall through */
5013                case PC_PRINT:
5014                if (ptype == 0) ptype = PT_PXPRINT;
5015                /* Fall through */
5016                case PC_PUNCT:
5017                if (ptype == 0) ptype = PT_PXPUNCT;
5018                *class_uchardata++ = local_negate? XCL_NOTPROP : XCL_PROP;
5019                *class_uchardata++ = ptype;
5020                *class_uchardata++ = 0;
5021                xclass_has_prop = TRUE;
5022                ptr = tempptr + 1;
5023                continue;
5024    
5025                /* For all other POSIX classes, no special action is taken in UCP
5026                mode. Fall through to the non_UCP case. */
5027    
5028                default:
5029                break;
5030                }
5031            }            }
5032  #endif  #endif
5033          /* In the non-UCP case, we build the bit map for the POSIX class in a          /* In the non-UCP case, or when UCP makes no difference, we build the
5034          chunk of local store because we may be adding and subtracting from it,          bit map for the POSIX class in a chunk of local store because we may be
5035          and we don't want to subtract bits that may be in the main map already.          adding and subtracting from it, and we don't want to subtract bits that
5036          At the end we or the result into the bit map that is being built. */          may be in the main map already. At the end we or the result into the
5037            bit map that is being built. */
5038    
5039          posix_class *= 3;          posix_class *= 3;
5040    
# Line 4803  for (;; ptr++) Line 5201  for (;; ptr++)
5201                  XCL_PROP : XCL_NOTPROP;                  XCL_PROP : XCL_NOTPROP;
5202                *class_uchardata++ = ptype;                *class_uchardata++ = ptype;
5203                *class_uchardata++ = pdata;                *class_uchardata++ = pdata;
5204                  xclass_has_prop = TRUE;
5205                class_has_8bitchar--;                /* Undo! */                class_has_8bitchar--;                /* Undo! */
5206                continue;                continue;
5207                }                }
# Line 4888  for (;; ptr++) Line 5287  for (;; ptr++)
5287  #endif  #endif
5288          d = *ptr;  /* Not UTF-8 mode */          d = *ptr;  /* Not UTF-8 mode */
5289    
5290          /* The second part of a range can be a single-character escape, but          /* The second part of a range can be a single-character escape
5291          not any of the other escapes. Perl 5.6 treats a hyphen as a literal          sequence, but not any of the other escapes. Perl treats a hyphen as a
5292          in such circumstances. */          literal in such circumstances. However, in Perl's warning mode, a
5293            warning is given, so PCRE now faults it as it is almost certainly a
5294            mistake on the user's part. */
5295    
5296          if (!inescq && d == CHAR_BACKSLASH)          if (!inescq)
5297            {            {
5298            int descape;            if (d == CHAR_BACKSLASH)
5299            descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);              {
5300            if (*errorcodeptr != 0) goto FAILED;              int descape;
5301                descape = check_escape(&ptr, &d, errorcodeptr, cd->bracount, options, TRUE);
5302                if (*errorcodeptr != 0) goto FAILED;
5303    
5304            /* \b is backspace; any other special means the '-' was literal. */              /* 0 means a character was put into d; \b is backspace; any other
5305                special causes an error. */
5306    
5307            if (descape != 0)              if (descape != 0)
             {  
             if (descape == ESC_b) d = CHAR_BS; else  
5308                {                {
5309                ptr = oldptr;                if (descape == ESC_b) d = CHAR_BS; else
5310                goto CLASS_SINGLE_CHARACTER;  /* A few lines below */                  {
5311                    *errorcodeptr = ERR83;
5312                    goto FAILED;
5313                    }
5314                }                }
5315              }              }
5316    
5317              /* A hyphen followed by a POSIX class is treated in the same way. */
5318    
5319              else if (d == CHAR_LEFT_SQUARE_BRACKET &&
5320                       (ptr[1] == CHAR_COLON || ptr[1] == CHAR_DOT ||
5321                        ptr[1] == CHAR_EQUALS_SIGN) &&
5322                       check_posix_syntax(ptr, &tempptr))
5323                {
5324                *errorcodeptr = ERR83;
5325                goto FAILED;
5326                }
5327            }            }
5328    
5329          /* Check that the two values are in the correct order. Optimize          /* Check that the two values are in the correct order. Optimize
# Line 4954  for (;; ptr++) Line 5370  for (;; ptr++)
5370        whatever repeat count may follow. In the case of reqchar, save the        whatever repeat count may follow. In the case of reqchar, save the
5371        previous value for reinstating. */        previous value for reinstating. */
5372    
5373        if (class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)        if (!inescq && class_one_char == 1 && ptr[1] == CHAR_RIGHT_SQUARE_BRACKET)
5374          {          {
5375          ptr++;          ptr++;
5376          zeroreqchar = reqchar;          zeroreqchar = reqchar;
# Line 5080  for (;; ptr++) Line 5496  for (;; ptr++)
5496        *code++ = OP_XCLASS;        *code++ = OP_XCLASS;
5497        code += LINK_SIZE;        code += LINK_SIZE;
5498        *code = negate_class? XCL_NOT:0;        *code = negate_class? XCL_NOT:0;
5499          if (xclass_has_prop) *code |= XCL_HASPROP;
5500    
5501        /* If the map is required, move up the extra data to make room for it;        /* If the map is required, move up the extra data to make room for it;
5502        otherwise just move the code pointer to the end of the extra data. */        otherwise just move the code pointer to the end of the extra data. */
# Line 5089  for (;; ptr++) Line 5506  for (;; ptr++)
5506          *code++ |= XCL_MAP;          *code++ |= XCL_MAP;
5507          memmove(code + (32 / sizeof(pcre_uchar)), code,          memmove(code + (32 / sizeof(pcre_uchar)), code,
5508            IN_UCHARS(class_uchardata - code));            IN_UCHARS(class_uchardata - code));
5509            if (negate_class && !xclass_has_prop)
5510              for (c = 0; c < 32; c++) classbits[c] = ~classbits[c];
5511          memcpy(code, classbits, 32);          memcpy(code, classbits, 32);
5512          code = class_uchardata + (32 / sizeof(pcre_uchar));          code = class_uchardata + (32 / sizeof(pcre_uchar));
5513          }          }
# Line 5101  for (;; ptr++) Line 5520  for (;; ptr++)
5520        }        }
5521  #endif  #endif
5522    
5523        /* Even though any XCLASS list is now discarded, we must allow for
5524        its memory. */
5525    
5526        if (lengthptr != NULL)
5527          *lengthptr += (int)(class_uchardata - class_uchardata_base);
5528    
5529      /* If there are no characters > 255, or they are all to be included or      /* If there are no characters > 255, or they are all to be included or
5530      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the      excluded, set the opcode to OP_CLASS or OP_NCLASS, depending on whether the
5531      whole class was negated and whether there were negative specials such as \S      whole class was negated and whether there were negative specials such as \S
# Line 5171  for (;; ptr++) Line 5596  for (;; ptr++)
5596    
5597      tempcode = previous;      tempcode = previous;
5598    
5599        /* Before checking for a possessive quantifier, we must skip over
5600        whitespace and comments in extended mode because Perl allows white space at
5601        this point. */
5602    
5603        if ((options & PCRE_EXTENDED) != 0)
5604          {
5605          const pcre_uchar *p = ptr + 1;
5606          for (;;)
5607            {
5608            while (MAX_255(*p) && (cd->ctypes[*p] & ctype_space) != 0) p++;
5609            if (*p != CHAR_NUMBER_SIGN) break;
5610            p++;
5611            while (*p != CHAR_NULL)
5612              {
5613              if (IS_NEWLINE(p))         /* For non-fixed-length newline cases, */
5614                {                        /* IS_NEWLINE sets cd->nllen. */
5615                p += cd->nllen;
5616                break;
5617                }
5618              p++;
5619    #ifdef SUPPORT_UTF
5620              if (utf) FORWARDCHAR(p);
5621    #endif
5622              }           /* Loop for comment characters */
5623            }             /* Loop for multiple comments */
5624          ptr = p - 1;    /* Character before the next significant one. */
5625          }
5626    
5627      /* If the next character is '+', we have a possessive quantifier. This      /* If the next character is '+', we have a possessive quantifier. This
5628      implies greediness, whatever the setting of the PCRE_UNGREEDY option.      implies greediness, whatever the setting of the PCRE_UNGREEDY option.
5629      If the next character is '?' this is a minimizing repeat, by default,      If the next character is '?' this is a minimizing repeat, by default,
# Line 5463  for (;; ptr++) Line 5916  for (;; ptr++)
5916      opcodes such as BRA and CBRA, as this is the place where they get converted      opcodes such as BRA and CBRA, as this is the place where they get converted
5917      into the more special varieties such as BRAPOS and SBRA. A test for >=      into the more special varieties such as BRAPOS and SBRA. A test for >=
5918      OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,      OP_ASSERT and <= OP_COND includes ASSERT, ASSERT_NOT, ASSERTBACK,
5919      ASSERTBACK_NOT, ONCE, BRA, CBRA, and COND. Originally, PCRE did not allow      ASSERTBACK_NOT, ONCE, ONCE_NC, BRA, BRAPOS, CBRA, CBRAPOS, and COND.
5920      repetition of assertions, but now it does, for Perl compatibility. */      Originally, PCRE did not allow repetition of assertions, but now it does,
5921        for Perl compatibility. */
5922    
5923      else if (*previous >= OP_ASSERT && *previous <= OP_COND)      else if (*previous >= OP_ASSERT && *previous <= OP_COND)
5924        {        {
# Line 5482  for (;; ptr++) Line 5936  for (;; ptr++)
5936        /* There is no sense in actually repeating assertions. The only potential        /* There is no sense in actually repeating assertions. The only potential
5937        use of repetition is in cases when the assertion is optional. Therefore,        use of repetition is in cases when the assertion is optional. Therefore,
5938        if the minimum is greater than zero, just ignore the repeat. If the        if the minimum is greater than zero, just ignore the repeat. If the
5939        maximum is not not zero or one, set it to 1. */        maximum is not zero or one, set it to 1. */
5940    
5941        if (*previous < OP_ONCE)    /* Assertion */        if (*previous < OP_ONCE)    /* Assertion */
5942          {          {
# Line 5524  for (;; ptr++) Line 5978  for (;; ptr++)
5978          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */          if (repeat_max <= 1)    /* Covers 0, 1, and unlimited */
5979            {            {
5980            *code = OP_END;            *code = OP_END;
5981            adjust_recurse(previous, 1, utf, cd, save_hwm);            adjust_recurse(previous, 1, utf, cd, save_hwm_offset);
5982            memmove(previous + 1, previous, IN_UCHARS(len));            memmove(previous + 1, previous, IN_UCHARS(len));
5983            code++;            code++;
5984            if (repeat_max == 0)            if (repeat_max == 0)
# Line 5548  for (;; ptr++) Line 6002  for (;; ptr++)
6002            {            {
6003            int offset;            int offset;
6004            *code = OP_END;            *code = OP_END;
6005            adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm);            adjust_recurse(previous, 2 + LINK_SIZE, utf, cd, save_hwm_offset);
6006            memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));            memmove(previous + 2 + LINK_SIZE, previous, IN_UCHARS(len));
6007            code += 2 + LINK_SIZE;            code += 2 + LINK_SIZE;
6008            *previous++ = OP_BRAZERO + repeat_type;            *previous++ = OP_BRAZERO + repeat_type;
# Line 5611  for (;; ptr++) Line 6065  for (;; ptr++)
6065              for (i = 1; i < repeat_min; i++)              for (i = 1; i < repeat_min; i++)
6066                {                {
6067                pcre_uchar *hc;                pcre_uchar *hc;
6068                pcre_uchar *this_hwm = cd->hwm;                size_t this_hwm_offset = cd->hwm - cd->start_workspace;
6069                memcpy(code, previous, IN_UCHARS(len));                memcpy(code, previous, IN_UCHARS(len));
6070    
6071                while (cd->hwm > cd->start_workspace + cd->workspace_size -                while (cd->hwm > cd->start_workspace + cd->workspace_size -
6072                       WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))                       WORK_SIZE_SAFETY_MARGIN -
6073                         (this_hwm_offset - save_hwm_offset))
6074                  {                  {
                 int save_offset = save_hwm - cd->start_workspace;  
                 int this_offset = this_hwm - cd->start_workspace;  
6075                  *errorcodeptr = expand_workspace(cd);                  *errorcodeptr = expand_workspace(cd);
6076                  if (*errorcodeptr != 0) goto FAILED;                  if (*errorcodeptr != 0) goto FAILED;
                 save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;  
                 this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;  
6077                  }                  }
6078    
6079                for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)                for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset;
6080                       hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
6081                       hc += LINK_SIZE)
6082                  {                  {
6083                  PUT(cd->hwm, 0, GET(hc, 0) + len);                  PUT(cd->hwm, 0, GET(hc, 0) + len);
6084                  cd->hwm += LINK_SIZE;                  cd->hwm += LINK_SIZE;
6085                  }                  }
6086                save_hwm = this_hwm;                save_hwm_offset = this_hwm_offset;
6087                code += len;                code += len;
6088                }                }
6089              }              }
# Line 5675  for (;; ptr++) Line 6128  for (;; ptr++)
6128          else for (i = repeat_max - 1; i >= 0; i--)          else for (i = repeat_max - 1; i >= 0; i--)
6129            {            {
6130            pcre_uchar *hc;            pcre_uchar *hc;
6131            pcre_uchar *this_hwm = cd->hwm;            size_t this_hwm_offset = cd->hwm - cd->start_workspace;
6132    
6133            *code++ = OP_BRAZERO + repeat_type;            *code++ = OP_BRAZERO + repeat_type;
6134    
# Line 5697  for (;; ptr++) Line 6150  for (;; ptr++)
6150            copying them. */            copying them. */
6151    
6152            while (cd->hwm > cd->start_workspace + cd->workspace_size -            while (cd->hwm > cd->start_workspace + cd->workspace_size -
6153                   WORK_SIZE_SAFETY_MARGIN - (this_hwm - save_hwm))                   WORK_SIZE_SAFETY_MARGIN -
6154                     (this_hwm_offset - save_hwm_offset))
6155              {              {
             int save_offset = save_hwm - cd->start_workspace;  
             int this_offset = this_hwm - cd->start_workspace;  
6156              *errorcodeptr = expand_workspace(cd);              *errorcodeptr = expand_workspace(cd);
6157              if (*errorcodeptr != 0) goto FAILED;              if (*errorcodeptr != 0) goto FAILED;
             save_hwm = (pcre_uchar *)cd->start_workspace + save_offset;  
             this_hwm = (pcre_uchar *)cd->start_workspace + this_offset;  
6158              }              }
6159    
6160            for (hc = save_hwm; hc < this_hwm; hc += LINK_SIZE)            for (hc = (pcre_uchar *)cd->start_workspace + save_hwm_offset;
6161                   hc < (pcre_uchar *)cd->start_workspace + this_hwm_offset;
6162                   hc += LINK_SIZE)
6163              {              {
6164              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));              PUT(cd->hwm, 0, GET(hc, 0) + len + ((i != 0)? 2+LINK_SIZE : 1));
6165              cd->hwm += LINK_SIZE;              cd->hwm += LINK_SIZE;
6166              }              }
6167            save_hwm = this_hwm;            save_hwm_offset = this_hwm_offset;
6168            code += len;            code += len;
6169            }            }
6170    
# Line 5808  for (;; ptr++) Line 6260  for (;; ptr++)
6260                {                {
6261                int nlen = (int)(code - bracode);                int nlen = (int)(code - bracode);
6262                *code = OP_END;                *code = OP_END;
6263                adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm);                adjust_recurse(bracode, 1 + LINK_SIZE, utf, cd, save_hwm_offset);
6264                memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));                memmove(bracode + 1 + LINK_SIZE, bracode, IN_UCHARS(nlen));
6265                code += 1 + LINK_SIZE;                code += 1 + LINK_SIZE;
6266                nlen += 1 + LINK_SIZE;                nlen += 1 + LINK_SIZE;
# Line 5855  for (;; ptr++) Line 6307  for (;; ptr++)
6307        goto FAILED;        goto FAILED;
6308        }        }
6309    
6310      /* If the character following a repeat is '+', or if certain optimization      /* If the character following a repeat is '+', possessive_quantifier is
6311      tests above succeeded, possessive_quantifier is TRUE. For some opcodes,      TRUE. For some opcodes, there are special alternative opcodes for this
6312      there are special alternative opcodes for this case. For anything else, we      case. For anything else, we wrap the entire repeated item inside OP_ONCE
6313      wrap the entire repeated item inside OP_ONCE brackets. Logically, the '+'      brackets. Logically, the '+' notation is just syntactic sugar, taken from
6314      notation is just syntactic sugar, taken from Sun's Java package, but the      Sun's Java package, but the special opcodes can optimize it.
     special opcodes can optimize it.  
6315    
6316      Some (but not all) possessively repeated subpatterns have already been      Some (but not all) possessively repeated subpatterns have already been
6317      completely handled in the code just above. For them, possessive_quantifier      completely handled in the code just above. For them, possessive_quantifier
6318      is always FALSE at this stage.      is always FALSE at this stage. Note that the repeated item starts at
6319        tempcode, not at previous, which might be the first part of a string whose
6320      Note that the repeated item starts at tempcode, not at previous, which      (former) last char we repeated. */
     might be the first part of a string whose (former) last char we repeated.  
   
     Possessifying an 'exact' quantifier has no effect, so we can ignore it. But  
     an 'upto' may follow. We skip over an 'exact' item, and then test the  
     length of what remains before proceeding. */  
6321    
6322      if (possessive_quantifier)      if (possessive_quantifier)
6323        {        {
6324        int len;        int len;
6325    
6326        if (*tempcode == OP_TYPEEXACT)        /* Possessifying an EXACT quantifier has no effect, so we can ignore it.
6327          However, QUERY, STAR, or UPTO may follow (for quantifiers such as {5,6},
6328          {5,}, or {5,10}). We skip over an EXACT item; if the length of what
6329          remains is greater than zero, there's a further opcode that can be
6330          handled. If not, do nothing, leaving the EXACT alone. */
6331    
6332          switch(*tempcode)
6333            {
6334            case OP_TYPEEXACT:
6335          tempcode += PRIV(OP_lengths)[*tempcode] +          tempcode += PRIV(OP_lengths)[*tempcode] +
6336            ((tempcode[1 + IMM2_SIZE] == OP_PROP            ((tempcode[1 + IMM2_SIZE] == OP_PROP
6337            || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);            || tempcode[1 + IMM2_SIZE] == OP_NOTPROP)? 2 : 0);
6338            break;
6339    
6340        else if (*tempcode == OP_EXACT || *tempcode == OP_NOTEXACT)          /* CHAR opcodes are used for exacts whose count is 1. */
6341          {  
6342            case OP_CHAR:
6343            case OP_CHARI:
6344            case OP_NOT:
6345            case OP_NOTI:
6346            case OP_EXACT:
6347            case OP_EXACTI:
6348            case OP_NOTEXACT:
6349            case OP_NOTEXACTI:
6350          tempcode += PRIV(OP_lengths)[*tempcode];          tempcode += PRIV(OP_lengths)[*tempcode];
6351  #ifdef SUPPORT_UTF  #ifdef SUPPORT_UTF
6352          if (utf && HAS_EXTRALEN(tempcode[-1]))          if (utf && HAS_EXTRALEN(tempcode[-1]))
6353            tempcode += GET_EXTRALEN(tempcode[-1]);            tempcode += GET_EXTRALEN(tempcode[-1]);
6354  #endif  #endif
6355            break;
6356    
6357            /* For the class opcodes, the repeat operator appears at the end;
6358            adjust tempcode to point to it. */
6359    
6360            case OP_CLASS:
6361            case OP_NCLASS:
6362            tempcode += 1 + 32/sizeof(pcre_uchar);
6363            break;
6364    
6365    #if defined SUPPORT_UTF || !defined COMPILE_PCRE8
6366            case OP_XCLASS:
6367            tempcode += GET(tempcode, 1);
6368            break;
6369    #endif
6370          }          }
6371    
6372          /* If tempcode is equal to code (which points to the end of the repeated
6373          item), it means we have skipped an EXACT item but there is no following
6374          QUERY, STAR, or UPTO; the value of len will be 0, and we do nothing. In
6375          all other cases, tempcode will be pointing to the repeat opcode, and will
6376          be less than code, so the value of len will be greater than 0. */
6377    
6378        len = (int)(code - tempcode);        len = (int)(code - tempcode);
6379          if (len > 0)
6380            {
6381            unsigned int repcode = *tempcode;
6382    
6383            /* There is a table for possessifying opcodes, all of which are less
6384            than OP_CALLOUT. A zero entry means there is no possessified version.
6385            */
6386    
6387            if (repcode < OP_CALLOUT && opcode_possessify[repcode] > 0)
6388              *tempcode = opcode_possessify[repcode];
6389    
6390            /* For opcode without a special possessified version, wrap the item in
6391            ONCE brackets. Because we are moving code along, we must ensure that any
6392            pending recursive references are updated. */
6393    
6394            else
6395              {
6396              *code = OP_END;
6397              adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm_offset);
6398              memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
6399              code += 1 + LINK_SIZE;
6400              len += 1 + LINK_SIZE;
6401              tempcode[0] = OP_ONCE;
6402              *code++ = OP_KET;
6403              PUTINC(code, 0, len);
6404              PUT(tempcode, 1, len);
6405              }
6406            }
6407    
6408    #ifdef NEVER
6409        if (len > 0) switch (*tempcode)        if (len > 0) switch (*tempcode)
6410          {          {
6411          case OP_STAR:  *tempcode = OP_POSSTAR; break;          case OP_STAR:  *tempcode = OP_POSSTAR; break;
# Line 5919  for (;; ptr++) Line 6433  for (;; ptr++)
6433          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;          case OP_TYPEQUERY: *tempcode = OP_TYPEPOSQUERY; break;
6434          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;          case OP_TYPEUPTO:  *tempcode = OP_TYPEPOSUPTO; break;
6435    
6436            case OP_CRSTAR:   *tempcode = OP_CRPOSSTAR; break;
6437            case OP_CRPLUS:   *tempcode = OP_CRPOSPLUS; break;
6438            case OP_CRQUERY:  *tempcode = OP_CRPOSQUERY; break;
6439            case OP_CRRANGE:  *tempcode = OP_CRPOSRANGE; break;
6440    
6441          /* Because we are moving code along, we must ensure that any          /* Because we are moving code along, we must ensure that any
6442          pending recursive references are updated. */          pending recursive references are updated. */
6443    
6444          default:          default:
6445          *code = OP_END;          *code = OP_END;
6446          adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm);          adjust_recurse(tempcode, 1 + LINK_SIZE, utf, cd, save_hwm_offset);
6447          memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));          memmove(tempcode + 1 + LINK_SIZE, tempcode, IN_UCHARS(len));
6448          code += 1 + LINK_SIZE;          code += 1 + LINK_SIZE;
6449          len += 1 + LINK_SIZE;          len += 1 + LINK_SIZE;
# Line 5934  for (;; ptr++) Line 6453  for (;; ptr++)
6453          PUT(tempcode, 1, len);          PUT(tempcode, 1, len);
6454          break;          break;
6455          }          }
6456    #endif
6457        }        }
6458    
6459      /* In all case we no longer have a previous item. We also set the      /* In all case we no longer have a previous item. We also set the
# Line 5952  for (;; ptr++) Line 6472  for (;; ptr++)
6472      parenthesis forms.  */      parenthesis forms.  */
6473    
6474      case CHAR_LEFT_PARENTHESIS:      case CHAR_LEFT_PARENTHESIS:
6475      newoptions = options;      ptr++;
     skipbytes = 0;  
     bravalue = OP_CBRA;  
     save_hwm = cd->hwm;  
     reset_bracount = FALSE;  
6476    
6477      /* First deal with various "verbs" that can be introduced by '*'. */      /* First deal with comments. Putting this code right at the start ensures
6478        that comments have no bad side effects. */
6479    
6480        if (ptr[0] == CHAR_QUESTION_MARK && ptr[1] == CHAR_NUMBER_SIGN)
6481          {
6482          ptr += 2;
6483          while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;
6484          if (*ptr == CHAR_NULL)
6485            {
6486            *errorcodeptr = ERR18;
6487            goto FAILED;
6488            }
6489          continue;
6490          }
6491    
6492        /* Now deal with various "verbs" that can be introduced by '*'. */
6493    
     ptr++;  
6494      if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'      if (ptr[0] == CHAR_ASTERISK && (ptr[1] == ':'
6495           || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))           || (MAX_255(ptr[1]) && ((cd->ctypes[ptr[1]] & ctype_letter) != 0))))
6496        {        {
# Line 6081  for (;; ptr++) Line 6611  for (;; ptr++)
6611        goto FAILED;        goto FAILED;
6612        }        }
6613    
6614        /* Initialize for "real" parentheses */
6615    
6616        newoptions = options;
6617        skipbytes = 0;
6618        bravalue = OP_CBRA;
6619        save_hwm_offset = cd->hwm - cd->start_workspace;
6620        reset_bracount = FALSE;
6621    
6622      /* Deal with the extended parentheses; all are introduced by '?', and the      /* Deal with the extended parentheses; all are introduced by '?', and the
6623      appearance of any of them means that this is not a capturing group. */      appearance of any of them means that this is not a capturing group. */
6624    
6625      else if (*ptr == CHAR_QUESTION_MARK)      if (*ptr == CHAR_QUESTION_MARK)
6626        {        {
6627        int i, set, unset, namelen;        int i, set, unset, namelen;
6628        int *optset;        int *optset;
# Line 6093  for (;; ptr++) Line 6631  for (;; ptr++)
6631    
6632        switch (*(++ptr))        switch (*(++ptr))
6633          {          {
         case CHAR_NUMBER_SIGN:                 /* Comment; skip to ket */  
         ptr++;  
         while (*ptr != CHAR_NULL && *ptr != CHAR_RIGHT_PARENTHESIS) ptr++;  
         if (*ptr == CHAR_NULL)  
           {  
           *errorcodeptr = ERR18;  
           goto FAILED;  
           }  
         continue;  
   
   
6634          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6635          case CHAR_VERTICAL_LINE:  /* Reset capture count for each branch */          case CHAR_VERTICAL_LINE:  /* Reset capture count for each branch */
6636          reset_bracount = TRUE;          reset_bracount = TRUE;
# Line 6122  for (;; ptr++) Line 6649  for (;; ptr++)
6649          tempptr = ptr;          tempptr = ptr;
6650    
6651          /* A condition can be an assertion, a number (referring to a numbered          /* A condition can be an assertion, a number (referring to a numbered
6652          group), a name (referring to a named group), or 'R', referring to          group's having been set), a name (referring to a named group), or 'R',
6653          recursion. R<digits> and R&name are also permitted for recursion tests.          referring to recursion. R<digits> and R&name are also permitted for
6654            recursion tests.
6655          There are several syntaxes for testing a named group: (?(name)) is used  
6656          by Python; Perl 5.10 onwards uses (?(<name>) or (?('name')).          There are ways of testing a named group: (?(name)) is used by Python;
6657            Perl 5.10 onwards uses (?(<name>) or (?('name')).
6658          There are two unfortunate ambiguities, caused by history. (a) 'R' can  
6659          be the recursive thing or the name 'R' (and similarly for 'R' followed          There is one unfortunate ambiguity, caused by history. 'R' can be the
6660          by digits), and (b) a number could be a name that consists of digits.          recursive thing or the name 'R' (and similarly for 'R' followed by
6661          In both cases, we look for a name first; if not found, we try the other          digits). We look for a name first; if not found, we try the other case.
         cases.  
6662    
6663          For compatibility with auto-callouts, we allow a callout to be          For compatibility with auto-callouts, we allow a callout to be
6664          specified before a condition that is an assertion. First, check for the          specified before a condition that is an assertion. First, check for the
# Line 6154  for (;; ptr++) Line 6680  for (;; ptr++)
6680                (tempptr[2] == CHAR_EQUALS_SIGN ||                (tempptr[2] == CHAR_EQUALS_SIGN ||
6681                 tempptr[2] == CHAR_EXCLAMATION_MARK ||                 tempptr[2] == CHAR_EXCLAMATION_MARK ||
6682                 tempptr[2] == CHAR_LESS_THAN_SIGN))                 tempptr[2] == CHAR_LESS_THAN_SIGN))
6683              {
6684              cd->iscondassert = TRUE;
6685            break;            break;
6686              }
6687    
6688          /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all          /* Other conditions use OP_CREF/OP_DNCREF/OP_RREF/OP_DNRREF, and all
6689          need to skip at least 1+IMM2_SIZE bytes at the start of the group. */          need to skip at least 1+IMM2_SIZE bytes at the start of the group. */
6690    
6691          code[1+LINK_SIZE] = OP_CREF;          code[1+LINK_SIZE] = OP_CREF;
6692          skipbytes = 1+IMM2_SIZE;          skipbytes = 1+IMM2_SIZE;
6693          refsign = -1;          refsign = -1;     /* => not a number */
6694            namelen = -1;     /* => not a name; must set to avoid warning */
6695            name = NULL;      /* Always set to avoid warning */
6696            recno = 0;        /* Always set to avoid warning */
6697    
6698          /* Check for a test for recursion in a named group. */          /* Check for a test for recursion in a named group. */
6699    
6700          if (ptr[1] == CHAR_R && ptr[2] == CHAR_AMPERSAND)          ptr++;
6701            if (*ptr == CHAR_R && ptr[1] == CHAR_AMPERSAND)
6702            {            {
6703            terminator = -1;            terminator = -1;
6704            ptr += 2;            ptr += 2;
# Line 6174  for (;; ptr++) Line 6707  for (;; ptr++)
6707    
6708          /* Check for a test for a named group's having been set, using the Perl          /* Check for a test for a named group's having been set, using the Perl
6709          syntax (?(<name>) or (?('name'), and also allow for the original PCRE          syntax (?(<name>) or (?('name'), and also allow for the original PCRE
6710          syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). As names may          syntax of (?(name) or for (?(+n), (?(-n), and just (?(n). */
         consist entirely of digits, there is scope for ambiguity. */  
6711    
6712          else if (ptr[1] == CHAR_LESS_THAN_SIGN)          else if (*ptr == CHAR_LESS_THAN_SIGN)
6713            {            {
6714            terminator = CHAR_GREATER_THAN_SIGN;            terminator = CHAR_GREATER_THAN_SIGN;
6715            ptr++;            ptr++;
6716            }            }
6717          else if (ptr[1] == CHAR_APOSTROPHE)          else if (*ptr == CHAR_APOSTROPHE)
6718            {            {
6719            terminator = CHAR_APOSTROPHE;            terminator = CHAR_APOSTROPHE;
6720            ptr++;            ptr++;
# Line 6190  for (;; ptr++) Line 6722  for (;; ptr++)
6722          else          else
6723            {            {
6724            terminator = CHAR_NULL;            terminator = CHAR_NULL;
6725            if (ptr[1] == CHAR_MINUS || ptr[1] == CHAR_PLUS) refsign = *(++ptr);            if (*ptr == CHAR_MINUS || *ptr == CHAR_PLUS) refsign = *ptr++;
6726                else if (IS_DIGIT(*ptr)) refsign = 0;
6727            }            }
6728    
6729          /* When a name is one of a number of duplicates, a different opcode is          /* Handle a number */
         used and it needs more memory. Unfortunately we cannot tell whether a  
         name is a duplicate in the first pass, so we have to allow for more  
         memory except when we know it is a relative numerical reference. */  
   
         if (refsign < 0 && lengthptr != NULL) *lengthptr += IMM2_SIZE;  
6730    
6731          /* We now expect to read a name (possibly all digits); any thing else          if (refsign >= 0)
         is an error. In the case of all digits, also get it as a number. */  
   
         if (!MAX_255(ptr[1]) || (cd->ctypes[ptr[1]] & ctype_word) == 0)  
6732            {            {
6733            ptr += 1;  /* To get the right offset */            while (IS_DIGIT(*ptr))
6734            *errorcodeptr = ERR28;              {
6735            goto FAILED;              recno = recno * 10 + (int)(*ptr - CHAR_0);
6736                ptr++;
6737                }
6738            }            }
6739    
6740          recno = 0;          /* Otherwise we expect to read a name; anything else is an error. When
6741          name = ++ptr;          a name is one of a number of duplicates, a different opcode is used and
6742          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)          it needs more memory. Unfortunately we cannot tell whether a name is a
6743            duplicate in the first pass, so we have to allow for more memory. */
6744    
6745            else
6746            {            {
6747            if (recno >= 0)            if (IS_DIGIT(*ptr))
6748              recno = (IS_DIGIT(*ptr))? recno * 10 + (int)(*ptr - CHAR_0) : -1;              {
6749            ptr++;              *errorcodeptr = ERR84;
6750                goto FAILED;
6751                }
6752              if (!MAX_255(*ptr) || (cd->ctypes[*ptr] & ctype_word) == 0)
6753                {
6754                *errorcodeptr = ERR28;   /* Assertion expected */
6755                goto FAILED;
6756                }
6757              name = ptr++;
6758              while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0)
6759                {
6760                ptr++;
6761                }
6762              namelen = (int)(ptr - name);
6763              if (lengthptr != NULL) *lengthptr += IMM2_SIZE;
6764            }            }
         namelen = (int)(ptr - name);  
6765    
6766          /* Check the terminator */          /* Check the terminator */
6767    
6768          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||          if ((terminator > 0 && *ptr++ != (pcre_uchar)terminator) ||
6769              *ptr++ != CHAR_RIGHT_PARENTHESIS)              *ptr++ != CHAR_RIGHT_PARENTHESIS)
6770            {            {
6771            ptr--;      /* Error offset */            ptr--;                  /* Error offset */
6772            *errorcodeptr = ERR26;            *errorcodeptr = ERR26;  /* Malformed number or name */
6773            goto FAILED;            goto FAILED;
6774            }            }
6775    
# Line 6235  for (;; ptr++) Line 6778  for (;; ptr++)
6778          if (lengthptr != NULL) break;          if (lengthptr != NULL) break;
6779    
6780          /* In the real compile we do the work of looking for the actual          /* In the real compile we do the work of looking for the actual
6781          reference. If the string started with "+" or "-" we require the rest to          reference. If refsign is not negative, it means we have a number in
6782          be digits, in which case recno will be set. */          recno. */
6783    
6784          if (refsign > 0)          if (refsign >= 0)
6785            {            {
6786            if (recno <= 0)            if (recno <= 0)
6787              {              {
6788              *errorcodeptr = ERR58;              *errorcodeptr = ERR35;
6789              goto FAILED;              goto FAILED;
6790              }              }
6791            recno = (refsign == CHAR_MINUS)?            if (refsign != 0) recno = (refsign == CHAR_MINUS)?
6792              cd->bracount - recno + 1 : recno +cd->bracount;              cd->bracount - recno + 1 : recno + cd->bracount;
6793            if (recno <= 0 || recno > cd->final_bracount)            if (recno <= 0 || recno > cd->final_bracount)
6794              {              {
6795              *errorcodeptr = ERR15;              *errorcodeptr = ERR15;
6796              goto FAILED;              goto FAILED;
6797              }              }
6798            PUT2(code, 2+LINK_SIZE, recno);            PUT2(code, 2+LINK_SIZE, recno);
6799              if (recno > cd->top_backref) cd->top_backref = recno;
6800            break;            break;
6801            }            }
6802    
6803          /* Otherwise (did not start with "+" or "-"), start by looking for the          /* Otherwise look for the name. */
         name. */  
6804    
6805          slot = cd->name_table;          slot = cd->name_table;
6806          for (i = 0; i < cd->names_found; i++)          for (i = 0; i < cd->names_found; i++)
# Line 6276  for (;; ptr++) Line 6819  for (;; ptr++)
6819            int offset = i++;            int offset = i++;
6820            int count = 1;            int count = 1;
6821            recno = GET2(slot, 0);   /* Number from first found */            recno = GET2(slot, 0);   /* Number from first found */
6822              if (recno > cd->top_backref) cd->top_backref = recno;
6823            for (; i < cd->names_found; i++)            for (; i < cd->names_found; i++)
6824              {              {
6825              slot += cd->name_entry_size;              slot += cd->name_entry_size;
6826              if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0) break;              if (STRNCMP_UC_UC(name, slot+IMM2_SIZE, namelen) != 0 ||
6827                  (slot+IMM2_SIZE)[namelen] != 0) break;
6828              count++;              count++;
6829              }              }
6830    
6831            if (count > 1)            if (count > 1)
6832              {              {
6833              PUT2(code, 2+LINK_SIZE, offset);              PUT2(code, 2+LINK_SIZE, offset);
# Line 6298  for (;; ptr++) Line 6844  for (;; ptr++)
6844          /* If terminator == CHAR_NULL it means that the name followed directly          /* If terminator == CHAR_NULL it means that the name followed directly
6845          after the opening parenthesis [e.g. (?(abc)...] and in this case there          after the opening parenthesis [e.g. (?(abc)...] and in this case there
6846          are some further alternatives to try. For the cases where terminator !=          are some further alternatives to try. For the cases where terminator !=
6847          0 [things like (?(<name>... or (?('name')... or (?(R&name)... ] we have          CHAR_NULL [things like (?(<name>... or (?('name')... or (?(R&name)... ]
6848          now checked all the possibilities, so give an error. */          we have now checked all the possibilities, so give an error. */
6849    
6850          else if (terminator != CHAR_NULL)          else if (terminator != CHAR_NULL)
6851            {            {
# Line 6336  for (;; ptr++) Line 6882  for (;; ptr++)
6882            skipbytes = 1;            skipbytes = 1;
6883            }            }
6884    
6885          /* Check for the "name" actually being a subpattern number. We are          /* Reference to an unidentified subpattern. */
         in the second pass here, so final_bracount is set. */  
   
         else if (recno > 0 && recno <= cd->final_bracount)  
           {  
           PUT2(code, 2+LINK_SIZE, recno);  
           }  
   
         /* Either an unidentified subpattern, or a reference to (?(0) */  
6886    
6887          else          else
6888            {            {
6889            *errorcodeptr = (recno == 0)? ERR35: ERR15;            *errorcodeptr = ERR15;
6890            goto FAILED;            goto FAILED;
6891            }            }
6892          break;          break;
# Line 6361  for (;; ptr++) Line 6899  for (;; ptr++)
6899          ptr++;          ptr++;
6900          break;          break;
6901    
6902            /* Optimize (?!) to (*FAIL) unless it is quantified - which is a weird
6903            thing to do, but Perl allows all assertions to be quantified, and when
6904            they contain capturing parentheses there may be a potential use for
6905            this feature. Not that that applies to a quantified (?!) but we allow
6906            it for uniformity. */
6907    
6908          /* ------------------------------------------------------------ */          /* ------------------------------------------------------------ */
6909          case CHAR_EXCLAMATION_MARK:            /* Negative lookahead */          case CHAR_EXCLAMATION_MARK:            /* Negative lookahead */
6910          ptr++;          ptr++;
6911          if (*ptr == CHAR_RIGHT_PARENTHESIS)    /* Optimize (?!) */          if (*ptr == CHAR_RIGHT_PARENTHESIS && ptr[1] != CHAR_ASTERISK &&
6912                 ptr[1] != CHAR_PLUS && ptr[1] != CHAR_QUESTION_MARK &&
6913                (ptr[1] != CHAR_LEFT_CURLY_BRACKET || !is_counted_repeat(ptr+2)))
6914            {            {
6915            *code++ = OP_FAIL;            *code++ = OP_FAIL;
6916            previous = NULL;            previous = NULL;
# Line 6461  for (;; ptr++) Line 7006  for (;; ptr++)
7006          terminator = (*ptr == CHAR_LESS_THAN_SIGN)?          terminator = (*ptr == CHAR_LESS_THAN_SIGN)?
7007            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
7008          name = ++ptr;          name = ++ptr;
7009            if (IS_DIGIT(*ptr))
7010              {
7011              *errorcodeptr = ERR84;   /* Group name must start with non-digit */
7012              goto FAILED;
7013              }
7014          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
7015          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
7016    
# Line 6575  for (;; ptr++) Line 7124  for (;; ptr++)
7124    
7125          NAMED_REF_OR_RECURSE:          NAMED_REF_OR_RECURSE:
7126          name = ++ptr;          name = ++ptr;
7127            if (IS_DIGIT(*ptr))
7128              {
7129              *errorcodeptr = ERR84;   /* Group name must start with non-digit */
7130              goto FAILED;
7131              }
7132          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;          while (MAX_255(*ptr) && (cd->ctypes[*ptr] & ctype_word) != 0) ptr++;
7133          namelen = (int)(ptr - name);          namelen = (int)(ptr - name);
7134    
# Line 6622  for (;; ptr++) Line 7176  for (;; ptr++)
7176            /* Count named back references. */            /* Count named back references. */
7177    
7178            if (!is_recurse) cd->namedrefcount++;            if (!is_recurse) cd->namedrefcount++;
7179    
7180              /* We have to allow for a named reference to a duplicated name (this
7181              cannot be determined until the second pass). This needs an extra
7182              16-bit data item. */
7183    
7184              *lengthptr += IMM2_SIZE;
7185            }            }
7186    
7187          /* In the real compile, search the name table. We check the name          /* In the real compile, search the name table. We check the name
# Line 6668  for (;; ptr++) Line 7228  for (;; ptr++)
7228            for (i++; i < cd->names_found; i++)            for (i++; i < cd->names_found; i++)
7229              {              {
7230              if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;              if (STRCMP_UC_UC(slot + IMM2_SIZE, cslot + IMM2_SIZE) != 0) break;
7231    
7232    
7233              count++;              count++;
7234              cslot += cd->name_entry_size;              cslot += cd->name_entry_size;
7235              }              }
# Line 6967  for (;; ptr++) Line 7529  for (;; ptr++)
7529        skipbytes = IMM2_SIZE;        skipbytes = IMM2_SIZE;
7530        }        }
7531    
7532      /* Process nested bracketed regex. Assertions used not to be repeatable,      /* Process nested bracketed regex. First check for parentheses nested too
7533      but this was changed for Perl compatibility, so all kinds can now be      deeply. */
7534      repeated. We copy code into a non-register variable (tempcode) in order to  
7535      be able to pass its address because some compilers complain otherwise. */      if ((cd->parens_depth += 1) > PARENS_NEST_LIMIT)
7536          {
7537          *errorcodeptr = ERR82;
7538          goto FAILED;
7539          }
7540    
7541        /* All assertions used not to be repeatable, but this was changed for Perl
7542        compatibility. All kinds can now be repeated except for assertions that are
7543        conditions (Perl also forbids these to be repeated). We copy code into a
7544        non-register variable (tempcode) in order to be able to pass its address
7545        because some compilers complain otherwise. At the start of a conditional
7546        group whose condition is an assertion, cd->iscondassert is set. We unset it
7547        here so as to allow assertions later in the group to be quantified. */
7548    
7549        if (bravalue >= OP_ASSERT && bravalue <= OP_ASSERTBACK_NOT &&
7550            cd->iscondassert)
7551          {
7552          previous = NULL;
7553          cd->iscondassert = FALSE;
7554          }
7555        else previous = code;
7556    
     previous = code;                      /* For handling repetition */  
7557      *code = bravalue;      *code = bravalue;
7558      tempcode = code;      tempcode = code;
7559      tempreqvary = cd->req_varyopt;        /* Save value before bracket */      tempreqvary = cd->req_varyopt;        /* Save value before bracket */
# Line 7001  for (;; ptr++) Line 7582  for (;; ptr++)
7582           ))           ))
7583        goto FAILED;        goto FAILED;
7584    
7585        cd->parens_depth -= 1;
7586    
7587      /* If this was an atomic group and there are no capturing groups within it,      /* If this was an atomic group and there are no capturing groups within it,
7588      generate OP_ONCE_NC instead of OP_ONCE. */      generate OP_ONCE_NC instead of OP_ONCE. */
7589    
# Line 7215  for (;; ptr++) Line 7798  for (;; ptr++)
7798        if (escape == ESC_g)        if (escape == ESC_g)
7799          {          {
7800          const pcre_uchar *p;          const pcre_uchar *p;
7801          save_hwm = cd->hwm;   /* Normally this is set when '(' is read */          pcre_uint32 cf;
7802    
7803            save_hwm_offset = cd->hwm - cd->start_workspace;   /* Normally this is set when '(' is read */
7804          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?          terminator = (*(++ptr) == CHAR_LESS_THAN_SIGN)?
7805            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;            CHAR_GREATER_THAN_SIGN : CHAR_APOSTROPHE;
7806    
7807          /* These two statements stop the compiler for warning about possibly          /* These two statements stop the compiler for warning about possibly
7808          unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In          unset variables caused by the jump to HANDLE_NUMERICAL_RECURSION. In
7809          fact, because we actually check for a number below, the paths that          fact, because we do the check for a number below, the paths that
7810          would actually be in error are never taken. */          would actually be in error are never taken. */
7811    
7812          skipbytes = 0;          skipbytes = 0;
7813          reset_bracount = FALSE;          reset_bracount = FALSE;
7814    
7815          /* Test for a name */          /* If it's not a signed or unsigned number, treat it as a name. */
7816    
7817          if (ptr[1] != CHAR_PLUS && ptr[1] != CHAR_MINUS)          cf = ptr[1];
7818            if (cf != CHAR_PLUS && cf != CHAR_MINUS && !IS_DIGIT(cf))
7819            {            {
           BOOL is_a_number = TRUE;  
           for (p = ptr + 1; *p != CHAR_NULL && *p != (pcre_uchar)terminator; p++)  
             {  
             if (!MAX_255(*p)) { is_a_number = FALSE; break; }  
             if ((cd->ctypes[*p] & ctype_digit) == 0) is_a_number = FALSE;  
             if ((cd->ctypes[*p] & ctype_word) == 0) break;  
             }  
           if (*p != (pcre_uchar)terminator)  
             {  
             *errorcodeptr = ERR57;  
             break;  
             }  
           if (is_a_number)  
             {  
             ptr++;  
             goto HANDLE_NUMERICAL_RECURSION;  
             }  
7820            is_recurse = TRUE;            is_recurse = TRUE;
7821            goto NAMED_REF_OR_RECURSE;            goto NAMED_REF_OR_RECURSE;
7822            }            }
7823    
7824          /* Test a signed number in angle brackets or quotes. */          /* Signed or unsigned number (cf = ptr[1]) is known to be plus or minus
7825            or a digit. */
7826    
7827          p = ptr + 2;          p = ptr + 2;
7828          while (IS_DIGIT(*p)) p++;          while (IS_DIGIT(*p)) p++;
# Line 7392  for (;; ptr++) Line 7962  for (;; ptr++)
7962    
7963      /* ===================================================================*/      /* ===================================================================*/
7964      /* Handle a literal character. It is guaranteed not to be whitespace or #      /* Handle a literal character. It is guaranteed not to be whitespace or #
7965      when the extended flag is set. If we are in UTF-8 mode, it may be a      when the extended flag is set. If we are in a UTF mode, it may be a
7966      multi-byte literal character. */      multi-unit literal character. */
7967    
7968      default:      default:
7969      NORMAL_CHAR:      NORMAL_CHAR:
# Line 7557  int length; Line 8127  int length;
8127  unsigned int orig_bracount;  unsigned int orig_bracount;
8128  unsigned int max_bracount;  unsigned int max_bracount;
8129  branch_chain bc;  branch_chain bc;
8130    size_t save_hwm_offset;
8131    
8132    /* If set, call the external function that checks for stack availability. */
8133    
8134    if (PUBL(stack_guard) != NULL && PUBL(stack_guard)())
8135      {
8136      *errorcodeptr= ERR85;
8137      return FALSE;
8138      }
8139    
8140    /* Miscellaneous initialization */
8141    
8142  bc.outer = bcptr;  bc.outer = bcptr;
8143  bc.current_branch = code;  bc.current_branch = code;
# Line 7564  bc.current_branch = code; Line 8145  bc.current_branch = code;
8145  firstchar = reqchar = 0;  firstchar = reqchar = 0;
8146  firstcharflags = reqcharflags = REQ_UNSET;  firstcharflags = reqcharflags = REQ_UNSET;
8147    
8148    save_hwm_offset = cd->hwm - cd->start_workspace;
8149    
8150  /* Accumulate the length for use in the pre-compile phase. Start with the  /* Accumulate the length for use in the pre-compile phase. Start with the
8151  length of the BRA and KET and any extra bytes that are required at the  length of the BRA and KET and any extra bytes that are required at the
8152  beginning. We accumulate in a local variable to save frequent testing of  beginning. We accumulate in a local variable to save frequent testing of
# Line 7757  for (;;) Line 8340  for (;;)
8340    
8341      /* If it was a capturing subpattern, check to see if it contained any      /* If it was a capturing subpattern, check to see if it contained any
8342      recursive back references. If so, we must wrap it in atomic brackets.      recursive back references. If so, we must wrap it in atomic brackets.
8343      In any event, remove the block from the chain. */      Because we are moving code along, we must ensure that any pending recursive
8344        references are updated. In any event, remove the block from the chain. */
8345    
8346      if (capnumber > 0)      if (capnumber > 0)
8347        {        {
8348        if (cd->open_caps->flag)        if (cd->open_caps->flag)
8349          {          {
8350            *code = OP_END;
8351            adjust_recurse(start_bracket, 1 + LINK_SIZE,
8352              (options & PCRE_UTF8) != 0, cd, save_hwm_offset);
8353          memmove(start_bracket + 1 + LINK_SIZE, start_bracket,          memmove(start_bracket + 1 + LINK_SIZE, start_bracket,
8354            IN_UCHARS(code - start_bracket));            IN_UCHARS(code - start_bracket));
8355          *start_bracket = OP_ONCE;          *start_bracket = OP_ONCE;
# Line 7986  do { Line 8573  do {
8573         case OP_RREF:         case OP_RREF:
8574         case OP_DNRREF:         case OP_DNRREF:
8575         case OP_DEF:         case OP_DEF:
8576           case OP_FAIL:
8577         return FALSE;         return FALSE;
8578    
8579         default:     /* Assertion */         default:     /* Assertion */
# Line 8381  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. * Line 8969  PCRE_UTF8 == PCRE_UTF16 == PCRE_UTF32. *
8969      { skipatstart += 6; options |= PCRE_UTF8; continue; }      { skipatstart += 6; options |= PCRE_UTF8; continue; }
8970    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_UCP_RIGHTPAR, 4) == 0)
8971      { skipatstart += 6; options |= PCRE_UCP; continue; }      { skipatstart += 6; options |= PCRE_UCP; continue; }
8972      else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_AUTO_POSSESS_RIGHTPAR, 16) == 0)
8973        { skipatstart += 18; options |= PCRE_NO_AUTO_POSSESS; continue; }
8974    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)    else if (STRNCMP_UC_C8(ptr+skipatstart+2, STRING_NO_START_OPT_RIGHTPAR, 13) == 0)
8975      { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }      { skipatstart += 15; options |= PCRE_NO_START_OPTIMIZE; continue; }
8976    
# Line 8568  cd->dupnames = FALSE; Line 9158  cd->dupnames = FALSE;
9158  cd->namedrefcount = 0;  cd->namedrefcount = 0;
9159  cd->start_code = cworkspace;  cd->start_code = cworkspace;
9160  cd->hwm = cworkspace;  cd->hwm = cworkspace;
9161    cd->iscondassert = FALSE;
9162  cd->start_workspace = cworkspace;  cd->start_workspace = cworkspace;
9163  cd->workspace_size = COMPILE_WORK_SIZE;  cd->workspace_size = COMPILE_WORK_SIZE;
9164  cd->named_groups = named_groups;  cd->named_groups = named_groups;
# Line 8575  cd->named_group_list_size = NAMED_GROUP_ Line 9166  cd->named_group_list_size = NAMED_GROUP_
9166  cd->start_pattern = (const pcre_uchar *)pattern;  cd->start_pattern = (const pcre_uchar *)pattern;
9167  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));  cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
9168  cd->req_varyopt = 0;  cd->req_varyopt = 0;
9169    cd->parens_depth = 0;
9170  cd->assert_depth = 0;  cd->assert_depth = 0;
9171  cd->max_lookbehind = 0;  cd->max_lookbehind = 0;
9172  cd->external_options = options;  cd->external_options = options;
# Line 8589  outside can help speed up starting point Line 9181  outside can help speed up starting point
9181  ptr += skipatstart;  ptr += skipatstart;
9182  code = cworkspace;  code = cworkspace;
9183  *code = OP_BRA;  *code = OP_BRA;
9184    
9185  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,  (void)compile_regex(cd->external_options, &code, &ptr, &errorcode, FALSE,
9186    FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL,    FALSE, 0, 0, &firstchar, &firstcharflags, &reqchar, &reqcharflags, NULL,
9187    cd, &length);    cd, &length);
# Line 8603  if (length > MAX_PATTERN_SIZE) Line 9196  if (length > MAX_PATTERN_SIZE)
9196    goto PCRE_EARLY_ERROR_RETURN;    goto PCRE_EARLY_ERROR_RETURN;
9197    }    }
9198    
 /* If there are groups with duplicate names and there are also references by  
 name, we must allow for the possibility of named references to duplicated  
 groups. These require an extra data item each. */  
   
 if (cd->dupnames && cd->namedrefcount > 0)  
   length += cd->namedrefcount * IMM2_SIZE * sizeof(pcre_uchar);  
   
9199  /* Compute the size of the data block for storing the compiled pattern. Integer  /* Compute the size of the data block for storing the compiled pattern. Integer
9200  overflow should no longer be possible because nowadays we limit the maximum  overflow should no longer be possible because nowadays we limit the maximum
9201  value of cd->names_found and cd->name_entry_size. */  value of cd->names_found and cd->name_entry_size. */
# Line 8660  field; this time it's used for rememberi Line 9246  field; this time it's used for rememberi
9246  */  */
9247    
9248  cd->final_bracount = cd->bracount;  /* Save for checking forward references */  cd->final_bracount = cd->bracount;  /* Save for checking forward references */
9249    cd->parens_depth = 0;
9250  cd->assert_depth = 0;  cd->assert_depth = 0;
9251  cd->bracount = 0;  cd->bracount = 0;
9252  cd->max_lookbehind = 0;  cd->max_lookbehind = 0;
# Line 8667  cd->name_table = (pcre_uchar *)re + re-> Line 9254  cd->name_table = (pcre_uchar *)re + re->
9254  codestart = cd->name_table + re->name_entry_size * re->name_count;  codestart = cd->name_table + re->name_entry_size * re->name_count;
9255  cd->start_code = codestart;  cd->start_code = codestart;
9256  cd->hwm = (pcre_uchar *)(cd->start_workspace);  cd->hwm = (pcre_uchar *)(cd->start_workspace);
9257    cd->iscondassert = FALSE;
9258  cd->req_varyopt = 0;  cd->req_varyopt = 0;
9259  cd->had_accept = FALSE;  cd->had_accept = FALSE;
9260  cd->had_pruneorskip = FALSE;  cd->had_pruneorskip = FALSE;
# Line 8762  subpattern. */ Line 9350  subpattern. */
9350    
9351  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;  if (errorcode == 0 && re->top_backref > re->top_bracket) errorcode = ERR15;
9352    
9353  /* Unless disabled, check whether single character iterators can be  /* Unless disabled, check whether any single character iterators can be
9354  auto-possessified. The function overwrites the appropriate opcode values. */  auto-possessified. The function overwrites the appropriate opcode values, so
9355    the type of the pointer must be cast. NOTE: the intermediate variable "temp" is
9356    used in this code because at least one compiler gives a warning about loss of
9357    "const" attribute if the cast (pcre_uchar *)codestart is used directly in the
9358    function call. */
9359    
9360  if ((options & PCRE_NO_AUTO_POSSESSIFY) == 0)  if ((options & PCRE_NO_AUTO_POSSESS) == 0)
9361    auto_possessify((pcre_uchar *)codestart, utf, cd);    {
9362      pcre_uchar *temp = (pcre_uchar *)codestart;
9363      auto_possessify(temp, utf, cd);
9364      }
9365    
9366  /* If there were any lookbehind assertions that contained OP_RECURSE  /* If there were any lookbehind assertions that contained OP_RECURSE
9367  (recursions or subroutine calls), a flag is set for them to be checked here,  (recursions or subroutine calls), a flag is set for them to be checked here,
# Line 8989  return (pcre32 *)re; Line 9584  return (pcre32 *)re;
9584  }  }
9585    
9586  /* End of pcre_compile.c */  /* End of pcre_compile.c */
9587    

Legend:
Removed from v.1376  
changed lines
  Added in v.1538

  ViewVC Help
Powered by ViewVC 1.1.5