Skip to content

Commit dada83b

Browse files
nobutmm1
authored andcommitted
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 ruby#9381] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 8e92ab1 commit dada83b

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

hash.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ rb_hash(VALUE obj)
111111
return hval;
112112
}
113113

114+
st_index_t rb_objid_hash(st_index_t index);
115+
114116
static st_index_t
115117
rb_any_hash(VALUE a)
116118
{
@@ -119,9 +121,7 @@ rb_any_hash(VALUE a)
119121

120122
if (SPECIAL_CONST_P(a)) {
121123
if (a == Qundef) return 0;
122-
hnum = rb_hash_start((st_index_t)a);
123-
hnum = rb_hash_uint(hnum, (st_index_t)rb_any_hash);
124-
hnum = rb_hash_end(hnum);
124+
hnum = rb_objid_hash((st_index_t)a);
125125
}
126126
else if (BUILTIN_TYPE(a) == T_STRING) {
127127
hnum = rb_str_hash(a);
@@ -134,6 +134,15 @@ rb_any_hash(VALUE a)
134134
return (st_index_t)RSHIFT(hnum, 1);
135135
}
136136

137+
st_index_t
138+
rb_objid_hash(st_index_t index)
139+
{
140+
st_index_t hnum = rb_hash_start(index);
141+
hnum = rb_hash_uint(hnum, (st_index_t)rb_any_hash);
142+
hnum = rb_hash_end(hnum);
143+
return hnum;
144+
}
145+
137146
static const struct st_hash_type objhash = {
138147
rb_any_cmp,
139148
rb_any_hash,

object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ rb_obj_equal(VALUE obj1, VALUE obj2)
161161
VALUE
162162
rb_obj_hash(VALUE obj)
163163
{
164+
st_index_t rb_objid_hash(st_index_t index);
164165
VALUE oid = rb_obj_id(obj);
165166
#if SIZEOF_LONG == SIZEOF_VOIDP
166167
st_index_t index = NUM2LONG(oid);
@@ -169,7 +170,7 @@ rb_obj_hash(VALUE obj)
169170
#else
170171
# error not supported
171172
#endif
172-
st_index_t h = rb_hash_end(rb_hash_start(index));
173+
st_index_t h = rb_objid_hash(index);
173174
return LONG2FIX(h);
174175
}
175176

test/ruby/test_hash.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,27 @@ def hash
12171217
assert_no_memory_leak([], prepare, code, bug9187)
12181218
end
12191219

1220+
def test_wrapper_of_special_const
1221+
bug9381 = '[ruby-core:59638] [Bug #9381]'
1222+
1223+
wrapper = Class.new do
1224+
def initialize(obj)
1225+
@obj = obj
1226+
end
1227+
1228+
def hash
1229+
@obj.hash
1230+
end
1231+
1232+
def eql?(other)
1233+
@obj.eql?(other)
1234+
end
1235+
end
1236+
1237+
hash = {5 => bug9381}
1238+
assert_equal(bug9381, hash[wrapper.new(5)])
1239+
end
1240+
12201241
class TestSubHash < TestHash
12211242
class SubHash < Hash
12221243
end

0 commit comments

Comments
 (0)