Merge branch 'jc/perl-cat-blob' into maint

perl/Git.pm::cat_blob slurped everything in core only to write it
out to a file descriptor, which was not a very smart thing to do.

* jc/perl-cat-blob:
  Git.pm: fix cat_blob crashes on large files
This commit is contained in:
Junio C Hamano 2013-03-26 12:42:24 -07:00
commit 06d7abb13c

View File

@ -965,20 +965,22 @@ sub cat_blob {
my $size = $1; my $size = $1;
my $blob; my $blob;
my $bytesRead = 0; my $bytesLeft = $size;
while (1) { while (1) {
my $bytesLeft = $size - $bytesRead;
last unless $bytesLeft; last unless $bytesLeft;
my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024; my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;
my $read = read($in, $blob, $bytesToRead, $bytesRead); my $read = read($in, $blob, $bytesToRead);
unless (defined($read)) { unless (defined($read)) {
$self->_close_cat_blob(); $self->_close_cat_blob();
throw Error::Simple("in pipe went bad"); throw Error::Simple("in pipe went bad");
} }
unless (print $fh $blob) {
$bytesRead += $read; $self->_close_cat_blob();
throw Error::Simple("couldn't write to passed in filehandle");
}
$bytesLeft -= $read;
} }
# Skip past the trailing newline. # Skip past the trailing newline.
@ -993,11 +995,6 @@ sub cat_blob {
throw Error::Simple("didn't find newline after blob"); throw Error::Simple("didn't find newline after blob");
} }
unless (print $fh $blob) {
$self->_close_cat_blob();
throw Error::Simple("couldn't write to passed in filehandle");
}
return $size; return $size;
} }