Skip to content

Fix out-of-bounds write in sharp s case mapping - #72

Closed
zev wants to merge 1 commit into
jruby:masterfrom
zev:fix-sharp-s-case-map-overflow
Closed

zev wants to merge 1 commit into
jruby:masterfrom
zev:fix-sharp-s-case-map-overflow

Conversation

@zev

@zev zev commented Nov 19, 2026 •

Copy link
Copy Markdown

Port of ruby/ruby@d582f8f, the CRuby fix for GHSA-34m4-m9fh-gqp5.

The caseMap methods of the ISO-8859-x and Windows-125x encodings upcase or fold the sharp s (0xDF) to two bytes (SS / ss). The loop only checked for one byte of room (toP < toEnd), so the second byte could be written past the end of the output buffer. In CRuby this is a heap overflow. On the JVM it throws ArrayIndexOutOfBoundsException, which JRuby exposes as:

("\xDF".b * 21).force_encoding("ISO-8859-1").upcase
# => Java::JavaLang::ArrayIndexOutOfBoundsException: Index 41 out of bounds for length 41

The same applies to swapcase, downcase(:fold) and casecmp?, in all 14 encodings that expand the sharp s. This PR changes the loop condition to toEnd - toP > 1 in those same 14 files, as CRuby did. ISO-8859-5/7 and Windows-1251/1253 never expand a byte, so they are unchanged.

Tests

TestCaseMap#testSharpSExpansionDoesNotOverflow runs 1 to 64 sharp s through upcase, fold, swapcase and an a-prefixed upcase for each affected encoding. It sizes and refills buffers the way CRuby and JRuby do (remaining input + 20). It fails with the AIOOBE above without the fix. mvn test passes: 45 tests, 0 failures (1 skipped, already skipped before this change).

Related JRuby fix, for a separate bug where case mapping that outgrows the first buffer returns an empty string: jruby/jruby#10122.

🤖 Generated with Claude Code

The caseMap methods of ISO-8859-x and Windows-125x map the sharp s
(0xDF) to two bytes but checked for only one byte of room in the output
buffer, so they wrote one byte past it and threw
ArrayIndexOutOfBoundsException, e.g. when upcasing 21 sharp s
characters with an input length + 20 buffer. Check for two bytes.

Port of ruby/ruby@d582f8f
(GHSA-34m4-m9fh-gqp5).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev

zev commented Nov 19, 2026

Copy link
Copy Markdown
Author

@headius while this is less of an issue for JRuby, this does help clean it up a bit.

@headius

headius commented Nov 19, 2026

Copy link
Copy Markdown
Member

Looks like we both took on the same project. I believe this was addressed by my agent in #73 and has been released in 1.0.66.

@headius headius closed this Nov 19, 2026
@headius headius added this to the invalid/duplicate/wontfix milestone Nov 19, 2026
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.

2 participants