Really check that sysread(I, $x, 1, -4) dies with "Offset outside string"
[p5sagit/p5-mst-13.2.git] / t / op / write.t
old mode 100755 (executable)
new mode 100644 (file)
index 4b5005a..f13ac5f
@@ -6,6 +6,8 @@ BEGIN {
     require './test.pl';
 }
 
+use strict;    # Amazed that this hackery can be made strict ...
+
 # read in a file
 sub cat {
     my $file = shift;
@@ -59,7 +61,7 @@ for my $tref ( @NumTests ){
 my $bas_tests = 20;
 
 # number of tests in section 3
-my $bug_tests = 4;
+my $bug_tests = 4 + 3 * 3 * 5 * 2 * 3 + 2 + 1 + 1;
 
 # number of tests in section 4
 my $hmb_tests = 35;
@@ -72,6 +74,8 @@ plan $tests;
 ## Section 1
 ############
 
+use vars qw($fox $multiline $foo $good);
+
 format OUT =
 the quick brown @<<
 $fox
@@ -100,7 +104,7 @@ $foo = 'when in the course of human events it becomes necessary';
 write(OUT);
 close OUT or die "Could not close: $!";
 
-$right =
+my $right =
 "the quick brown fox
 jumped
 forescore
@@ -195,7 +199,7 @@ is cat('Op_write.tmp'), $right and do { 1 while unlink 'Op_write.tmp' };
 
 # formline tests
 
-$mustbe = <<EOT;
+$right = <<EOT;
 @ a
 @> ab
 @>> abc
@@ -209,7 +213,8 @@ $mustbe = <<EOT;
 @>>>>>>>>>>         abc
 EOT
 
-$was1 = $was2 = '';
+my $was1 = my $was2 = '';
+use vars '$format2';
 for (0..10) {           
   # lexical picture
   $^A = '';
@@ -222,8 +227,8 @@ for (0..10) {
   formline $format2, 'abc';
   $was2 .= "$format2 $^A\n";
 }
-is $was1, $mustbe;
-is $was2, $mustbe;
+is $was1, $right;
+is $was2, $right;
 
 $^A = '';
 
@@ -282,6 +287,7 @@ EOFORMAT
 
 open(OUT10, '>Op_write.tmp') || die "Can't create Op_write.tmp";
 
+use vars '$test1';
 $test1 = 12.95;
 write(OUT10);
 close OUT10 or die "Could not close: $!";
@@ -493,8 +499,6 @@ for my $tref ( @NumTests ){
 ## Easiest to add new tests just here
 #####################################
 
-use strict;    # Amazed that this hackery can be made strict ...
-
 # DAPM. Exercise a couple of error codepaths
 
 {
@@ -507,6 +511,72 @@ use strict;        # Amazed that this hackery can be made strict ...
     like $@, qr/Undefined format/, 'no such format';
 }
 
+{
+  package Count;
+
+  sub TIESCALAR {
+    my $class = shift;
+    bless [shift, 0, 0], $class;
+  }
+
+  sub FETCH {
+    my $self = shift;
+    ++$self->[1];
+    $self->[0];
+  }
+
+  sub STORE {
+    my $self = shift;
+    ++$self->[2];
+    $self->[0] = shift;
+  }
+}
+
+{
+  my ($pound_utf8, $pm_utf8) = map { my $a = "$_\x{100}"; chop $a; $a}
+    my ($pound, $pm) = ("\xA3", "\xB1");
+
+  foreach my $first ('N', $pound, $pound_utf8) {
+    foreach my $base ('N', $pm, $pm_utf8) {
+      foreach my $second ($base, "$base\n", "$base\nMoo!", "$base\nMoo!\n",
+                         "$base\nMoo!\n",) {
+       foreach (['^*', qr/(.+)/], ['@*', qr/(.*?)$/s]) {
+         my ($format, $re) = @$_;
+         foreach my $class ('', 'Count') {
+           my $name = "$first, $second $format $class";
+           $name =~ s/\n/\\n/g;
+
+           $first =~ /(.+)/ or die $first;
+           my $expect = "1${1}2";
+           $second =~ $re or die $second;
+           $expect .= " 3${1}4";
+
+           if ($class) {
+             my $copy1 = $first;
+             my $copy2;
+             tie $copy2, $class, $second;
+             is swrite("1^*2 3${format}4", $copy1, $copy2), $expect, $name;
+             my $obj = tied $copy2;
+             is $obj->[1], 1, 'value read exactly once';
+           } else {
+             my ($copy1, $copy2) = ($first, $second);
+             is swrite("1^*2 3${format}4", $copy1, $copy2), $expect, $name;
+           }
+         }
+       }
+      }
+    }
+  }
+}
+
+{
+  # This will fail an assertion in 5.10.0 built with -DDEBUGGING (because
+  # pp_formline attempts to set SvCUR() on an SVt_RV). I suspect that it will
+  # be doing something similarly out of bounds on everything from 5.000
+  my $ref = [];
+  is swrite('>^*<', $ref), ">$ref<";
+  is swrite('>@*<', $ref), ">$ref<";
+}
 
 format EMPTY =
 .
@@ -537,6 +607,26 @@ $= = 10;
 select $oldfh;
 close STDOUT_DUP;
 
+*CmT =  *{$::{Comment}}{FORMAT};
+ok  defined *{$::{CmT}}{FORMAT}, "glob assign";
+
+fresh_perl_like(<<'EOP', qr/^Format STDOUT redefined at/, {stderr => 1}, '#64562 - Segmentation fault with redefined formats and warnings');
+#!./perl
+
+use strict;
+use warnings; # crashes!
+
+format =
+.
+
+write;
+
+format =
+.
+
+write;
+EOP
+
 #############################
 ## Section 4
 ## Add new tests *above* here