Skip to content

Parse the to_str result strictly in Integer() with exception: false - #10114

Open
taxor03 wants to merge 1 commit into
jruby:jruby-10.0from
taxor03:fix-integer-to-str-exception-10.0
Open

taxor03 wants to merge 1 commit into
jruby:jruby-10.0from
taxor03:fix-integer-to-str-exception-10.0

Conversation

@taxor03

@taxor03 taxor03 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Fixes #10107.

For an object that converts with to_str, TypeConverter.convertToInteger parsed the String with the 4-argument ConvertBytes.byteListToInum(runtime, bytes, base, badcheck) and passed exception as badcheck. With exception: false that turned off the check for trailing characters, so Integer(o, exception: false) returned 1 for "1---", 12 for "12abc" and 0 for "0x", "0.0" and "". It now calls RubyNumeric.str2inum(runtime, str, base, true, exception), the same call as for a String argument a few lines above, like CRuby's rb_convert_to_integer, which calls rb_str_convert_to_inum(tmp, base, TRUE, raise_exception) in both places (object.c#L3456). Through str2inum, a to_str result in an ASCII-incompatible encoding now also raises Encoding::CompatibilityError, as it does for a String argument here and in CRuby.

This adds "parses the String returned by #to_str" and "swallows an error if the String can't be converted to an Integer" under "when passed exception: false" in spec/ruby/core/kernel/Integer_spec.rb. The second fails before this change and both pass after it, and pass on CRuby 4.0.9 and 3.4.12. Tested with spec/ruby/core/kernel/Integer_spec.rb and spec/ruby/core/string/to_i_spec.rb (also with -Xjit.threshold=0 and -X-C), test/mri/ruby/test_integer.rb and the fast RubySpec suite. No new failures.

The commit cherry-picks cleanly onto master, and the new examples pass there with the default parser and with -Xparser.prism.

@kares kares added this to the JRuby 10.0.9.0 milestone Nov 18, 2026

@kares kares 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.

👍 nice find

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