Skip to content

Commit a134a60

Browse files
committed
Merge branch 'jc/perl-cat-blob' into maint-1.8.1
* jc/perl-cat-blob: Git.pm: fix cat_blob crashes on large files
2 parents d7df695 + 712c6ad commit a134a60

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

perl/Git.pm

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -965,20 +965,22 @@ sub cat_blob {
965965
my $size = $1;
966966

967967
my $blob;
968-
my $bytesRead = 0;
968+
my $bytesLeft = $size;
969969

970970
while (1) {
971-
my $bytesLeft = $size - $bytesRead;
972971
last unless $bytesLeft;
973972

974973
my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;
975-
my $read = read($in, $blob, $bytesToRead, $bytesRead);
974+
my $read = read($in, $blob, $bytesToRead);
976975
unless (defined($read)) {
977976
$self->_close_cat_blob();
978977
throw Error::Simple("in pipe went bad");
979978
}
980-
981-
$bytesRead += $read;
979+
unless (print $fh $blob) {
980+
$self->_close_cat_blob();
981+
throw Error::Simple("couldn't write to passed in filehandle");
982+
}
983+
$bytesLeft -= $read;
982984
}
983985

984986
# Skip past the trailing newline.
@@ -993,11 +995,6 @@ sub cat_blob {
993995
throw Error::Simple("didn't find newline after blob");
994996
}
995997

996-
unless (print $fh $blob) {
997-
$self->_close_cat_blob();
998-
throw Error::Simple("couldn't write to passed in filehandle");
999-
}
1000-
1001998
return $size;
1002999
}
10031000

0 commit comments

Comments
 (0)