ruby-changes:32046
From: nobu <ko1@a...>
Date: Tue, 10 Dec 2013 23:44:58 +0900 (JST)
Subject: [ruby-changes:32046] nobu:r44125 (trunk): array.c, hash.c: add salt
nobu 2013-12-10 23:44:51 +0900 (Tue, 10 Dec 2013) New Revision: 44125 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44125 Log: array.c, hash.c: add salt * array.c (rb_ary_hash): add salt to differentiate false and empty array. [ruby-core:58993] [Bug #9231] * hash.c (rb_any_hash, rb_hash_hash): ditto. Modified files: trunk/ChangeLog trunk/array.c trunk/hash.c trunk/test/ruby/test_array.rb trunk/test/ruby/test_hash.rb Index: array.c =================================================================== --- array.c (revision 44124) +++ array.c (revision 44125) @@ -3792,6 +3792,7 @@ rb_ary_hash(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L3792 VALUE n; h = rb_hash_start(RARRAY_LEN(ary)); + h = rb_hash_uint(h, (st_index_t)rb_ary_hash); for (i=0; i<RARRAY_LEN(ary); i++) { n = rb_hash(RARRAY_AREF(ary, i)); h = rb_hash_uint(h, NUM2LONG(n)); Index: ChangeLog =================================================================== --- ChangeLog (revision 44124) +++ ChangeLog (revision 44125) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Dec 10 23:44:42 2013 Nobuyoshi Nakada <nobu@r...> + + * array.c (rb_ary_hash): add salt to differentiate false and empty + array. [ruby-core:58993] [Bug #9231] + + * hash.c (rb_any_hash, rb_hash_hash): ditto. + Tue Dec 10 18:16:09 2013 SHIBATA Hiroshi <shibata.hiroshi@g...> * man/ruby.1: [DOC] Use www.ruby-toolbox.com instead of RAA. Index: hash.c =================================================================== --- hash.c (revision 44124) +++ hash.c (revision 44125) @@ -118,7 +118,9 @@ rb_any_hash(VALUE a) https://github.com/ruby/ruby/blob/trunk/hash.c#L118 if (SPECIAL_CONST_P(a)) { if (a == Qundef) return 0; - hnum = rb_hash_end(rb_hash_start((st_index_t)a)); + hnum = rb_hash_start((st_index_t)a); + hnum = rb_hash_uint(hnum, (st_index_t)rb_any_hash); + hnum = rb_hash_end(hnum); } else if (BUILTIN_TYPE(a) == T_STRING) { hnum = rb_str_hash(a); @@ -1972,10 +1974,12 @@ hash_i(VALUE key, VALUE val, VALUE arg) https://github.com/ruby/ruby/blob/trunk/hash.c#L1974 static VALUE rb_hash_hash(VALUE hash) { - st_index_t hval = RHASH_SIZE(hash); - - if (!hval) return INT2FIX(0); - rb_hash_foreach(hash, hash_i, (VALUE)&hval); + st_index_t size = RHASH_SIZE(hash); + st_index_t hval = rb_hash_start(size); + hval = rb_hash_uint(hval, (st_index_t)rb_hash_hash); + if (size) { + rb_hash_foreach(hash, hash_i, (VALUE)&hval); + } hval = rb_hash_end(hval); return INT2FIX(hval); } Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 44124) +++ test/ruby/test_array.rb (revision 44125) @@ -909,6 +909,8 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L909 a3 = @cls[ 'dog', 'cat' ] assert_equal(a1.hash, a2.hash) assert_not_equal(a1.hash, a3.hash) + bug9231 = '[ruby-core:58993] [Bug #9231]' + assert_not_equal(false.hash, @cls[].hash, bug9231) end def test_include? Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 44124) +++ test/ruby/test_hash.rb (revision 44125) @@ -883,6 +883,8 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L883 h = @cls[1=>2] h.shift assert_equal({}.hash, h.hash, '[ruby-core:38650]') + bug9231 = '[ruby-core:58993] [Bug #9231]' + assert_not_equal(0, @cls[].hash, bug9231) end def test_update2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/