Permalink
Browse files

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]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36391 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent e81c1d5 commit 8d6add973ebcb3b4c1efbfaf07786550a3e219af @nobu nobu committed Jul 14, 2012
Showing with 14 additions and 1 deletion.
  1. +5 −0 ChangeLog
  2. +6 −1 hash.c
  3. +3 −0 test/ruby/test_hash.rb
View
@@ -1,3 +1,8 @@
+Sat Jul 14 17:08:13 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* lib/test/unit.rb (Test::Unit::Runner#_run_parallel): use
View
7 hash.c
@@ -393,8 +393,13 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
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:
View
@@ -712,6 +712,9 @@ def test_intialize_wrong_arguments
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

0 comments on commit 8d6add9

Please sign in to comment.