Fix NoMethodError when encoding with invalid: :replace option#9182
Merged
headius merged 2 commits intojruby:masterfrom Jan 30, 2026
Merged
Conversation
When encoding a string with `invalid: :replace` and `replace:` options but without `undef: :replace`, JRuby was incorrectly throwing NoMethodError instead of Encoding::UndefinedConversionError. The bug occurred because when `ecopts` hash exists (due to `:replace` option) but doesn't contain a `:fallback` key, the code was setting `fallbackFunc` to `AREF_FALLBACK` with a nil `fallback` value. When an undefined conversion was encountered, it tried to call `nil.[]()` which raised NoMethodError. The fix adds a check for `!fallback.isNil()` before assigning the fallback function, ensuring that the fallback is only used when explicitly provided. Fixes jruby#9009
d756e3f to
d66b260
Compare
Member
|
Nice thanks! I think this can go into 10.0.3.0 if @enebo approves. |
Member
|
@khasinski One small nitpick... could you see if there's any equivalent spec under spec/ruby? If it's there and tagged (spec/tags/ruby) we can remove the tag. If it's not there, you can port your code over. This will improve ruby/spec for all impls. |
headius
requested changes
Jan 29, 2026
Move test for UndefinedConversionError behavior from JRuby-specific test suite to shared ruby/spec. Also add spec for the basic case of encoding without options raising UndefinedConversionError. Related to jruby#9009
Member
|
Great thanks! I'll check if that existing spec is currently excluded. |
Member
|
I tested your branch and did not see any additional specs pass, so I'll merge what we have here. If you would like to work on those other failures, that would be great! |
headius
approved these changes
Jan 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NoMethodError: undefined method '[]' for nilwhen encoding a string withinvalid: :replaceoption but withoutundef: :replaceEncoding::UndefinedConversionErrormatching MRI behaviorProblem
When calling:
JRuby 10.0.2.0 threw:
Expected (MRI behavior):
Root Cause
In
EncodingUtils.transcodeLoop(), whenecoptshash exists (due to:replaceoption) but doesn't contain a:fallbackkey, the code was settingfallbackFunc = AREF_FALLBACKwithfallback = nil. When an undefined conversion was encountered, it tried to callnil.[]()which raisedNoMethodError.Fix
Added a check for
!fallback.isNil()before assigning the fallback function, ensuring that the fallback is only used when explicitly provided via the:fallbackoption.Test plan
Fixes #9009