Skip to content

Commit

Permalink
Merge branch 'jc/perl-cat-blob' into maint-1.8.1
Browse files Browse the repository at this point in the history
* jc/perl-cat-blob:
  Git.pm: fix cat_blob crashes on large files
  • Loading branch information
Junio C Hamano committed Apr 3, 2013
2 parents d7df695 + 712c6ad commit a134a60
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions perl/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -965,20 +965,22 @@ sub cat_blob {
my $size = $1;

my $blob;
my $bytesRead = 0;
my $bytesLeft = $size;

while (1) {
my $bytesLeft = $size - $bytesRead;
last unless $bytesLeft;

my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;
my $read = read($in, $blob, $bytesToRead, $bytesRead);
my $read = read($in, $blob, $bytesToRead);
unless (defined($read)) {
$self->_close_cat_blob();
throw Error::Simple("in pipe went bad");
}

$bytesRead += $read;
unless (print $fh $blob) {
$self->_close_cat_blob();
throw Error::Simple("couldn't write to passed in filehandle");
}
$bytesLeft -= $read;
}

# Skip past the trailing newline.
Expand All @@ -993,11 +995,6 @@ sub cat_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;
}

Expand Down

0 comments on commit a134a60

Please sign in to comment.