ruby-changes:26934
From: marcandre <ko1@a...>
Date: Wed, 30 Jan 2013 15:00:35 +0900 (JST)
Subject: [ruby-changes:26934] marcandRe: r38986 (trunk): * array.c (rb_ary_bsearch): Raise TypeError on bad return from block
marcandre 2013-01-30 15:00:24 +0900 (Wed, 30 Jan 2013) New Revision: 38986 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38986 Log: * array.c (rb_ary_bsearch): Raise TypeError on bad return from block * range.c (range_bsearch): ditto * test/ruby/test_array.rb (class): Test for above * test/ruby/test_range.rb (class): ditto Modified files: trunk/ChangeLog trunk/array.c trunk/range.c trunk/test/ruby/test_array.rb trunk/test/ruby/test_range.rb Index: array.c =================================================================== --- array.c (revision 38985) +++ array.c (revision 38986) @@ -2462,7 +2462,9 @@ rb_ary_bsearch(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L2462 } } else { - smaller = RTEST(v); + rb_raise(rb_eTypeError, "wrong argument type %s" + "(must respond be numeric, true, false or nil)", + rb_obj_classname(v)); } if (smaller) { high = mid; Index: ChangeLog =================================================================== --- ChangeLog (revision 38985) +++ ChangeLog (revision 38986) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jan 30 15:00:05 2013 Marc-Andre Lafortune <ruby-core@m...> + + * array.c (rb_ary_bsearch): Raise TypeError on bad return from block + + * range.c (range_bsearch): ditto + + * test/ruby/test_array.rb (class): Test for above + + * test/ruby/test_range.rb (class): ditto + Wed Jan 30 14:46:28 2013 Marc-Andre Lafortune <ruby-core@m...> * range.c: Restrict bsearch to integers [#7728] Index: range.c =================================================================== --- range.c (revision 38985) +++ range.c (revision 38986) @@ -595,7 +595,9 @@ range_bsearch(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L595 smaller = cmp < 0; \ } \ else { \ - smaller = RTEST(v); \ + rb_raise(rb_eTypeError, "wrong argument type %s" \ + "(must respond be numeric, true, false or nil)", \ + rb_obj_classname(v)); \ } \ } while (0) Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 38985) +++ test/ruby/test_array.rb (revision 38986) @@ -2249,6 +2249,13 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2249 assert_raise(ArgumentError) { a.rotate!(1, 1) } end + def test_bsearch_typechecks_return_values + assert_raise(TypeError) do + [1, 2, 42, 100, 666].bsearch{ "not ok" } + end + assert_equal [1, 2, 42, 100, 666].bsearch{}, [1, 2, 42, 100, 666].bsearch{false} + end + def test_bsearch_with_no_block enum = [1, 2, 42, 100, 666].bsearch assert_nil enum.size @@ -2276,9 +2283,4 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2283 assert_include([4, 7], a.bsearch {|x| (2**100).coerce((1 - x / 4) * (2**100)).first }) end - - def test_bsearch_undefined - a = [0, 4, 7, 10, 12] - assert_equal(nil, a.bsearch {|x| "foo" }) # undefined behavior - end end Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 38985) +++ test/ruby/test_range.rb (revision 38986) @@ -357,6 +357,13 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L357 assert_equal 42, (1..42).each.size end + def test_bsearch_typechecks_return_values + assert_raise(TypeError) do + (1..42).bsearch{ "not ok" } + end + assert_equal (1..42).bsearch{}, (1..42).bsearch{false} + end + def test_bsearch_with_no_block enum = (42...666).bsearch assert_nil enum.size -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/