{"thread":{"id":"32977","subject":"[PATCHv3] Fix in Git.pm cat_blob crashes on large files","startedAt":"2013-02-22T21:01:18Z","lastAt":"2013-02-22T21:20:02Z","messageCount":2,"participants":["Joshua Clayton","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"210055","messageId":"1361566878-20117-1-git-send-email-stillcompiling@gmail.com","threadId":"32977","inReplyTo":null,"subject":"[PATCHv3] Fix in Git.pm cat_blob crashes on large files","fromName":"Joshua Clayton","fromEmail":"stillcompiling@gmail.com","sentAt":"2013-02-22T21:01:18Z","receivedAt":"2013-02-22T21:01:18Z","isPatch":false,"sender":{"key":"stillcompiling@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1380815?v=4"},"body":"Read and write each 1024 byte buffer, rather than trying to buffer\nthe entire content of the file.\nPrevious code would crash on all files > 2 Gib, when the offset variable\nbecame negative (perhaps below the level of perl), resulting in a crash.\nOn a 32 bit system, or a system with low memory it might crash before\nreaching 2 GiB due to memory exhaustion.\nThis code may leave a partial file behind in case of failure, where the\nold code would leave a completely empty file.\nNeither version verifies the correctness of the content.\nCalling code must take care of verification and cleanup.\n\nSigned-off-by: Joshua Clayton <stillcompiling@gmail.com>\nReviewed-by: Jeff King <peff@peff.net>\nReviewed-by: Junio C Hamano <gitster@pobox.com>\n---\n perl/Git.pm |   17 +++++++----------\n 1 file changed, 7 insertions(+), 10 deletions(-)\n\ndiff --git a/perl/Git.pm b/perl/Git.pm\nindex 931047c..db6e0a8 100644\n--- a/perl/Git.pm\n+++ b/perl/Git.pm\n@@ -942,20 +942,22 @@ sub cat_blob {\n \tmy $size = $1;\n \n \tmy $blob;\n-\tmy $bytesRead = 0;\n+\tmy $bytesLeft = $size;\n \n \twhile (1) {\n-\t\tmy $bytesLeft = $size - $bytesRead;\n \t\tlast unless $bytesLeft;\n \n \t\tmy $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;\n-\t\tmy $read = read($in, $blob, $bytesToRead, $bytesRead);\n+\t\tmy $read = read($in, $blob, $bytesToRead);\n \t\tunless (defined($read)) {\n \t\t\t$self->_close_cat_blob();\n \t\t\tthrow Error::Simple(\"in pipe went bad\");\n \t\t}\n-\n-\t\t$bytesRead += $read;\n+\t\tunless (print $fh $blob) {\n+\t\t\t$self->_close_cat_blob();\n+\t\t\tthrow Error::Simple(\"couldn't write to passed in filehandle\");\n+\t\t}\n+\t\t$bytesLeft -= $read;\n \t}\n \n \t# Skip past the trailing newline.\n@@ -970,11 +972,6 @@ sub cat_blob {\n \t\tthrow Error::Simple(\"didn't find newline after blob\");\n \t}\n \n-\tunless (print $fh $blob) {\n-\t\t$self->_close_cat_blob();\n-\t\tthrow Error::Simple(\"couldn't write to passed in filehandle\");\n-\t}\n-\n \treturn $size;\n }\n \n-- \n1.7.10.4\nI'm trying to send this with git-send-email this time around to beat\n the linewrapping problem.\n"},{"id":"210058","messageId":"7vip5kcagt.fsf@alter.siamese.dyndns.org","threadId":"32977","inReplyTo":"1361566878-20117-1-git-send-email-stillcompiling@gmail.com","subject":"Re: [PATCHv3] Fix in Git.pm cat_blob crashes on large files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-02-22T21:20:02Z","receivedAt":"2013-02-22T21:20:02Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n"}]}