summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-13 09:04:32 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-13 09:04:32 +0000
commit48de2ea5f9b9067779acb0f7f76e5f879f2b42c0 (patch)
tree8fc7b7396059745163a8d4c224c82647da9345e5
parent75d25ede15e664ee0048ebc4e84e9e34838c2313 (diff)
range.c: prohibit `(1..nil)`
Now endless range can be created by either a literal `(1..)` or explicit range creation `Range.new(1, nil)`. [Bug #14845] This change is intended for "early failure"; for example, `(1..var).to_a` causes out of memory if `var` is inadvertently nil. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63646 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--compile.c4
-rw-r--r--parse.y12
-rw-r--r--range.c14
-rw-r--r--test/ruby/test_range.rb46
4 files changed, 35 insertions, 41 deletions
diff --git a/compile.c b/compile.c
index 21971830ea7..2caf92ecf1f 100644
--- a/compile.c
+++ b/compile.c
@@ -7188,10 +7188,10 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
case NODE_DOT2:
case NODE_DOT3:{
int excl = type == NODE_DOT3;
- VALUE flag = INT2FIX(excl);
const NODE *b = node->nd_beg;
const NODE *e = node->nd_end;
- if (number_literal_p(b) && number_literal_p(e)) {
+ VALUE flag = INT2FIX(excl | (e ? 0 : 2));
+ if (number_literal_p(b) && e && number_literal_p(e)) {
if (!popped) {
VALUE val = rb_range_new(b->nd_lit, e->nd_lit, excl);
iseq_add_mark_object_compile_time(iseq, val);
diff --git a/parse.y b/parse.y
index b1ddc1bf1ca..53ad745ec9d 100644
--- a/parse.y
+++ b/parse.y
@@ -1916,24 +1916,16 @@ arg : lhs '=' arg_rhs
| arg tDOT2
{
/*%%%*/
- YYLTYPE loc;
- loc.beg_pos = @2.end_pos;
- loc.end_pos = @2.end_pos;
-
value_expr($1);
- $$ = NEW_DOT2($1, new_nil(&loc), &@$);
+ $$ = NEW_DOT2($1, 0, &@$);
/*% %*/
/*% ripper: dot2!($1, Qnil) %*/
}
| arg tDOT3
{
/*%%%*/
- YYLTYPE loc;
- loc.beg_pos = @2.end_pos;
- loc.end_pos = @2.end_pos;
-
value_expr($1);
- $$ = NEW_DOT3($1, new_nil(&loc), &@$);
+ $$ = NEW_DOT3($1, 0, &@$);
/*% %*/
/*% ripper: dot3!($1, Qnil) %*/
}
diff --git a/range.c b/range.c
index dcae2b2397e..c9521dca5cb 100644
--- a/range.c
+++ b/range.c
@@ -37,7 +37,7 @@ static VALUE r_cover_p(VALUE, VALUE, VALUE, VALUE);
static void
range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
{
- if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && !NIL_P(end)) {
+ if ((!FIXNUM_P(beg) || !FIXNUM_P(end)) && end != Qundef) {
VALUE v;
v = rb_funcall(beg, id_cmp, 1, end);
@@ -47,15 +47,16 @@ range_init(VALUE range, VALUE beg, VALUE end, VALUE exclude_end)
RANGE_SET_EXCL(range, exclude_end);
RANGE_SET_BEG(range, beg);
- RANGE_SET_END(range, end);
+ RANGE_SET_END(range, end == Qundef ? Qnil : end);
}
VALUE
-rb_range_new(VALUE beg, VALUE end, int exclude_end)
+rb_range_new(VALUE beg, VALUE end, int flag)
{
VALUE range = rb_obj_alloc(rb_cRange);
- range_init(range, beg, end, RBOOL(exclude_end));
+ if (flag & 2) end = Qundef;
+ range_init(range, beg, end, RBOOL(flag & 1));
return range;
}
@@ -85,6 +86,7 @@ range_initialize(int argc, VALUE *argv, VALUE range)
rb_scan_args(argc, argv, "21", &beg, &end, &flags);
range_modify(range);
+ if (NIL_P(end)) end = Qundef;
range_init(range, beg, end, RBOOL(RTEST(flags)));
return Qnil;
}
@@ -1339,7 +1341,7 @@ range_dumper(VALUE range)
rb_ivar_set(v, id_excl, RANGE_EXCL(range));
rb_ivar_set(v, id_beg, RANGE_BEG(range));
- rb_ivar_set(v, id_end, RANGE_END(range));
+ if (!NIL_P(RANGE_END(range))) rb_ivar_set(v, id_end, RANGE_END(range));
return v;
}
@@ -1354,7 +1356,7 @@ range_loader(VALUE range, VALUE obj)
range_modify(range);
beg = rb_ivar_get(obj, id_beg);
- end = rb_ivar_get(obj, id_end);
+ end = rb_ivar_lookup(obj, id_end, Qundef);
excl = rb_ivar_get(obj, id_excl);
if (!NIL_P(excl)) {
range_init(range, beg, end, RBOOL(RTEST(excl)));
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index c98b130456d..8237e698810 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -13,8 +13,8 @@ class TestRange < Test::Unit::TestCase
assert_raise(ArgumentError) { (1.."3") }
- assert_equal((0..nil), Range.new(0, nil, false))
- assert_equal((0...nil), Range.new(0, nil, true))
+ assert_equal((0..), Range.new(0, nil, false))
+ assert_equal((0...), Range.new(0, nil, true))
obj = Object.new
def obj.<=>(other)
@@ -161,15 +161,15 @@ class TestRange < Test::Unit::TestCase
assert_not_equal(r, (1..2))
assert_not_equal(r, (0..2))
assert_not_equal(r, (0...1))
- assert_not_equal(r, (0..nil))
+ assert_not_equal(r, (0..))
subclass = Class.new(Range)
assert_equal(r, subclass.new(0,1))
- r = (0..nil)
+ r = (0..)
assert_equal(r, r)
- assert_equal(r, (0..nil))
+ assert_equal(r, (0..))
assert_not_equal(r, 0)
- assert_not_equal(r, (0...nil))
+ assert_not_equal(r, (0...))
subclass = Class.new(Range)
assert_equal(r, subclass.new(0,nil))
end
@@ -185,11 +185,11 @@ class TestRange < Test::Unit::TestCase
subclass = Class.new(Range)
assert_operator(r, :eql?, subclass.new(0,1))
- r = (0..nil)
+ r = (0..)
assert_operator(r, :eql?, r)
- assert_operator(r, :eql?, 0..nil)
+ assert_operator(r, :eql?, 0..)
assert_not_operator(r, :eql?, 0)
- assert_not_operator(r, :eql?, 0...nil)
+ assert_not_operator(r, :eql?, 0...)
subclass = Class.new(Range)
assert_operator(r, :eql?, subclass.new(0,nil))
end
@@ -198,8 +198,8 @@ class TestRange < Test::Unit::TestCase
assert_kind_of(Integer, (0..1).hash)
assert_equal((0..1).hash, (0..1).hash)
assert_not_equal((0..1).hash, (0...1).hash)
- assert_equal((0..nil).hash, (0..nil).hash)
- assert_not_equal((0..nil).hash, (0...nil).hash)
+ assert_equal((0..).hash, (0..).hash)
+ assert_not_equal((0..).hash, (0...).hash)
end
def test_step
@@ -380,9 +380,9 @@ class TestRange < Test::Unit::TestCase
assert_equal(0, (0..1).begin)
assert_equal(1, (0..1).end)
assert_equal(1, (0...1).end)
- assert_equal(0, (0..nil).begin)
- assert_equal(nil, (0..nil).end)
- assert_equal(nil, (0...nil).end)
+ assert_equal(0, (0..).begin)
+ assert_equal(nil, (0..).end)
+ assert_equal(nil, (0...).end)
end
def test_first_last
@@ -402,17 +402,17 @@ class TestRange < Test::Unit::TestCase
assert_equal("c", ("a"..."c").last)
assert_equal(0, (2...0).last)
- assert_equal([0, 1, 2], (0..nil).first(3))
- assert_equal(0, (0..nil).first)
- assert_equal("a", ("a"..nil).first)
+ assert_equal([0, 1, 2], (0..).first(3))
+ assert_equal(0, (0..).first)
+ assert_equal("a", ("a"..).first)
# XXX: How should (0...).last(3) behave?
end
def test_to_s
assert_equal("0..1", (0..1).to_s)
assert_equal("0...1", (0...1).to_s)
- assert_equal("0..", (0..nil).to_s)
- assert_equal("0...", (0...nil).to_s)
+ assert_equal("0..", (0..).to_s)
+ assert_equal("0...", (0...).to_s)
bug11767 = '[ruby-core:71811] [Bug #11767]'
assert_predicate(("0".taint.."1").to_s, :tainted?, bug11767)
@@ -423,8 +423,8 @@ class TestRange < Test::Unit::TestCase
def test_inspect
assert_equal("0..1", (0..1).inspect)
assert_equal("0...1", (0...1).inspect)
- assert_equal("0..", (0..nil).inspect)
- assert_equal("0...", (0...nil).inspect)
+ assert_equal("0..", (0..).inspect)
+ assert_equal("0...", (0...).inspect)
bug11767 = '[ruby-core:71811] [Bug #11767]'
assert_predicate(("0".taint.."1").inspect, :tainted?, bug11767)
@@ -435,8 +435,8 @@ class TestRange < Test::Unit::TestCase
def test_eqq
assert_operator(0..10, :===, 5)
assert_not_operator(0..10, :===, 11)
- assert_operator(5..nil, :===, 11)
- assert_not_operator(5..nil, :===, 0)
+ assert_operator(5.., :===, 11)
+ assert_not_operator(5.., :===, 0)
end
def test_eqq_time