Skip to content

Fix String case mapping returning empty string when output grows - #10122

Merged
headius merged 1 commit into
jruby:jruby-10.0from
zev:fix-casemap-multibuffer-size
Nov 20, 2026
Merged

headius merged 1 commit into
jruby:jruby-10.0from
zev:fix-casemap-multibuffer-size

Conversation

@zev

@zev zev commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

StringSupport.caseMap collects output in several buffers when the mapped string outgrows the first one (input length + 20). When it joins them, the result ByteList is created with new ByteList(tgtLen). That constructor only reserves capacity, and the code never set the size, so the result came back empty:

("ΐ" * 5).upcase          # => ""  (expected "Ϊ́Ϊ́Ϊ́Ϊ́Ϊ́")
("İ" * 30).downcase       # => ""
("ΐ" * 30).to_sym.upcase  # => :""

This affects String and Symbol upcase, downcase (including :fold), swapcase and capitalize whose output grows by more than about 20 bytes, in UTF-8 or any other encoding. The bug has been there since 22d9d36 (2018). The fix is one line: set the real size after copying the buffers in.

Specs

I added a "returns the whole result when it is much longer than self" example to each of these:

  • spec/ruby/core/string/{upcase,downcase,swapcase,capitalize}_spec.rb, plus a downcase(:fold) case
  • spec/ruby/core/symbol/{upcase,downcase,swapcase,capitalize}_spec.rb

Results:

  • JRuby 10.0 (3.4.12) without the fix: all 9 new examples fail.
  • With the fix: 0 failures (9 files, 196 examples, including casecmp_spec.rb).
  • CRuby 4.0.7 and CRuby 3.3.12 both pass. 3.3 is the oldest version ruby/spec targets, so no version guard is needed.

Related

I found this while checking JRuby against GHSA-34m4-m9fh-gqp5, the CRuby sharp s heap overflow. The jcodings side of that is in jruby/jcodings#73 (released in 1.0.66), which superseded jruby/jcodings#72.

🤖 Generated with Claude Code

@zev
zev marked this pull request as ready for review November 19, 2026 17:25
@zev

zev commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@headius this was found while looking at jruby/jcodings#72 .

@zev

zev commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@headius I'd like your call on a hardening question this raised. It's not needed for the fix here.

Background. StringSupport.caseMap sizes each output buffer as remaining input * buffer count + CASE_MAPPING_ADDITIONAL_LENGTH, where that constant is 20 (StringSupport.java:2902, used at :2920). This copies CRuby's rb_str_casemap (string.c, CASE_MAPPING_ADDITIONAL_LENGTH 20). It then calls enc.caseMap(...) repeatedly until pp.value reaches the end of the input.

The question. The loop relies on every call consuming at least one input character. After jruby/jcodings#72 (the GHSA-34m4-m9fh-gqp5 port), the single-byte encodings won't start a character unless at least 2 bytes are free (toEnd - toP > 1). If a buffer ever had fewer than 2 bytes free, caseMap would return 0 without moving pp.value, and this loop would spin forever. Other encodings have their own minimum room needed per character. Encoding.caseMap has no Javadoc, so none of this is written down anywhere.

That can't happen today: every buffer is at least 20 bytes, and this is the only caller of Encoding.caseMap in JRuby. CRuby has the same implicit assumption and no guard. It would only matter if the sizing changes, or if another jcodings user (TruffleRuby?) calls caseMap with small buffers.

Options:

  1. Leave it as is. This matches CRuby, and the case can't be reached with the current sizing.
  2. Add a no-progress check in StringSupport.caseMap. If a call doesn't advance pp.value, raise an error instead of looping. That's one comparison per buffer, and it turns a future sizing mistake into an error instead of a hang. It's the same class of problem as the OptExactInfo.concatStr loop in Use patched Joni and JCodings releases for regexp behavior #10121.
  3. Add Javadoc to Encoding.caseMap in jcodings. It would say that the method may stop before consuming all input, that callers must call it again from pp.value, and that the buffer needs room for the longest single mapping. This could go alongside option 2.

I'm happy to add 2 and/or 3 to this PR and jcodings#72, or leave it.

@headius

headius commented Nov 19, 2026

Copy link
Copy Markdown
Member

I think our work crossed in the air; I just fixed and released jcodings and joni separately to address differences with CRuby related to the recent security patches.

I've got another agent updating jcodings and joni and making sure our handling of these conditions matches CRuby, and then we'll reevaluate this to see what's left.

@zev

zev commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Great to hear! Let me know if I should close this or if you want to reevaluate it after your done on your side.

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex review: The production fix is sound: new ByteList(tgtLen) reserves capacity but starts with logical size zero, and this multi-buffer branch copies into the backing array without updating that size. Setting the final size fixes the empty result.

Change requested: move this regression from test/jruby/test_string.rb into the shared spec/ruby/core/string specs (for example, the relevant upcase/capitalize/downcase specs). These assertions cover public Ruby behavior and the expected results pass on CRuby, so this belongs in the cross-implementation suite. Keep the expansion cases that exercise the multi-buffer path; if the description claims Symbol methods too, cover those public methods or narrow the claim.

The no-progress concern raised in the discussion is not demonstrated with current buffer sizing: JRuby's sole caller adds 20 bytes of headroom. I would leave that guard out absent a reachable zero-progress case. Separately, CRuby #20006's large-output integer-overflow guard is outside this fix; JRuby's StringSupport.caseMap still uses unchecked int accumulation and merits a distinct bounded-safety follow-up.

Comment thread test/jruby/test_string.rb Resolved
@headius

headius commented Nov 19, 2026

Copy link
Copy Markdown
Member

Great to hear! Let me know if I should close this or if you want to reevaluate it after your done on your side.

I had my agent review this in light of the other work and you still have a good fix here. Move the specs and we'll be good.

@headius

headius commented Nov 19, 2026

Copy link
Copy Markdown
Member

Actually... could you make this against jruby-10.0 branch please? It's such a trivial fix for a valid bug.

@headius headius added this to the JRuby 10.0.9.0 milestone Nov 19, 2026
StringSupport.caseMap gathers output in several buffers when the mapped
string outgrows the first one (input length + 20). The combined ByteList
was created with new ByteList(tgtLen), which only reserves capacity, and
its size was never set, so the result came back empty:

    ("ΐ" * 5).upcase # => "" (expected "Ϊ́" * 5)

Set the real size after copying the buffers in. This affects String and
Symbol upcase, downcase, swapcase and capitalize; add specs for each.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev
zev force-pushed the fix-casemap-multibuffer-size branch from 1cfe3cd to 2c747d1 Compare November 19, 2026 23:32
@zev
zev changed the base branch from master to jruby-10.0 November 19, 2026 23:32
@zev

zev commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Updated per review:

  • Retargeted to jruby-10.0. It's one commit on top of the current branch.
  • Moved the regression into the shared specs: String upcase/downcase (plus :fold)/swapcase/capitalize, and the same four for Symbol, since Symbol goes through the same path (all 4 Symbol examples fail without the fix). I removed the test_string.rb test.
  • On the version guard: the new specs pass on CRuby 3.3.12 and 4.0.7, and 3.3 is the oldest version ruby/spec targets, so I didn't add a guard.
  • I left out the no-progress check, as you suggested. I'll open the CRuby #20006 integer-overflow guard as a separate PR.

@headius
headius merged commit e0b9c70 into jruby:jruby-10.0 Nov 20, 2026
251 of 254 checks passed
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