1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Add test to demonstrate issue deserializing hash with ivar

Currently the elements of a hash are revived before any ivar values.
This causes an issue when the `[]=` method references an instance
variable.
This commit is contained in:
Jordan Owens 2019-01-16 00:29:12 -05:00 committed by Hiroshi SHIBATA
parent a53ab897c3
commit 0016edbead

View file

@ -6,6 +6,18 @@ module Psych
class X < Hash
end
class HashWithIvar < Hash
def initialize
@keys = []
super
end
def []=(k, v)
@keys << k
super(k, v)
end
end
class HashWithCustomInit < Hash
attr_reader :obj
def initialize(obj)
@ -24,6 +36,14 @@ module Psych
@hash = { :a => 'b' }
end
def test_hash_with_ivar
t1 = HashWithIvar.new
t1[:foo] = :bar
t2 = Psych.load(Psych.dump(t1))
assert_equal t1, t2
assert_cycle t1
end
def test_referenced_hash_with_ivar
a = [1,2,3,4,5]
t1 = [HashWithCustomInit.new(a)]