diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2019-07-22 12:02:23 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2019-07-22 19:43:32 +0200 |
commit | 0414982ffd5391f2acccfc7481b3100cc181b20d (patch) | |
tree | d11237d599b28c09a9892da3e022ad8b72a8e0a1 /lib | |
parent | 34032528a7f92ab98e566c4852574c5b044c76c0 (diff) | |
download | psych-0414982ffd5391f2acccfc7481b3100cc181b20d.zip |
Deduplicate hash keys if they're strings
Diffstat (limited to 'lib')
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index c265acb..49447e1 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -336,7 +336,7 @@ module Psych SHOVEL = '<<' def revive_hash hash, o o.children.each_slice(2) { |k,v| - key = accept(k) + key = deduplicate(accept(k)) val = accept(v) if key == SHOVEL && k.tag != "tag:yaml.org,2002:str" @@ -368,6 +368,28 @@ module Psych hash end + if String.method_defined?(:-@) + def deduplicate key + if key.is_a?(String) + # It is important to untaint the string, otherwise it won't + # be deduplicated into and fstring, but simply frozen. + -(key.untaint) + else + key + end + end + else + def deduplicate key + if key.is_a?(String) + # Deduplication is not supported by this implementation, + # but we emulate it's side effects + key.untaint.freeze + else + key + end + end + end + def merge_key hash, key, val end |