# [PATCH] Fix in Git.pm cat_blob crashes on large files (reviewed by Jeff King)

2 messages from 2013-02-22 to 2013-02-22. Participants: Joshua Clayton, Erik Faye-Lund.
Thread: https://gitlist.dev/t/32975

## Joshua Clayton, 2013-02-22 17:03

Subject: [PATCH] Fix in Git.pm cat_blob crashes on large files (reviewed by Jeff King)
Message-ID: <CAMB+bfLjewgKjSN6WNHGkwpRx9OaWipi_-TDa0MnJMDN+6tPZA@mail.gmail.com>
URL: https://gitlist.dev/e/CAMB%2BbfLjewgKjSN6WNHGkwpRx9OaWipi_-TDa0MnJMDN%2B6tPZA%40mail.gmail.com

```
Read and write each 1024 byte buffer, rather than trying to buffer
the entire content of the file.
Previous code would crash on all files > 2 Gib, when the offset variable
became negative (perhaps below the level of perl), resulting in a crash.
On a 32 bit system, or a system with low memory it might crash before
reaching 2 GiB due to memory exhaustion.

Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
---
 perl/Git.pm |   12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/perl/Git.pm b/perl/Git.pm
index 931047c..cc91288 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -949,13 +949,16 @@ sub cat_blob {
 		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");
+		}
 	}

 	# Skip past the trailing newline.
@@ -970,11 +973,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;
 }

-- 
1.7.10.4

```

## Erik Faye-Lund, 2013-02-22 17:10

Subject: Re: [PATCH] Fix in Git.pm cat_blob crashes on large files (reviewed by Jeff King)
Message-ID: <CABPQNSaGSOio1Sn=--jdCfOkw4N+L_khH3Z6AZAQ=aaUg_OGig@mail.gmail.com>
URL: https://gitlist.dev/e/CABPQNSaGSOio1Sn%3D--jdCfOkw4N%2BL_khH3Z6AZAQ%3DaaUg_OGig%40mail.gmail.com
In-Reply-To: <CAMB+bfLjewgKjSN6WNHGkwpRx9OaWipi_-TDa0MnJMDN+6tPZA@mail.gmail.com>

```
On Fri, Feb 22, 2013 at 6:03 PM, Joshua Clayton
<stillcompiling@gmail.com> wrote:
> Read and write each 1024 byte buffer, rather than trying to buffer
> the entire content of the file.
> Previous code would crash on all files > 2 Gib, when the offset variable
> became negative (perhaps below the level of perl), resulting in a crash.
> On a 32 bit system, or a system with low memory it might crash before
> reaching 2 GiB due to memory exhaustion.
>
> Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>

We usually put "Reviewed-by: Name <email>" right below the SoB-line,
rather than in the subject. And then we CC the people who were
involved in the previous round :)

```
