ruby-changes:24340
From: nobu <ko1@a...>
Date: Sat, 14 Jul 2012 17:08:27 +0900 (JST)
Subject: [ruby-changes:24340] nobu:r36391 (trunk): hash.c: raise on invalid input
nobu 2012-07-14 17:08:15 +0900 (Sat, 14 Jul 2012) New Revision: 36391 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=36391 Log: hash.c: raise on invalid input * hash.c (rb_hash_s_create): raise an exception, when input elements are not one or two elements arrays. [ruby-core:39945] [Bug #5406] Modified files: trunk/ChangeLog trunk/hash.c trunk/test/ruby/test_hash.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 36390) +++ ChangeLog (revision 36391) @@ -1,3 +1,8 @@ +Sat Jul 14 17:08:13 2012 Nobuyoshi Nakada <nobu@r...> + + * hash.c (rb_hash_s_create): raise an exception, when input elements + are not one or two elements arrays. [ruby-core:39945] [Bug #5406] + Sat Jul 14 16:16:48 2012 Nobuyoshi Nakada <nobu@r...> * lib/test/unit.rb (Test::Unit::Runner#_run_parallel): use Index: hash.c =================================================================== --- hash.c (revision 36390) +++ hash.c (revision 36391) @@ -393,8 +393,13 @@ VALUE v = rb_check_array_type(RARRAY_PTR(tmp)[i]); VALUE key, val = Qnil; - if (NIL_P(v)) continue; + if (NIL_P(v)) { + rb_raise(rb_eArgError, "wrong element type (expected array)"); + } switch (RARRAY_LEN(v)) { + default: + rb_raise(rb_eArgError, "invalid number of elements (%ld for 1..2)", + RARRAY_LEN(v)); case 2: val = RARRAY_PTR(v)[1]; case 1: Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 36390) +++ test/ruby/test_hash.rb (revision 36391) @@ -712,6 +712,9 @@ def test_create assert_equal({1=>2, 3=>4}, Hash[[[1,2],[3,4]]]) assert_raise(ArgumentError) { Hash[0, 1, 2] } + bug5406 = '[ruby-core:39945]' + assert_raise(ArgumentError, bug5406) { Hash[[[1, 2], 3]] } + assert_raise(ArgumentError, bug5406) { Hash[[[1, 2], [3, 4, 5]]] } assert_equal({1=>2, 3=>4}, Hash[1,2,3,4]) o = Object.new def o.to_hash() {1=>2} end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/