Skip to content

Fix out-of-bounds read in gets with a limit inside a truncated character - #233

Open
aristocratte wants to merge 1 commit into
ruby:masterfrom
aristocratte:fix-gets-limit-truncated-char
Open

aristocratte wants to merge 1 commit into
ruby:masterfrom
aristocratte:fix-gets-limit-truncated-char

Conversation

@aristocratte

Copy link
Copy Markdown

StringIO#gets, #readlines and #each_line with 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_head then 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, and pos ends up larger than the string size.

io = StringIO.new("abcdef".b.freeze)
io.set_encoding(Encoding::UTF_32LE)
io.gets(5)
io.pos # => 8, should be 6

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::ArrayIndexOutOfBoundsException for the same input.

This change clamps the adjusted end to the end of the string in both implementations and adds a test.

Tested locally:

  • CRuby 4.0.3: the new test fails before the change (pos is 8) and passes after; rake test passes (109 tests).
  • JRuby 10.1.3.0: the new test errors before the change and passes after; rake test passes (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

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant