[前][次][番号順一覧][スレッド一覧]

ruby-changes:65616

From: Kenichi <ko1@a...>
Date: Mon, 22 Mar 2021 14:11:10 +0900 (JST)
Subject: [ruby-changes:65616] 7d3fdfb27d (master): Hash#transform_values! ensures receiver modifiable in block [Bug #17736]

https://git.ruby-lang.org/ruby.git/commit/?id=7d3fdfb27d

From 7d3fdfb27dac456827b004d9e66a44b15f8cd762 Mon Sep 17 00:00:00 2001
From: Kenichi Kamiya <kachick1@g...>
Date: Mon, 22 Mar 2021 14:10:52 +0900
Subject: Hash#transform_values! ensures receiver modifiable in block [Bug
 #17736]

---
 hash.c                 | 1 +
 test/ruby/test_hash.rb | 9 +++++++++
 2 files changed, 10 insertions(+)

diff --git a/hash.c b/hash.c
index 2cfdbad..2711168 100644
--- a/hash.c
+++ b/hash.c
@@ -3303,6 +3303,7 @@ transform_values_foreach_replace(st_data_t *key, st_data_t *value, st_data_t arg https://github.com/ruby/ruby/blob/trunk/hash.c#L3303
 {
     VALUE new_value = rb_yield((VALUE)*value);
     VALUE hash = (VALUE)argp;
+    rb_hash_modify(hash);
     RB_OBJ_WRITE(hash, value, new_value);
     return ST_CONTINUE;
 }
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index a6acbc6..bd94b40 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -1803,6 +1803,15 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1803
     x = @cls[a: 1, b: 2, c: 3]
     y = x.transform_values!.with_index {|v, i| "#{v}.#{i}" }
     assert_equal(%w(1.0  2.1  3.2), y.values_at(:a, :b, :c))
+
+    x = @cls[a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10]
+    assert_raise(FrozenError) do
+      x.transform_values!() do |v|
+        x.freeze if v == 2
+        v.succ
+      end
+    end
+    assert_equal(@cls[a: 2, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10], x)
   end
 
   def test_broken_hash_value
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]