develooper Front page | perl.perl5.porters | Postings from April 2008

One less File::Copy bug

Thread Next
From:
Nicholas Clark
Date:
April 24, 2008 10:18
Subject:
One less File::Copy bug
Message ID:
20080424171812.GD70966@plum.flirble.org
I'm surprised how long it took us to make such an easy fix.

Sorry Abigail, you're going to have to update your talk.

Nicholas Clark

----- Forwarded message from Nicholas Clark <nwc10+p5p4@colon.colondot.net> -----

Envelope-to: nick@ccl4.org
Delivery-date: Thu, 24 Apr 2008 18:15:23 +0100
Mailing-List: contact perl5-changes-help@perl.org; run by ezmlm
Delivered-To: mailing list perl5-changes@perl.org
Delivered-To: perl5-changes@perl.org
From: Nicholas Clark <nwc10+p5p4@colon.colondot.net>
To: "Anybody And Everybody" <perl5-changes@perl.org>
Subject: Change 33740: Stop File::Copy truncating destination files if passed 3 named
Date: Thu, 24 Apr 2008 10:15:03 -0700 (PDT)

Change 33740 by nicholas@mouse-mill on 2008/04/24 17:04:58

	Stop File::Copy truncating destination files if passed 3 named
	arguments by accident. In Copy.t, ensure that all file system calls
	die with $! if they fail.

Affected files ...

... //depot/perl/lib/File/Copy.pm#50 edit
... //depot/perl/lib/File/Copy.t#10 edit

Differences ...

==== //depot/perl/lib/File/Copy.pm#50 (text) ====
Index: perl/lib/File/Copy.pm
--- perl/lib/File/Copy.pm#49~32184~	2007-10-24 02:37:15.000000000 -0700
+++ perl/lib/File/Copy.pm	2008-04-24 10:04:58.000000000 -0700
@@ -23,7 +23,7 @@
 # package has not yet been updated to work with Perl 5.004, and so it
 # would be a Bad Thing for the CPAN module to grab it and replace this
 # module.  Therefore, we set this module's version higher than 2.0.
-$VERSION = '2.11';
+$VERSION = '2.12';
 
 require Exporter;
 @ISA = qw(Exporter);
@@ -79,6 +79,12 @@
     my $from = shift;
     my $to = shift;
 
