]> git.vpit.fr Git - perl/modules/re-engine-Plugin.git/blobdiff - Plugin.xs
Fix an off-by-one error when allocating the parens buffer
[perl/modules/re-engine-Plugin.git] / Plugin.xs
index e9d1307a58d92df669fcb66abbecca469f032852..2ebc28deebb9c3be4afe92773fcc1aaaec4efc23 100644 (file)
--- a/Plugin.xs
+++ b/Plugin.xs
@@ -44,13 +44,11 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
     dSP;
     REGEXP * rx;
     re__engine__Plugin re;
-    I32 count;
     I32 buffers;
 
     /* exp/xend version of the pattern & length */
     STRLEN plen;
     char*  exp = SvPV((SV*)pattern, plen);
-    char* xend = exp + plen;
 
     /* The REGEXP structure to return to perl */
     Newxz(rx, 1, REGEXP);
@@ -67,7 +65,7 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
     rx->extflags = flags;          /* Flags for perl to use */
     rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */
 
-    /* Store a precompiled regexp for pp_regcomp to use */
+    /* Precompiled pattern for pp_regcomp to use */
     rx->prelen = plen;
     rx->precomp = savepvn(exp, rx->prelen);
 
@@ -112,7 +110,7 @@ Plugin_comp(pTHX_ const SV * const pattern, const U32 flags)
 
     buffers = rx->nparens;
 
-    Newxz(rx->offs, buffers, regexp_paren_pair);
+    Newxz(rx->offs, buffers + 1, regexp_paren_pair);
 
     return rx;
 }
@@ -126,32 +124,36 @@ Plugin_exec(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
     SV * callback = get_H_callback("exec");
     GET_SELF_FROM_PPRIVATE(rx->pprivate);
 
-    /* Store the current str for ->str */
-    self->str = (SV*)sv;
-    SvREFCNT_inc(self->str);
+    if (callback) {
+        /* Store the current str for ->str */
+        self->str = (SV*)sv;
+        SvREFCNT_inc(self->str);
 
-    ENTER;
-    SAVETMPS;
+        ENTER;
+        SAVETMPS;
    
-    PUSHMARK(SP);
-    XPUSHs(rx->pprivate);
-    XPUSHs(sv);
-    PUTBACK;
+        PUSHMARK(SP);
+        XPUSHs(rx->pprivate);
+        XPUSHs(sv);
+        PUTBACK;
 
-    call_sv(callback, G_SCALAR);
+        call_sv(callback, G_SCALAR);
  
-    SPAGAIN;
+        SPAGAIN;
 
-    SV * ret = POPs;
+        SV * ret = POPs;
 
-    if (SvTRUE(ret))
-        matched = 1;
-    else
-        matched = 0;
+        if (SvTRUE(ret))
+            matched = 1;
+        else
+            matched = 0;
 
-    PUTBACK;
-    FREETMPS;
-    LEAVE;
+        PUTBACK;
+        FREETMPS;
+        LEAVE;
+    } else {
+        matched = 0;
+    }
 
     return matched;
 }
@@ -206,9 +208,9 @@ Plugin_free(pTHX_ REGEXP * const rx)
 }
 
 void *
-Plugin_dupe(pTHX_ const REGEXP * rx, CLONE_PARAMS *param)
+Plugin_dupe(pTHX_ REGEXP * const rx, CLONE_PARAMS *param)
 {
-    Perl_croak("dupe not supported yet");
+    Perl_croak(aTHX_ "dupe not supported yet");
     return rx->pprivate;
 }
 
