summaryrefslogtreecommitdiff
path: root/range.c
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-13 09:04:32 (GMT)
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-13 09:04:32 (GMT)
commit48de2ea5f9b9067779acb0f7f76e5f879f2b42c0 (patch)
tree8fc7b7396059745163a8d4c224c82647da9345e5 /range.c
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
Diffstat (limited to 'range.c')
-rw-r--r--range.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/range.c b/range.c
index dcae2b2..c9521dc 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)));