Skip to content

Commit

Permalink
Fixed rindex calling into mrb_equal bug
Browse files Browse the repository at this point in the history
Fixed by Alex Snaps and reported by Mathieu Leduc-Hamel,
both from shopify.com.  Thank you!
  • Loading branch information
matz committed Nov 15, 2016
1 parent 242b219 commit 1f554ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -868,13 +868,16 @@ static mrb_value
mrb_ary_rindex_m(mrb_state *mrb, mrb_value self)
{
mrb_value obj;
mrb_int i;
mrb_int i, len;

mrb_get_args(mrb, "o", &obj);
for (i = RARRAY_LEN(self) - 1; i >= 0; i--) {
if (mrb_equal(mrb, RARRAY_PTR(self)[i], obj)) {
return mrb_fixnum_value(i);
}
if (i > (len = RARRAY_LEN(self))) {
i = len;
}
}
return mrb_nil_value();
}
Expand Down
12 changes: 12 additions & 0 deletions test/t/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,15 @@
ary.each {|p| h[p.class] += 1}
assert_equal({Array=>200}, h)
end

assert("Array#rindex") do
class Sneaky
def ==(*)
$a.clear
$a.replace([1])
false
end
end
$a = [2, 3, 4, 5, 6, 7, 8, 9, 10, Sneaky.new]
assert_equal 0, $a.rindex(1)
end

0 comments on commit 1f554ff

Please sign in to comment.