Skip to content

Commit

Permalink
Force this buffer to BINARY encoding
Browse files Browse the repository at this point in the history
This buffer is used by a few tests that wrap non-IO objects with
a duck-typed wrapper, as seen here:

  def z.read(size
    @buf ||= TestZlib.create_gzip_stream("hello")
    @buf.slice!(0, size)
  end

If this buffer is not encoded as BINARY, then the slice! call may
treat the requested length as a character count. With the buffer
using the default UTF-8 this may lead to binary bytes being
misinterpreted as prefix bytes, and the resulting slice will
contain more bytes than were requested.

This is bad form for an IO#read implementation; it should never
return more bytes than requested. A second bug in IOInputStream
blindly attempts to copy all those bytes into its buffer, resulting
in an ArrayIndexOutOfBoundsException (bug #1 in jruby#8391).

The fix here opens the StringIO using encoding: "BINARY" to ensure
the slice length can only be interpreted as a byte count, so the
read slicing never returns more than the number of bytes requested.

Partial fix for jruby#8391.
  • Loading branch information
headius committed Oct 30, 2024
1 parent 9a2a1df commit 9198c6a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion test/jruby/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_gzip_reader_check_corrupted_trailer
end

def self.create_gzip_stream(string)
s = StringIO.new
s = StringIO.new(encoding: "BINARY")
Zlib::GzipWriter.wrap(s) { |io|
io.write("hello")
}
Expand Down

0 comments on commit 9198c6a

Please sign in to comment.