This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
allow embedded null characters in diagnostics
[perl5.git] / regcomp.c
index 19b030f..2755d61 100644 (file)
--- a/regcomp.c
+++ b/regcomp.c
@@ -66,7 +66,7 @@
  *
  ****    Alterations to Henry's code are...
  ****
- ****    Copyright (c) 1991-1998, Larry Wall
+ ****    Copyright (c) 1991-1999, Larry Wall
  ****
  ****    You may distribute under the terms of either the GNU General Public
  ****    License or the Artistic License, as specified in the README file.
@@ -146,6 +146,8 @@ static void regtail _((regnode *, regnode *));
 static char* regwhite _((char *, char *));
 static char* nextchar _((void));
 static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
+static char* regpposixcc _((I32 value));
+static void clear_re _((void *r));
 #endif
 
 /* Length of a variant. */
@@ -170,7 +172,8 @@ typedef struct {
 } scan_data_t;
 #endif
 
-static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
+                                     0, 0, 0 };
 
 #define SF_BEFORE_EOL          (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
 #define SF_BEFORE_SEOL         0x1
@@ -206,6 +209,12 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
 
 STATIC void
+clear_re(void *r)
+{
+    ReREFCNT_dec((regexp *)r);
+}
+
+STATIC void
 scan_commit(scan_data_t *data)
 {
     dTHR;
@@ -607,7 +616,7 @@ study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32
                        
                        l -= old;
                        /* Get the added string: */
-                       last_str = newSVpv(s  + old, l);
+                       last_str = newSVpvn(s  + old, l);
                        if (deltanext == 0 && pos_before == b) {
                            /* What was added is a constant string */
                            if (mincount > 1) {
@@ -776,7 +785,7 @@ reginitcolors(void)
                PL_colors[i] = ++s;
            }
            else
-               PL_colors[i] = "";
+               PL_colors[i] = s = "";
        }
     } else {
        while (i < 6) 
@@ -867,7 +876,12 @@ pregcomp(char *exp, char *xend, PMOP *pm)
     r->prelen = xend - exp;
     r->precomp = PL_regprecomp;
     r->subbeg = r->subbase = NULL;
-    r->nparens = PL_regnpar - 1;               /* set early to validate backrefs */
+    r->nparens = PL_regnpar - 1;       /* set early to validate backrefs */
+
+    r->substrs = 0;                    /* Useful during FAIL. */
+    r->startp = 0;                     /* Useful during FAIL. */
+    r->endp = 0;                       /* Useful during FAIL. */
+
     PL_regcomp_rx = r;
 
     /* Second pass: emit code. */
@@ -963,9 +977,9 @@ pregcomp(char *exp, char *xend, PMOP *pm)
        */
        minlen = 0;
 
-       data.longest_fixed = newSVpv("",0);
-       data.longest_float = newSVpv("",0);
-       data.last_found = newSVpv("",0);
+       data.longest_fixed = newSVpvn("",0);
+       data.longest_float = newSVpvn("",0);
+       data.last_found = newSVpvn("",0);
        data.longest = &(data.longest_fixed);
        first = scan;
        
@@ -1152,16 +1166,16 @@ reg(I32 paren, I32 *flagp)
                    AV *av;
                    
                    if (PL_regcomp_parse - 1 - s) 
-                       sv = newSVpv(s, PL_regcomp_parse - 1 - s);
+                       sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
                    else
-                       sv = newSVpv("", 0);
+                       sv = newSVpvn("", 0);
 
                    rop = sv_compile_2op(sv, &sop, "re", &av);
 
-                   n = add_data(3, "nso");
+                   n = add_data(3, "nop");
                    PL_regcomp_rx->data->data[n] = (void*)rop;
-                   PL_regcomp_rx->data->data[n+1] = (void*)av;
-                   PL_regcomp_rx->data->data[n+2] = (void*)sop;
+                   PL_regcomp_rx->data->data[n+1] = (void*)sop;
+                   PL_regcomp_rx->data->data[n+2] = (void*)av;
                    SvREFCNT_dec(sv);
                }
                else {                                          /* First pass */
@@ -1377,8 +1391,11 @@ reg(I32 paren, I32 *flagp)
     }
 
     /* Check for proper termination. */