@@ -257,7 +259,6 @@ Plugin_numbered_buff_STORE(pTHX_ REGEXP * const rx, const I32 paren,
                            SV const * const value)
 {
     dSP;
-    I32 items;
     SV * callback;
     GET_SELF_FROM_PPRIVATE(rx->pprivate);
 
@@ -286,11 +287,8 @@ Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const rx, const SV * const sv,
                               const I32 paren)
 {
     dSP;
-    I32 items;
     SV * callback;
-    re__engine__Plugin self;
-
-    SELF_FROM_PPRIVATE(self,rx->pprivate);
+    GET_SELF_FROM_PPRIVATE(rx->pprivate);
 
     callback = self->cb_num_capture_buff_LENGTH;
 
@@ -322,12 +320,16 @@ Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const rx, const SV * const sv,
 
 
 SV*
-Plugin_named_buff_FETCH(pTHX_ REGEXP * const rx, SV * const key, U32 flags)
+Plugin_named_buff (pTHX_ REGEXP * const rx, SV * const key, SV * const value,
+                   const U32 flags)
 {
-    PERL_UNUSED_ARG(rx);
-    PERL_UNUSED_ARG(key);
-    PERL_UNUSED_ARG(flags);
+    return NULL;
+}
 
+SV*
+Plugin_named_buff_iter (pTHX_ REGEXP * const rx, const SV * const lastkey,
+                        const U32 flags)
+{
     return NULL;
 }
 
@@ -339,23 +341,17 @@ Plugin_package(pTHX_ REGEXP * const rx)
 }
 
 MODULE = re::engine::Plugin    PACKAGE = re::engine::Plugin
-PROTOTYPES: ENABLE
+PROTOTYPES: DISABLE
 
-SV *
+void
 pattern(re::engine::Plugin self, ...)
-CODE:
-    SvREFCNT_inc(self->pattern);
-    RETVAL = self->pattern;
-OUTPUT:
-    RETVAL
+PPCODE:
+    XPUSHs(self->pattern);
 
-SV *
+void
 str(re::engine::Plugin self, ...)
-CODE:
-    SvREFCNT_inc(self->str);
-    RETVAL = self->str;
-OUTPUT:
-    RETVAL
+PPCODE:
+    XPUSHs(self->str);
 
 char*
 mod(re::engine::Plugin self, ...)
@@ -390,50 +386,58 @@ PPCODE:
       XPUSHs(&PL_sv_yes);
     }
 
-SV *
+void
 stash(re::engine::Plugin self, ...)
-PREINIT:
-    SV * stash;
-CODE:
+PPCODE:
     if (items > 1) {
-        self->stash = sv_mortalcopy(ST(1));
+        self->stash = ST(1);
         SvREFCNT_inc(self->stash);
+        XSRETURN_EMPTY;
+    } else {
+        XPUSHs(self->stash);
     }
-    SvREFCNT_inc(self->stash);
-    RETVAL = self->stash;
-OUTPUT:
-    RETVAL
 
-SV *
+void
 minlen(re::engine::Plugin self, ...)
-CODE:
+PPCODE:
     if (items > 1) {
         self->rx->minlen = (I32)SvIV(ST(1));
+        XSRETURN_EMPTY;
+    } else {
+        if (self->rx->minlen) {
+            XPUSHs(sv_2mortal(newSViv(self->rx->minlen)));
+        } else {
+            XPUSHs(sv_2mortal(&PL_sv_undef));
+        }
     }
 
-    RETVAL = self->rx->minlen ? newSViv(self->rx->minlen) : &PL_sv_undef;
-OUTPUT:
-    RETVAL
-
-SV *
+void
 gofs(re::engine::Plugin self, ...)
-CODE:
+PPCODE:
     if (items > 1) {
         self->rx->gofs = (U32)SvIV(ST(1));
+        XSRETURN_EMPTY;
+    } else {
+        if (self->rx->gofs) {
+            XPUSHs(sv_2mortal(newSVuv(self->rx->gofs)));
+        } else {
+            XPUSHs(sv_2mortal(&PL_sv_undef));
+        }
     }
-    RETVAL = self->rx->gofs ? newSVuv(self->rx->gofs) : &PL_sv_undef;
-OUTPUT:
-    RETVAL
 
-SV *
+void
 nparens(re::engine::Plugin self, ...)
-CODE:
+PPCODE:
     if (items > 1) {
         self->rx->nparens = (U32)SvIV(ST(1));
+        XSRETURN_EMPTY;
+    } else {
+        if (self->rx->nparens) {
+            XPUSHs(sv_2mortal(newSVuv(self->rx->nparens)));
+        } else {
+            XPUSHs(sv_2mortal(&PL_sv_undef));
+        }
     }
-    RETVAL = self->rx->nparens ? newSVuv(self->rx->nparens) : &PL_sv_undef;
-OUTPUT:
-    RETVAL
 
 void
 _num_capture_buff_FETCH(re::engine::Plugin self, ...)