From ab6efa5be2f5a2fb1b5a05a90f398b27ea52617a Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 8 Jan 2014 06:55:24 +0000 Subject: [PATCH] object.c: hash value from objid with salt * hash.c (rb_objid_hash): return hash value from object ID with a salt, extract from rb_any_hash(). * object.c (rb_obj_hash): return same value as rb_any_hash(). fix r44125. [ruby-core:59638] [Bug #9381] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ hash.c | 15 ++++++++++++--- object.c | 3 ++- test/ruby/test_hash.rb | 21 +++++++++++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index a0a2ba0eb7..89304de430 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Jan 8 15:55:21 2014 Nobuyoshi Nakada + + * hash.c (rb_objid_hash): return hash value from object ID with a + salt, extract from rb_any_hash(). + + * object.c (rb_obj_hash): return same value as rb_any_hash(). + fix r44125. [ruby-core:59638] [Bug #9381] + Wed Jan 8 13:12:41 2014 Nobuyoshi Nakada * lib/timeout.rb (Timeout::ExitException.catch): pass arguments diff --git a/hash.c b/hash.c index 0eca4b99a4..51ba38dd58 100644 --- a/hash.c +++ b/hash.c @@ -122,6 +122,8 @@ rb_hash(VALUE obj) return hval; } +st_index_t rb_objid_hash(st_index_t index); + static st_index_t rb_any_hash(VALUE a) { @@ -130,9 +132,7 @@ rb_any_hash(VALUE a) if (SPECIAL_CONST_P(a)) { if (a == Qundef) return 0; - hnum = rb_hash_start((st_index_t)a); - hnum = rb_hash_uint(hnum, (st_index_t)rb_any_hash); - hnum = rb_hash_end(hnum); + hnum = rb_objid_hash((st_index_t)a); } else if (BUILTIN_TYPE(a) == T_STRING) { hnum = rb_str_hash(a); @@ -145,6 +145,15 @@ rb_any_hash(VALUE a) return (st_index_t)RSHIFT(hnum, 1); } +st_index_t +rb_objid_hash(st_index_t index) +{ + st_index_t hnum = rb_hash_start(index); + hnum = rb_hash_uint(hnum, (st_index_t)rb_any_hash); + hnum = rb_hash_end(hnum); + return hnum; +} + static const struct st_hash_type objhash = { rb_any_cmp, rb_any_hash, diff --git a/object.c b/object.c index e54285d8fc..1398201c83 100644 --- a/object.c +++ b/object.c @@ -161,6 +161,7 @@ rb_obj_equal(VALUE obj1, VALUE obj2) VALUE rb_obj_hash(VALUE obj) { + st_index_t rb_objid_hash(st_index_t index); VALUE oid = rb_obj_id(obj); #if SIZEOF_LONG == SIZEOF_VOIDP st_index_t index = NUM2LONG(oid); @@ -169,7 +170,7 @@ rb_obj_hash(VALUE obj) #else # error not supported #endif - st_index_t h = rb_hash_end(rb_hash_start(index)); + st_index_t h = rb_objid_hash(index); return LONG2FIX(h); } diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 70c04424db..3861fde0a4 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -1215,6 +1215,27 @@ class TestHash < Test::Unit::TestCase assert_no_memory_leak([], prepare, code, bug9187) end + def test_wrapper_of_special_const + bug9381 = '[ruby-core:59638] [Bug #9381]' + + wrapper = Class.new do + def initialize(obj) + @obj = obj + end + + def hash + @obj.hash + end + + def eql?(other) + @obj.eql?(other) + end + end + + hash = {5 => bug9381} + assert_equal(bug9381, hash[wrapper.new(5)]) + end + class TestSubHash < TestHash class SubHash < Hash def reject(*)