Repository navigation
Do not write through a shared buffer in GzipReader#read and #readpartial - #190
Open
aristocratte wants to merge 1 commit into
Open
aristocratte wants to merge 1 commit into
aristocratte wants to merge 1 commit into
Conversation
zstream_shift_buffer() fills the buffer given by the caller with
rb_str_resize() followed by memcpy(). rb_str_resize() returns at once
for a string that shares its memory when the length does not change,
and it only clears the coderange when the string shrinks.
So when the buffer still shared its memory with a copy (String#dup, a
frozen copy, the copy a Hash makes of its key), the next read of the
same length overwrote that copy, and the buffer kept the coderange of
its previous contents.
gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("A" * 1000 + "B" * 1000)))
buf = "".dup
copy = gz.readpartial(1000, buf).dup
gz.readpartial(1000, buf)
copy[0, 4] # => "BBBB"
Call rb_str_modify() on the buffer before writing into it.
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Zlib::GzipReader#read(len, buf)and#readpartial(len, buf)overwrite earlier copies of the buffer.A long String and its
dupshare one allocation until one of them is modified.zstream_shift_buffer()fills the buffer withrb_str_resize()followed bymemcpy(), andrb_str_resize()returns at once for a shared string when the length does not change. The write then goes through the shared memory, so a frozen copy, or the copy a Hash made of its key, is overwritten too.rb_str_resize()also clears the coderange only when the string shrinks, so the buffer kept the coderange of its previous contents: after reading"\xFF" * 10into a buffer that was"".dup,buf.ascii_only?wastrue.It happens when the buffer holds a string too long to be embedded (623 bytes or more on Ruby 4.0), the next read returns the same number of bytes, and those bytes are already decompressed in the internal buffer. When GzipReader has to read from the underlying IO during the call, that IO's
readpartialunshares the buffer first.This change calls
rb_str_modify()on the buffer before writing into it, as the other methods that read into a buffer do.Reproduced with zlib 3.2.4 (this repository), 3.2.3 (Ruby 4.0.9 and 3.4.12) and, for
readpartial, 3.1.2 (Ruby 3.3.13). The two new tests fail without the change; with it the suite passes (114 tests, 608 assertions) on Ruby 4.0.3.Reported through HackerOne, where it was handled as a regular bug.
🤖 Generated with Claude Code