diff --git a/hash.c b/hash.c index fc38f2f0ab..3f278e0c03 100644 --- a/hash.c +++ b/hash.c @@ -1587,11 +1587,15 @@ VALUE 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); } diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index 6aaeddc9d4..03ebd38f1f 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -280,6 +280,24 @@ class TestHash < Test::Unit::TestCase 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