kpumuk commented on code in PR #3547:
URL: https://github.com/apache/thrift/pull/3547#discussion_r3314514950


##########
lib/rb/lib/thrift/transport/header_transport.rb:
##########
@@ -370,13 +385,48 @@ def parse_header_format(buf)
       payload = buf.read
       transforms.each do |transform_id|
         if transform_id == HeaderTransformID::ZLIB
-          payload = Zlib::Inflate.inflate(payload)
+          payload = bounded_inflate(payload)
         end
       end
 
       StringIO.new(payload)
     end
 
+    # Inflates +compressed+ with a running byte-count check against 
@max_decompressed_size.
+    # Raises TransportException::SIZE_LIMIT if the decompressed output would 
exceed the limit.
+    def bounded_inflate(compressed)
+      inflater = Zlib::Inflate.new
+      total = 0
+      chunks = []
+      offset = 0
+      begin
+        while offset < compressed.bytesize
+          out = inflater.inflate(compressed.byteslice(offset, 
ZLIB_INFLATE_CHUNK_SIZE))

Review Comment:
   There is a memory over-use here. I would recommend defining a `buffer = ''` 
variable outside of the loop, and the pass it here:
   
   
   ```suggestion
             out = inflater.inflate(compressed.byteslice(offset, 
ZLIB_INFLATE_CHUNK_SIZE), buffer:)
   ```
   
   Without it, uncompressing a large payload would require double memory.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to