Check reentry after comparison conversion

Comparison result conversion can invoke Ruby code before reentry is
checked.
This commit is contained in:
Nobuyoshi Nakada 2026-11-07 02:41:08 +09:00
parent c5943e4975
commit 338c55e07e
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2026-11-06 18:40:23 +00:00
2 changed files with 18 additions and 5 deletions

10
enum.c
View File

@ -1956,9 +1956,10 @@ struct nmin_data {
int by: 1; /* min_by if 1 */
};
static VALUE
cmpint_reenter_check(struct nmin_data *data, VALUE val)
static int
cmpint_reenter_check(struct nmin_data *data, VALUE cmp, VALUE a, VALUE b)
{
int val = rb_cmpint(cmp, a, b);
if (RBASIC(data->buf)->klass) {
rb_raise(rb_eRuntimeError, "%s%s reentered",
data->rev ? "max" : "min",
@ -1972,7 +1973,7 @@ nmin_cmp(const void *ap, const void *bp, void *_data)
{
struct nmin_data *data = (struct nmin_data *)_data;
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
#define rb_cmpint(cmp, a, b) rb_cmpint(cmpint_reenter_check(data, (cmp)), a, b)
#define rb_cmpint(cmp, a, b) cmpint_reenter_check(data, cmp, a, b)
return OPTIMIZED_CMP(a, b);
#undef rb_cmpint
}
@ -1983,8 +1984,7 @@ nmin_block_cmp(const void *ap, const void *bp, void *_data)
struct nmin_data *data = (struct nmin_data *)_data;
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
VALUE cmp = rb_yield_values(2, a, b);
cmpint_reenter_check(data, cmp);
return rb_cmpint(cmp, a, b);
return cmpint_reenter_check(data, cmp, a, b);
}
static void

View File

@ -918,6 +918,19 @@ class TestEnumerable < Test::Unit::TestCase
assert_operator(i, :<=, 5, "infinite loop")
c.call
end
assert_raise_with_message(RuntimeError, /reentered/) do
c = nil
cmp = Object.new
cmp.define_singleton_method(:>) do |other|
callcc {|c2| c ||= c2}
true
end
o = Object.new
o.define_singleton_method(:<=>) {|x| cmp}
[o, o].min(1)
c.call
end
end
def test_reverse_each