+    my $size;
+    if (@_) {
+	$size = shift(@_) + 0;
+	croak("Bad buffer size for copy: $size\n") unless ($size > 0);
+    }
+
     my $from_a_handle = (ref($from)
 			 ? (ref($from) eq 'GLOB'
 			    || UNIVERSAL::isa($from, 'GLOB')
@@ -148,7 +154,7 @@
 
     my $closefrom = 0;
     my $closeto = 0;
-    my ($size, $status, $r, $buf);
+    my ($status, $r, $buf);
     local($\) = '';
 
     my $from_h;
@@ -162,6 +168,14 @@
 	$closefrom = 1;
     }
 
+    # Seems most logical to do this here, in case future changes would want to
+    # make this croak for some reason.
+    unless (defined $size) {
+	$size = tied(*$from_h) ? 0 : -s $from_h || 0;
+	$size = 1024 if ($size < 512);
+	$size = $Too_Big if ($size > $Too_Big);
+    }
+
     my $to_h;
     if ($to_a_handle) {
        $to_h = $to;
@@ -173,15 +187,6 @@
 	$closeto = 1;
     }
 
-    if (@_) {
-	$size = shift(@_) + 0;
-	croak("Bad buffer size for copy: $size\n") unless ($size > 0);
-    } else {
-	$size = tied(*$from_h) ? 0 : -s $from_h || 0;
-	$size = 1024 if ($size < 512);
-	$size = $Too_Big if ($size > $Too_Big);
-    }
-
     $! = 0;
     for (;;) {
 	my ($r, $w, $t);

==== //depot/perl/lib/File/Copy.t#10 (xtext) ====
Index: perl/lib/File/Copy.t
--- perl/lib/File/Copy.t#9~28869~	2006-09-19 00:54:24.000000000 -0700
+++ perl/lib/File/Copy.t	2008-04-24 10:04:58.000000000 -0700
@@ -1,4 +1,4 @@
-#!./perl
+#!./perl -w
 
 BEGIN {
    if( $ENV{PERL_CORE} ) {
@@ -11,7 +11,7 @@
 
 my $TB = Test::More->builder;
 
-plan tests => 60;
+plan tests => 70;
 
 # We're going to override rename() later on but Perl has to see an override
 # at compile time to honor it.
@@ -40,14 +40,14 @@
   }
 
   # First we create a file
-  open(F, ">file-$$") or die;
+  open(F, ">file-$$") or die $!;
   binmode F; # for DOSISH platforms, because test 3 copies to stdout
   printf F "ok\n";
   close F;
 
   copy "file-$$", "copy-$$";
 
-  open(F, "copy-$$") or die;
+  open(F, "copy-$$") or die $!;
   $foo = <F>;
   close(F);
 
@@ -77,7 +77,7 @@
 
   require IO::File;
   $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
-  binmode $fh or die;
+  binmode $fh or die $!;
   copy("file-$$",$fh);
   $fh->close or die "close: $!";
   open(R, "copy-$$") or die; $foo = <R>; close(R);
@@ -86,10 +86,10 @@
 
   require FileHandle;
   my $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
-  binmode $fh or die;
+  binmode $fh or die $!;
   copy("file-$$",$fh);
   $fh->close;
-  open(R, "copy-$$") or die; $foo = <R>; close(R);
+  open(R, "copy-$$") or die $!; $foo = <R>; close(R);
   is $foo, "ok\n", 'copy(fn, fh): same contents';
   unlink "file-$$" or die "unlink: $!";
 
@@ -108,7 +108,7 @@
   ok move("copy-$$", "file-$$"), 'move';
   ok -e "file-$$",              '  destination exists';
   ok !-e "copy-$$",              '  source does not';
-  open(R, "file-$$") or die; $foo = <R>; close(R);
+  open(R, "file-$$") or die $!; $foo = <R>; close(R);
   is $foo, "ok\n", 'contents preserved';
 
   TODO: {
@@ -121,7 +121,7 @@
   }
 
   # trick: create lib/ if not exists - not needed in Perl core
-  unless (-d 'lib') { mkdir 'lib' or die; }
+  unless (-d 'lib') { mkdir 'lib' or die $!; }
   copy "file-$$", "lib";
   open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
   is $foo, "ok\n", 'copy(fn, dir): same contents';
@@ -129,7 +129,7 @@
 
   # Do it twice to ensure copying over the same file works.
   copy "file-$$", "lib";
-  open(R, "lib/file-$$") or die; $foo = <R>; close(R);
+  open(R, "lib/file-$$") or die $!; $foo = <R>; close(R);
   is $foo, "ok\n", 'copy over the same file works';
   unlink "lib/file-$$" or die "unlink: $!";
 
@@ -164,8 +164,8 @@
     ok !-z "file-$$", 
       'rt.perl.org 5196: copying to itself would truncate the file';
 
-    unlink "symlink-$$";
-    unlink "file-$$";
+    unlink "symlink-$$" or die $!;
+    unlink "file-$$" or die $!;
   }
 
   SKIP: {
@@ -185,9 +185,41 @@
     ok ! -z "file-$$",
       'rt.perl.org 5196: copying to itself would truncate the file';
 
-    unlink "hardlink-$$";
-    unlink "file-$$";
+    unlink "hardlink-$$" or die $!;
+    unlink "file-$$" or die $!;
   }
+
+  open(F, ">file-$$") or die $!;
+  binmode F;
+  print F "this is file\n";
+  close F;
+
+  my $copy_msg = "this is copy\n";
+  open(F, ">copy-$$") or die $!;
+  binmode F;
+  print F $copy_msg;
+  close F;
+
+  my @warnings;
+  local $SIG{__WARN__} = sub { push @warnings, join '', @_ };
+
+  # pie-$$ so that we force a non-constant, else the numeric conversion (of 0)
+  # is cached and we don't get a warning the second time round
+  is eval { copy("file-$$", "copy-$$", "pie-$$"); 1 }, undef,
+    "a bad buffer size fails to copy";
+  like $@, qr/Bad buffer size for copy/, "with a helpful error message";
+  unless (is scalar @warnings, 1, "There is 1 warning") {
+    diag $_ foreach @warnings;
+  }
+
+  is -s "copy-$$", length $copy_msg, "but does not truncate the destination";
+  open(F, "copy-$$") or die $!;
+  $foo = <F>;
+  close(F);
+  is $foo, $copy_msg, "nor change the destination's contents";
+
+  unlink "file-$$" or die $!;
+  unlink "copy-$$" or die $!;
 }
 
 
End of Patch.

----- End forwarded message -----

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About