Skip to content

Raise ArgumentError instead of overflowing in large case mapping - #10123

Open
zev wants to merge 1 commit into
jruby:jruby-10.0from
zev:fix-casemap-size-overflow-10.0
Open

zev wants to merge 1 commit into
jruby:jruby-10.0from
zev:fix-casemap-size-overflow-10.0

Conversation

@zev

@zev zev commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #10122, as suggested in its review. Rebased onto jruby-10.0 now that #10122 is merged.

This ports ruby/ruby#20006 ("Fix an integer overflow in case conversion of strings").

Problem

StringSupport.caseMap added up the result length in an int, and sized each buffer as remaining * bufferCount + 20 in int arithmetic. When the result went over 2 GB, the total wrapped negative and case conversion failed with a raw Java exception:

("ΐ" * 400_000_000).upcase   # 800 MB in, 2.4 GB out
# => Java::JavaLang::NegativeArraySizeException: -1894967296

The buffer size calculation can wrap too. With 1.5 GB of input, the second buffer would be remaining * 2 + 20. On the JVM none of this corrupts memory; it fails with the wrong exception.

Fix

  • Add up the total in a long. Raise ArgumentError, "string size too big", the same error CRuby raises, as soon as it exceeds Helpers.MAX_ARRAY_SIZE.
  • Compute each buffer's size in a long and cap it at Helpers.MAX_ARRAY_SIZE.

CRuby also rejects up front any source longer than INT_MAX - 20, because one C buffer has to hold the whole source plus headroom. JRuby doesn't need that limit. jcodings' caseMap stops at the end of the buffer and the loop continues from pp.value, so a capped buffer is safe. The only real limit is that the result has to fit in one Java array.

Verification

Run with -J-Xmx16g on the rebased branch (jcodings 1.0.67):

Input Before After
"ΐ" * 400_000_000 (800 MB → 2.4 GB) NegativeArraySizeException ArgumentError: string size too big
"ΐ" * 750_000_000 (1.5 GB → 4.5 GB) NegativeArraySizeException ArgumentError: string size too big
"ΐ" * 300_000_000 (600 MB → 1.8 GB) correct correct (1,800,000,000 bytes, ends checked)
  • String/Symbol case-mapping specs: 9 files, 196 examples, 0 failures.
  • MRI test_case_mapping.rb / test_case_comprehensive.rb: only the two Shift_JIS :ascii tests fail. They're already in test/mri/excludes, and master fixed them in 9fba130.

There's no regression spec, because reaching the limit needs several GB of heap. CRuby's change has no test for it either.

🤖 Generated with Claude Code

@headius headius added this to the JRuby 10.0.9.0 milestone Nov 20, 2026
@headius

headius commented Nov 20, 2026

Copy link
Copy Markdown
Member

#10122 has been merged!

StringSupport.caseMap added up the result length in an int and sized
each buffer as remaining * buffer count + 20 in int arithmetic. For a
result over 2 GB the total wrapped negative and the case conversion
failed with a raw Java exception:

    ("ΐ" * 400_000_000).upcase
    # => Java::JavaLang::NegativeArraySizeException: -1894967296

Add up the total in a long and raise ArgumentError "string size too big"
once it exceeds the largest Java array, as CRuby does since
ruby/ruby#20006. Compute each buffer size in a long and cap it at the
largest array; caseMap stops at the end of the buffer and the loop
continues from there, so no up-front limit on the source is needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev
zev force-pushed the fix-casemap-size-overflow-10.0 branch from 7cb6b9e to 054c51e Compare November 20, 2026 06:41
@zev
zev marked this pull request as ready for review November 20, 2026 06:44
@zev

zev commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@headius rebased onto jruby-10.0 now that #10122 is in. It's a single commit touching only StringSupport.java, and it's ready for review.

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