-    if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
-       FAIL("unmatched () in regexp");
+    if (paren) {
+       PL_regflags = oregflags;
+       if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
+           FAIL("unmatched () in regexp");
+       }
     }
     else if (!paren && PL_regcomp_parse < PL_regxend) {
        if (*PL_regcomp_parse == ')') {
@@ -1388,9 +1405,6 @@ reg(I32 paren, I32 *flagp)
            FAIL("junk on end of regexp");      /* "Can't happen". */
        /* NOTREACHED */
     }
-    if (paren != 0) {
-       PL_regflags = oregflags;
-    }
 
     return(ret);
 }
@@ -1764,6 +1778,7 @@ tryagain:
            break;
        case 'b':
            PL_seen_zerolen++;
+           PL_regseen |= REG_SEEN_LOOKBEHIND;
            ret = reg_node(
                UTF
                    ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
@@ -1775,6 +1790,7 @@ tryagain:
            break;
        case 'B':
            PL_seen_zerolen++;
+           PL_regseen |= REG_SEEN_LOOKBEHIND;
            ret = reg_node(
                UTF
                    ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
@@ -2362,7 +2378,7 @@ regclassutf8(void)
            flags |= ANYOF_FOLD;
        if (LOC)
            flags |= ANYOF_LOCALE;
-       listsv = newSVpv("# comment\n",0);
+       listsv = newSVpvn("# comment\n",10);
     }
 
     if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
@@ -2482,7 +2498,7 @@ regclassutf8(void)
                    e = strchr(PL_regcomp_parse++, '}');
                     if (!e)
                         FAIL("Missing right brace on \\x{}");
-                   value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
+                   value = scan_hex(PL_regcomp_parse, e - PL_regcomp_parse, &numlen);
                    PL_regcomp_parse = e + 1;
                }
                else {
@@ -3108,13 +3124,30 @@ pregfree(struct regexp *r)
     }
     if (r->data) {
        int n = r->data->count;
+       AV* new_comppad = NULL;
+       AV* old_comppad;
+       SV** old_curpad;
+
        while (--n >= 0) {
            switch (r->data->what[n]) {
            case 's':
                SvREFCNT_dec((SV*)r->data->data[n]);
                break;
+           case 'p':
+               new_comppad = (AV*)r->data->data[n];
+               break;
            case 'o':
+               if (new_comppad == NULL)
+                   croak("panic: pregfree comppad");
+               old_comppad = PL_comppad;
+               old_curpad = PL_curpad;
+               PL_comppad = new_comppad;
+               PL_curpad = AvARRAY(new_comppad);
                op_free((OP_4tree*)r->data->data[n]);
+               PL_comppad = old_comppad;
+               PL_curpad = old_curpad;
+               SvREFCNT_dec((SV*)new_comppad);
+               new_comppad = NULL;
                break;
            case 'n':
                break;
@@ -3159,6 +3192,7 @@ re_croak2(const char* pat1,const char* pat2,...)
     STRLEN l1 = strlen(pat1);
     STRLEN l2 = strlen(pat2);
     char buf[512];
+    SV *msv;
     char *message;
 
     if (l1 > 510)
@@ -3170,9 +3204,9 @@ re_croak2(const char* pat1,const char* pat2,...)
     buf[l1 + l2] = '\n';
     buf[l1 + l2 + 1] = '\0';
     va_start(args, pat2);
-    message = mess(buf, &args);
+    msv = mess(buf, &args);
     va_end(args);
-    l1 = strlen(message);
+    message = SvPV(msv,l1);
     if (l1 > 512)
        l1 = 512;
     Copy(message, buf, l1 , char);