Repository navigation
Fix out-of-bounds read in gets with a limit inside a truncated character - #233
Open
aristocratte wants to merge 1 commit into
Open
aristocratte wants to merge 1 commit into
aristocratte wants to merge 1 commit into
Conversation
…acter
When the limit falls inside a truncated last character of a
fixed-width encoding (UTF-32, for example), the adjusted end of the
line was placed after the end of the string. The separator search
then read a few bytes past the buffer, and `pos` was left beyond the
end of the string. On JRuby the same case raised
`ArrayIndexOutOfBoundsException`.
io = StringIO.new("abcdef".b.freeze)
io.set_encoding(Encoding::UTF_32LE)
io.gets(5)
io.pos # => 8, should be 6
Clamp the adjusted end to the end of the string, in both the C and
the Java implementations. `readlines` and `each_line` go through the
same code.
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.
StringIO#gets,#readlinesand#each_linewith a byte limit can place the end of the line after the end of the string.It happens when the stream encoding is fixed-width (UTF-32, for example), the string ends with a truncated character, and the limit falls inside that truncated character.
rb_enc_right_char_headthen advances by a whole character and returns a pointer past the end of the buffer. The separator search reads a few bytes beyond the string, andposends up larger than the string size.The returned string is not affected, it was already clamped to the string size.
The Java implementation has the same logic and raises
Java::JavaLang::ArrayIndexOutOfBoundsExceptionfor the same input.This change clamps the adjusted end to the end of the string in both implementations and adds a test.
Tested locally:
posis 8) and passes after;rake testpasses (109 tests).rake testpasses (106 tests, 1 omission).This was first reported privately, where it was triaged as a regular bug and I was asked to send the patch here.
🤖 Generated with Claude Code