Make sure truncated in-memory files have trailing null
authorFather Chrysostomos <sprout@cpan.org>
Fri, 25 Oct 2013 13:26:16 +0000 (06:26 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Fri, 25 Oct 2013 15:15:15 +0000 (08:15 -0700)
Just spotted this....

If the target scalar contains something already, then setting SvCUR to
0 is not sufficient.

ext/PerlIO-scalar/scalar.xs
ext/PerlIO-scalar/t/scalar.t

index fd8ac67..552345d 100644 (file)
@@ -55,6 +55,7 @@ PerlIOScalar_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg,
     {
        sv_force_normal(s->var);
        SvCUR_set(s->var, 0);
+       if (SvPOK(s->var)) *SvPVX(s->var) = 0;
     }
     if (SvUTF8(s->var) && !sv_utf8_downgrade(s->var, TRUE)) {
        if (ckWARN(WARN_UTF8))
index 4de54f7..a1d2b53 100644 (file)
@@ -16,7 +16,7 @@ use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); # Not 0, 1, 2 everywhere.
 
 $| = 1;
 
-use Test::More tests => 112;
+use Test::More tests => 113;
 
 my $fh;
 my $var = "aaa\n";
@@ -340,7 +340,7 @@ sub has_trailing_nul(\$) {
 }
 SKIP: {
     if ($Config::Config{'extensions'} !~ m!\bPerlIO/scalar\b!) {
-       skip "no B", 3;
+       skip "no B", 4;
     }
     require B;
 
@@ -359,6 +359,10 @@ SKIP: {
     print $fh "abc";
     ok has_trailing_nul $memfile,
         'write appends null when growing string after seek past end';
+
+    open $fh, ">", \($memfile = "hello");
+    ok has_trailing_nul $memfile,
+        'initial truncation in ">" mode provides trailing null';
 }
 
 # [perl #112780] Cloning of in-memory handles