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

ruby-changes:59402

From: Koichi <ko1@a...>
Date: Mon, 23 Dec 2019 15:05:28 +0900 (JST)
Subject: [ruby-changes:59402] a96f8cecc2 (master): ObjectSpace._id2ref should check liveness.

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

From a96f8cecc2488126d7298ea304da8bad3dde1792 Mon Sep 17 00:00:00 2001
From: Koichi Sasada <ko1@a...>
Date: Mon, 23 Dec 2019 15:02:14 +0900
Subject: ObjectSpace._id2ref should check liveness.

objspace->id_to_obj_tbl can contain died objects because of lazy
sweep, so that it should check liveness.

diff --git a/gc.c b/gc.c
index ab7662b..f6e75e8 100644
--- a/gc.c
+++ b/gc.c
@@ -3644,7 +3644,8 @@ id2ref(VALUE objid) https://github.com/ruby/ruby/blob/trunk/gc.c#L3644
         }
     }
 
-    if (st_lookup(objspace->id_to_obj_tbl, objid, &orig)) {
+    if (st_lookup(objspace->id_to_obj_tbl, objid, &orig) &&
+        is_live_object(objspace, orig)) {
         return orig;
     }
 
diff --git a/test/ruby/test_objectspace.rb b/test/ruby/test_objectspace.rb
index c352b75..243e9f6 100644
--- a/test/ruby/test_objectspace.rb
+++ b/test/ruby/test_objectspace.rb
@@ -35,6 +35,26 @@ End https://github.com/ruby/ruby/blob/trunk/test/ruby/test_objectspace.rb#L35
   deftest_id2ref(false)
   deftest_id2ref(nil)
 
+  def test_id2ref_liveness
+    assert_normal_exit <<-EOS
+      ids = []
+      10.times{
+        1_000.times{
+          ids << 'hello'.object_id
+        }
+        objs = ids.map{|id|
+          begin
+            ObjectSpace._id2ref(id)
+          rescue RangeError
+            nil
+          end
+        }
+        GC.start
+        objs.each{|e| e.inspect}
+      }
+    EOS
+  end
+
   def test_count_objects
     h = {}
     ObjectSpace.count_objects(h)
-- 
cgit v0.10.2


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

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