Skip to content

[fix] Encoding.compatible?: check when swapping args#9345

Merged
headius merged 1 commit intojruby:jruby-10.0from
kares:enc-compatible-10
Mar 31, 2026
Merged

[fix] Encoding.compatible?: check when swapping args#9345
headius merged 1 commit intojruby:jruby-10.0from
kares:enc-compatible-10

Conversation

@kares
Copy link
Copy Markdown
Member

@kares kares commented Mar 29, 2026

When the first argument is not a String (e.g. Regexp) and the second is, areCompatible check swaps objects so that String is first.

However, JRuby also swapped encoding values, which is wrong — CRuby's enc_compatible_latter (encoding.c) only swaps the object/isstr/idx references but keeps enc1/enc2 derived from the original idx values before the swap, so they retain the original argument order.

This caused Encoding.compatible?(regexp_eucjp, string_utf8) to return EUC-JP instead of UTF-8 when the string was ASCII-only.

When the first argument is not a String (e.g. Regexp) and the second
is, areCompatible check swaps objects so that String is first.

However, JRuby also swapped encoding values, which is wrong — CRuby's
enc_compatible_latter (encoding.c) only swaps the object/isstr/idx
references but keeps enc1/enc2 derived from the original idx values
before the swap, so they retain the original argument order.

This caused Encoding.compatible?(regexp_eucjp, string_utf8) to
return EUC-JP instead of UTF-8 when the string was ASCII-only.
@kares kares added this to the JRuby 10.0.5.0 milestone Mar 29, 2026
@headius headius merged commit a780c41 into jruby:jruby-10.0 Mar 31, 2026
206 of 207 checks passed
@kares kares deleted the enc-compatible-10 branch April 7, 2026 08:06
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