summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-30 05:46:48 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-01-30 05:46:48 +0000
commit00d6bb5108209329996c8062ce3e947489c38ea7 (patch)
tree392b5d06203017b4a1da0c365c62337ccee6e213
parent39048aca7592a4096380f45f421bad578b61efb8 (diff)
* range.c: Restrict bsearch to integers [#7728]
* test/ruby/test_range.rb: Test for above git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--range.c16
-rw-r--r--test/ruby/test_range.rb10
3 files changed, 27 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 70c16e6..ce4c97e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Jan 30 14:46:28 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * range.c: Restrict bsearch to integers [#7728]
+
+ * test/ruby/test_range.rb: Test for above
+
Wed Jan 30 14:10:52 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* array.c (rb_ary_bsearch): Return enumerator if no block [#7725]
diff --git a/range.c b/range.c
index 12436ce..3944951 100644
--- a/range.c
+++ b/range.c
@@ -20,7 +20,7 @@
#include <math.h>
VALUE rb_cRange;
-static ID id_cmp, id_succ, id_beg, id_end, id_excl;
+static ID id_cmp, id_succ, id_beg, id_end, id_excl, id_integer_p;
#define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
#define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
@@ -497,6 +497,12 @@ double_as_int64(double d) {
}
#endif
+static int
+is_integer_p(VALUE v) {
+ VALUE is_int = rb_check_funcall(v, id_integer_p, 0, 0);
+ return RTEST(is_int) && is_int != Qundef;
+}
+
/*
* call-seq:
* rng.bsearch {|obj| block } -> value
@@ -635,10 +641,9 @@ range_bsearch(VALUE range)
BSEARCH(int64_as_double_to_num);
}
#endif
- else if (!NIL_P(rb_check_to_integer(beg, "to_int")) &&
- !NIL_P(rb_check_to_integer(end, "to_int"))) {
- VALUE low = beg;
- VALUE high = end;
+ else if (is_integer_p(beg) && is_integer_p(end)) {
+ VALUE low = rb_to_int(beg);
+ VALUE high = rb_to_int(end);
VALUE mid, org_high;
RETURN_ENUMERATOR(range, 0, 0);
if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1));
@@ -1299,6 +1304,7 @@ Init_Range(void)
id_beg = rb_intern("begin");
id_end = rb_intern("end");
id_excl = rb_intern("excl");
+ id_integer_p = rb_intern("integer?");
rb_cRange = rb_struct_define_without_accessor(
"Range", rb_cObject, range_alloc,
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index c8867cf..d859969 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -1,6 +1,7 @@
require 'test/unit'
require 'delegate'
require 'timeout'
+require 'bigdecimal'
class TestRange < Test::Unit::TestCase
def test_range_string
@@ -362,6 +363,15 @@ class TestRange < Test::Unit::TestCase
assert_equal 200, enum.each{|x| x >= 200 }
end
+ def test_bsearch_for_other_numerics
+ assert_raise(TypeError) {
+ (Rational(-1,2)..Rational(9,4)).bsearch
+ }
+ assert_raise(TypeError) {
+ (BigDecimal('0.5')..BigDecimal('2.25')).bsearch
+ }
+ end
+
def test_bsearch_for_fixnum
ary = [3, 4, 7, 9, 12]
assert_equal(0, (0...ary.size).bsearch {|i| ary[i] >= 2 })