git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCHv3] Fix in Git.pm cat_blob crashes on large files

From
Joshua Clayton <stillcompiling@gmail.com>
Date
Feb 22, 2013, 21:01 UTC
Message-ID
<1361566878-20117-1-git-send-email-stillcompiling@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. This code may leave a partial file behind in case of failure, where the old code would leave a completely empty file. Neither version verifies the correctness of the content. Calling code must take care of verification and cleanup.

Signed-off-by: Joshua Clayton <stillcompiling@gmail.com>
Reviewed-by: Jeff King <peff@peff.net>
Reviewed-by: Junio C Hamano <gitster@pobox.com>
---
 perl/Git.pm |   17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 931047c..db6e0a8 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -942,20 +942,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.
@@ -970,11 +972,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
I'm trying to send this with git-send-email this time around to beat
 the linewrapping problem.
Next: Junio C Hamano
Message 1 of 2 in “[PATCHv3] Fix in Git.pm cat_blob crashes on large files”
  1. Joshua ClaytonFeb 22, 2013
  2. Junio C HamanoFeb 22, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.