From d4020dd5faf28486123853e7f00c36139fc07793 Mon Sep 17 00:00:00 2001 From: viralpraxis Date: Wed, 16 Jul 2025 01:59:22 +0400 Subject: [PATCH 1/2] [Bug #21513] Raise on converting endless range to set ref: https://bugs.ruby-lang.org/issues/21513 Before this patch, trying to convert endless range (e.g. `(1..)`) to set (using `to_set`) would hang --- set.c | 8 ++++++++ test/ruby/test_set.rb | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/set.c b/set.c index 55d0e626338af5..61f1fd8bc43d1b 100644 --- a/set.c +++ b/set.c @@ -505,6 +505,14 @@ set_i_initialize(int argc, VALUE *argv, VALUE set) } } else { + ID id_size = rb_intern("size"); + if (rb_obj_is_kind_of(other, rb_mEnumerable) && rb_respond_to(other, id_size)) { + VALUE size = rb_funcall(other, id_size, 0); + if (RB_TYPE_P(size, T_FLOAT) && RFLOAT_VALUE(size) == INFINITY) { + rb_raise(rb_eArgError, "cannot initialize Set from an object with infinite size"); + } + } + rb_block_call(other, enum_method_id(other), 0, 0, rb_block_given_p() ? set_initialize_with_block : set_initialize_without_block, set); diff --git a/test/ruby/test_set.rb b/test/ruby/test_set.rb index 87e1fd8d26e908..934a470c1e626f 100644 --- a/test/ruby/test_set.rb +++ b/test/ruby/test_set.rb @@ -81,6 +81,20 @@ def test_s_new s = Set.new(ary) { |o| o * 2 } assert_equal([2,4,6], s.sort) + + assert_raise(ArgumentError) { + Set.new((1..)) + } + assert_raise(ArgumentError) { + Set.new((1..), &:succ) + } + assert_raise(ArgumentError) { + Set.new(1.upto(Float::INFINITY)) + } + + assert_raise(ArgumentError) { + Set.new(Object.new) + } end def test_clone From 7ee127d2d19927432ba0ea421294dbba304e15ff Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Tue, 29 Jul 2025 11:56:11 +0200 Subject: [PATCH 2/2] Get rid of imemo_ast It has been marked as obsolete for a while and I see no reason to keep it. --- debug_counter.h | 1 - ext/objspace/objspace.c | 1 - imemo.c | 13 ------------- internal/imemo.h | 1 - yjit/src/cruby_bindings.inc.rs | 1 - zjit/src/cruby_bindings.inc.rs | 1 - 6 files changed, 18 deletions(-) diff --git a/debug_counter.h b/debug_counter.h index 9f0649e0ac8e33..8ffce66f0f7b16 100644 --- a/debug_counter.h +++ b/debug_counter.h @@ -305,7 +305,6 @@ RB_DEBUG_COUNTER(obj_imemo_ment) RB_DEBUG_COUNTER(obj_imemo_iseq) RB_DEBUG_COUNTER(obj_imemo_env) RB_DEBUG_COUNTER(obj_imemo_tmpbuf) -RB_DEBUG_COUNTER(obj_imemo_ast) RB_DEBUG_COUNTER(obj_imemo_cref) RB_DEBUG_COUNTER(obj_imemo_svar) RB_DEBUG_COUNTER(obj_imemo_throw_data) diff --git a/ext/objspace/objspace.c b/ext/objspace/objspace.c index 8476c9b74752d1..8b3045fda322c4 100644 --- a/ext/objspace/objspace.c +++ b/ext/objspace/objspace.c @@ -500,7 +500,6 @@ count_imemo_objects(int argc, VALUE *argv, VALUE self) INIT_IMEMO_TYPE_ID(imemo_ment); INIT_IMEMO_TYPE_ID(imemo_iseq); INIT_IMEMO_TYPE_ID(imemo_tmpbuf); - INIT_IMEMO_TYPE_ID(imemo_ast); INIT_IMEMO_TYPE_ID(imemo_callinfo); INIT_IMEMO_TYPE_ID(imemo_callcache); INIT_IMEMO_TYPE_ID(imemo_constcache); diff --git a/imemo.c b/imemo.c index 5f24e0230129db..7153689030a7b6 100644 --- a/imemo.c +++ b/imemo.c @@ -16,7 +16,6 @@ rb_imemo_name(enum imemo_type type) // put no default case to get a warning if an imemo type is missing switch (type) { #define IMEMO_NAME(x) case imemo_##x: return #x; - IMEMO_NAME(ast); IMEMO_NAME(callcache); IMEMO_NAME(callinfo); IMEMO_NAME(constcache); @@ -220,10 +219,6 @@ rb_imemo_memsize(VALUE obj) { size_t size = 0; switch (imemo_type(obj)) { - case imemo_ast: - rb_bug("imemo_ast is obsolete"); - - break; case imemo_callcache: break; case imemo_callinfo: @@ -336,10 +331,6 @@ void rb_imemo_mark_and_move(VALUE obj, bool reference_updating) { switch (imemo_type(obj)) { - case imemo_ast: - rb_bug("imemo_ast is obsolete"); - - break; case imemo_callcache: { /* cc is callcache. * @@ -600,10 +591,6 @@ void rb_imemo_free(VALUE obj) { switch (imemo_type(obj)) { - case imemo_ast: - rb_bug("imemo_ast is obsolete"); - - break; case imemo_callcache: RB_DEBUG_COUNTER_INC(obj_imemo_callcache); diff --git a/internal/imemo.h b/internal/imemo.h index dcea997ae803a7..0ad00fe6b79d99 100644 --- a/internal/imemo.h +++ b/internal/imemo.h @@ -37,7 +37,6 @@ enum imemo_type { imemo_ment = 6, imemo_iseq = 7, imemo_tmpbuf = 8, - imemo_ast = 9, // Obsolete due to the universal parser imemo_callinfo = 10, imemo_callcache = 11, imemo_constcache = 12, diff --git a/yjit/src/cruby_bindings.inc.rs b/yjit/src/cruby_bindings.inc.rs index 8268b889199170..eeabbf594df9bb 100644 --- a/yjit/src/cruby_bindings.inc.rs +++ b/yjit/src/cruby_bindings.inc.rs @@ -405,7 +405,6 @@ pub const imemo_memo: imemo_type = 5; pub const imemo_ment: imemo_type = 6; pub const imemo_iseq: imemo_type = 7; pub const imemo_tmpbuf: imemo_type = 8; -pub const imemo_ast: imemo_type = 9; pub const imemo_callinfo: imemo_type = 10; pub const imemo_callcache: imemo_type = 11; pub const imemo_constcache: imemo_type = 12; diff --git a/zjit/src/cruby_bindings.inc.rs b/zjit/src/cruby_bindings.inc.rs index 492de3a5dfae3a..268bae3dc4860b 100644 --- a/zjit/src/cruby_bindings.inc.rs +++ b/zjit/src/cruby_bindings.inc.rs @@ -222,7 +222,6 @@ pub const imemo_memo: imemo_type = 5; pub const imemo_ment: imemo_type = 6; pub const imemo_iseq: imemo_type = 7; pub const imemo_tmpbuf: imemo_type = 8; -pub const imemo_ast: imemo_type = 9; pub const imemo_callinfo: imemo_type = 10; pub const imemo_callcache: imemo_type = 11; pub const imemo_constcache: imemo_type = 12;