Skip to content

Commit f4ab861

Browse files
committed
[GR-18163] Don't accept integer as first parameter to String#rindex
PullRequest: truffleruby/3211
2 parents fc85fe7 + 503b7fb commit f4ab861

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Compatibility:
3333
* Do not call `IO#flush` dynamically from `IO#close` (#2594, @gogainda).
3434
* Implement `rb_str_new_static` for C extensions that use it (@aardvark179).
3535
* Rewrote `ArrayEachIteratorNode` and re-introduced `each` specs for MRI parity when mutating arrays whilst iterating, rather than crashing (#2587, @MattAlp)
36+
* Update `String#rindex` to only accept `Regexp` or objects convertable to `String` as the first parameter (#2608, @bjfish).
3637

3738
Performance:
3839

spec/ruby/core/string/rindex_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
require_relative 'fixtures/utf-8-encoding'
55

66
describe "String#rindex with object" do
7-
it "raises a TypeError if obj isn't a String, Integer or Regexp" do
7+
it "raises a TypeError if obj isn't a String or Regexp" do
88
not_supported_on :opal do
99
-> { "hello".rindex(:sym) }.should raise_error(TypeError)
1010
end
1111
-> { "hello".rindex(mock('x')) }.should raise_error(TypeError)
1212
end
1313

14+
it "raises a TypeError if obj is an Integer" do
15+
-> { "hello".rindex(42) }.should raise_error(TypeError)
16+
end
17+
1418
it "doesn't try to convert obj to an integer via to_int" do
1519
obj = mock('x')
1620
obj.should_not_receive(:to_int)

src/main/ruby/truffleruby/core/string.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,23 +1087,7 @@ def rindex(sub, finish=undefined)
10871087

10881088
byte_finish = Primitive.string_byte_index_from_char_index(self, finish)
10891089

1090-
case sub
1091-
when Integer
1092-
if finish == size
1093-
return nil if finish == 0
1094-
end
1095-
1096-
begin
1097-
str = sub.chr
1098-
rescue RangeError
1099-
return nil
1100-
end
1101-
1102-
if byte_index = Primitive.find_string_reverse(self, str, byte_finish)
1103-
return Primitive.string_byte_character_index(self, byte_index)
1104-
end
1105-
1106-
when Regexp
1090+
if Primitive.object_kind_of?(sub, Regexp)
11071091
Primitive.encoding_ensure_compatible self, sub
11081092

11091093
match_data = Truffle::RegexpOperations.search_region(sub, self, 0, byte_finish, false, true)

0 commit comments

Comments
 (0)