ruby-changes:53157
From: normal <ko1@a...>
Date: Fri, 26 Oct 2018 14:32:53 +0900 (JST)
Subject: [ruby-changes:53157] normal:r65371 (trunk): hash.c: aset deduplicates un-tainted string
normal 2018-10-26 14:32:47 +0900 (Fri, 26 Oct 2018) New Revision: 65371 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65371 Log: hash.c: aset deduplicates un-tainted string We revisit [Bug #9188] since st.c is much improved since then, and benchmarks against so_k_nucleotide seem to indicate little or no performance change compared to before. [ruby-core:89555] [Feature #15251] From: Anmol Chopra <chopraanmol1@g...> Modified files: trunk/hash.c trunk/test/ruby/test_hash.rb Index: test/ruby/test_hash.rb =================================================================== --- test/ruby/test_hash.rb (revision 65370) +++ test/ruby/test_hash.rb (revision 65371) @@ -280,6 +280,24 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L280 assert_same a.keys[0], b.keys[0] end + def test_ASET_fstring_non_literal_key + underscore = "_" + non_literal_strings = Proc.new{ ["abc#{underscore}def", "abc" * 5, "abc" + "def", "" << "ghi" << "jkl"] } + + a, b = {}, {} + non_literal_strings.call.each do |string| + assert_equal 1, a[string] = 1 + end + + non_literal_strings.call.each do |string| + assert_equal 1, b[string] = 1 + end + + [a.keys, b.keys].transpose.each do |key_a, key_b| + assert_same key_a, key_b + end + end + def test_hash_aset_fstring_identity h = {}.compare_by_identity h['abc'] = 1 Index: hash.c =================================================================== --- hash.c (revision 65370) +++ hash.c (revision 65371) @@ -1587,11 +1587,15 @@ VALUE https://github.com/ruby/ruby/blob/trunk/hash.c#L1587 rb_hash_key_str(VALUE key) { VALUE k; + int not_tainted = !RB_OBJ_TAINTED(key); - if (!RB_OBJ_TAINTED(key) && + if (not_tainted && (k = fstring_existing_str(key)) != Qnil) { return k; } + else if(not_tainted) { + return rb_fstring(key); + } else { return rb_str_new_frozen(key); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/