croak if reallocation occurs during scan_str
[p5sagit/Devel-Declare.git] / Declare.xs
index 934f8f6..41a991b 100644 (file)
@@ -31,6 +31,10 @@ static int dd_debug = 0;
 #define LEX_NORMAL    10
 #define LEX_INTERPNORMAL   9
 
+/* please try not to have a line longer than this :) */
+
+#define DD_PREFERRED_LINESTR_SIZE 16384
+
 /* flag to trigger removal of temporary declaree sub */
 
 static int in_declare = 0;
@@ -214,10 +218,14 @@ int dd_toke_scan_ident(pTHX_ int offset) {
 }
 
 int dd_toke_scan_str(pTHX_ int offset) {
+  char* old_pvx = SvPVX(PL_linestr);
   STRLEN remaining = sv_len(PL_linestr) - offset;
   SV* line_copy = newSVsv(PL_linestr);
   char* base_s = SvPVX(PL_linestr) + offset;
   char* s = scan_str(base_s, FALSE, FALSE);
+  if(SvPVX(PL_linestr) != old_pvx)
+    croak("PL_linestr reallocated during scan_str, "
+      "Devel::Declare can't continue");
   if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
     int ret = (s - SvPVX(PL_linestr)) + remaining;
     sv_catsv(line_copy, PL_linestr);
@@ -229,8 +237,12 @@ int dd_toke_scan_str(pTHX_ int offset) {
 }
 
 int dd_toke_skipspace(pTHX_ int offset) {
+  char* old_pvx = SvPVX(PL_linestr);
   char* base_s = SvPVX(PL_linestr) + offset;
   char* s = skipspace_force(base_s);
+  if(SvPVX(PL_linestr) != old_pvx)
+    croak("PL_linestr reallocated during skipspace, "
+      "Devel::Declare can't continue");
   return s - base_s;
 }
 
@@ -274,6 +286,9 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
 
   PERL_UNUSED_VAR(user_data);
 
+  if (!DD_AM_LEXING)
+    return o; /* not lexing? */
+
   if (in_declare) {
     call_done_declare(aTHX);
     return o;
@@ -284,9 +299,6 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
   if (kid->op_type != OP_GV) /* not a GV so ignore */
     return o;
 
-  if (!DD_AM_LEXING)
-    return o; /* not lexing? */
-
   if (DD_DEBUG_TRACE) {
     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
   }
@@ -329,8 +341,8 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
 static void dd_block_start(pTHX_ int full)
 {
   PERL_UNUSED_VAR(full);
-  if (SvLEN(PL_linestr) < 8192)
-    (void) lex_grow_linestr(8192);
+  if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
+    (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
 }
 
 #else /* !DD_GROW_VIA_BLOCKHOOK */
@@ -360,7 +372,7 @@ OP* dd_pp_entereval(pTHX) {
         sv = sv_2mortal(newSVsv(sv));
       sv_catpvn(sv, "\n;", 2);
     }
-    SvGROW(sv, 8192);
+    SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
   }
   PUSHs(sv);
 #ifdef PERL_5_9_PLUS
@@ -379,16 +391,16 @@ STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
   return o;
 }
 
+#endif /* !DD_GROW_VIA_BLOCKHOOK */
+
 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
 {
   const I32 count = FILTER_READ(idx+1, sv, maxlen);
-  SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
+  SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
   /* filter_del(dd_filter_realloc); */
   return count;
 }
 
-#endif /* !DD_GROW_VIA_BLOCKHOOK */
-
 static int dd_handle_const(pTHX_ char *name) {
   switch (PL_lex_inwhat) {
     case OP_QR:
@@ -500,9 +512,7 @@ setup()
     hook_op_check(OP_CONST, dd_ck_const, NULL);
 #endif /* !DD_CONST_VIA_RV2CV */
   }
-#if !DD_GROW_VIA_BLOCKHOOK
   filter_add(dd_filter_realloc, NULL);
-#endif /* !DD_GROW_VIA_BLOCKHOOK */
 
 char*
 get_linestr()