This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More generalized fix for #127855, overallocate in SvGROW and not just sv_grow()
authorYves Orton <demerphq@gmail.com>
Fri, 8 Apr 2016 19:25:20 +0000 (21:25 +0200)
committerYves Orton <demerphq@gmail.com>
Fri, 8 Apr 2016 19:30:50 +0000 (21:30 +0200)
If we overallocate in SvGROW() and not just sv_grow() we can ensure
we have more cases where we can COW. We need to ensure we always
have room for a null and a reference count.

sv.c
sv.h

diff --git a/sv.c b/sv.c
index 68df702..0200679 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -4891,7 +4891,7 @@ Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
     }
     SvUPGRADE(sv, SVt_PV);
 
-    dptr = SvGROW(sv, len + 2);
+    dptr = SvGROW(sv, len + 1);
     Move(ptr,dptr,len,char);
     dptr[len] = '\0';
     SvCUR_set(sv, len);
diff --git a/sv.h b/sv.h
index bfda6bf..0c6e6d4 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -2111,12 +2111,15 @@ See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.
 
 #ifdef PERL_ANY_COW
 # define SvGROW(sv,len) \
-       (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
+        (SvIsCOW(sv) || SvLEN(sv) < ((len)+2) ? sv_grow(sv,(len)+2) : SvPVX(sv))
+# define SvGROW_mutable(sv,len) \
+    (SvLEN(sv) < ((len)+2) ? sv_grow(sv,(len)+2) : SvPVX_mutable(sv))
 #else
-# define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
+# define SvGROW(sv,len) \
+    (SvLEN(sv) < ((len)+1) ? sv_grow(sv,(len)+1) : SvPVX(sv))
+# define SvGROW_mutable(sv,len) \
+    (SvLEN(sv) < ((len)+1) ? sv_grow(sv,(len)+1) : SvPVX_mutable(sv))
 #endif
-#define SvGROW_mutable(sv,len) \
-    (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
 #define Sv_Grow sv_grow
 
 #define CLONEf_COPY_STACKS 1