aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2019-12-23 15:02:14 +0900
committerKoichi Sasada <ko1@atdot.net>2019-12-23 15:04:56 +0900
commita96f8cecc2488126d7298ea304da8bad3dde1792 (patch)
treeceed4c3656acf2969c9ff1d39752da23347b02de
parent672a61b97fe0cdb256611c707e7cc69856208467 (diff)
downloadruby-a96f8cecc2488126d7298ea304da8bad3dde1792.tar.gz
ObjectSpace._id2ref should check liveness.
objspace->id_to_obj_tbl can contain died objects because of lazy sweep, so that it should check liveness.
-rw-r--r--gc.c3
-rw-r--r--test/ruby/test_objectspace.rb20
2 files changed, 22 insertions, 1 deletions
diff --git a/gc.c b/gc.c
index ab7662b534..f6e75e8c78 100644
--- a/gc.c
+++ b/gc.c
@@ -3644,7 +3644,8 @@ id2ref(VALUE objid)
}
}
- 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 c352b75b70..243e9f681c 100644
--- a/test/ruby/test_objectspace.rb
+++ b/test/ruby/test_objectspace.rb
@@ -35,6 +35,26 @@ End
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)