Skip to content

Commit 7753557

Browse files
committed
[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
1 parent a46309d commit 7753557

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

set.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ set_i_initialize(int argc, VALUE *argv, VALUE set)
494494
rb_check_arity(argc, 0, 1);
495495

496496
if (argc > 0 && (other = argv[0]) != Qnil) {
497+
ID id_size = rb_intern("size");
498+
if (rb_respond_to(other, id_size)) {
499+
VALUE size = rb_funcall(other, id_size, 0);
500+
if (RB_TYPE_P(size, T_FLOAT) && RFLOAT_VALUE(size) == INFINITY) {
501+
rb_raise(rb_eArgError, "cannot initialize Set from a enumerable with infinite size");
502+
}
503+
}
504+
497505
if (RB_TYPE_P(other, T_ARRAY)) {
498506
long i;
499507
int block_given = rb_block_given_p();

test/ruby/test_set.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def test_s_new
8181

8282
s = Set.new(ary) { |o| o * 2 }
8383
assert_equal([2,4,6], s.sort)
84+
85+
assert_raise(ArgumentError) {
86+
Set.new((1..))
87+
}
88+
assert_raise(ArgumentError) {
89+
Set.new((1..), &:succ)
90+
}
91+
assert_raise(ArgumentError) {
92+
Set.new(1.upto(Float::INFINITY))
93+
}
8494
end
8595

8696
def test_clone

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy