Thread (4 messages) flat view 4 messages, 2 authors, 2016-06-15
DORMANTno replies REVIEWED: 1 (0M)

1 review trailer.

[PATCH] Fix in Git.pm cat_blob crashes on large files (resubmit with reviewed-by)

From: Joshua Clayton <hidden>
Date: 2016-06-15 22:56:13
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
Reviewed-by: Jeff King <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help