diff options
author | Benoit Daloze <[email protected]> | 2022-12-20 18:21:43 +0100 |
---|---|---|
committer | Benoit Daloze <[email protected]> | 2022-12-20 19:32:23 +0100 |
commit | 0efa36ac06a14a3e2e0aca395fee6530c790bdf1 () | |
tree | ea1b27a60f59fa7ce9e6f3efe5fd9934366c6f15 | |
parent | d557f17974384dde4ff2da021a1b38905a39bda2 (diff) |
Ensure Fiber storage is only accessed from the Fiber it belongs to
Notes: Merged: https://.com/ruby/ruby/pull/6972
-rw-r--r-- | cont.c | 10 | ||||
-rw-r--r-- | spec/ruby/core/fiber/storage_spec.rb | 26 | ||||
-rw-r--r-- | test/fiber/test_storage.rb | 12 |
3 files changed, 35 insertions, 13 deletions
@@ -2069,6 +2069,14 @@ fiber_storage_get(rb_fiber_t *fiber) return storage; } /** * call-seq: Fiber.current.storage -> hash (dup) * @@ -2077,6 +2085,7 @@ fiber_storage_get(rb_fiber_t *fiber) static VALUE rb_fiber_storage_get(VALUE self) { return rb_obj_dup(fiber_storage_get(fiber_ptr(self))); } @@ -2134,6 +2143,7 @@ rb_fiber_storage_set(VALUE self, VALUE value) "Fiber#storage= is experimental and may be removed in the future!"); } fiber_storage_validate(value); fiber_ptr(self)->cont.saved_ec.storage = rb_obj_dup(value); @@ -11,8 +11,7 @@ describe "Fiber.new(storage:)" do end it "creates a fiber with lazily initialized storage" do - fiber = Fiber.new(storage: nil) {} - fiber.storage.should == {} end it "creates a fiber by inheriting the storage of the parent fiber" do @@ -28,33 +27,34 @@ describe "Fiber.new(storage:)" do end end -describe "Fiber#storage" do ruby_version_is "3.2" do it "can clear the storage of the fiber" do - fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage } - fiber.storage = nil fiber.resume.should == {} end it "can set the storage of the fiber" do - fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage } - fiber.storage = {life: 43} fiber.resume.should == {life: 43} end it "can't set the storage of the fiber to non-hash" do - fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage } - -> { fiber.storage = 42 }.should raise_error(TypeError) end it "can't set the storage of the fiber to a frozen hash" do - fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage } - -> { fiber.storage = {life: 43}.freeze }.should raise_error(FrozenError) end it "can't set the storage of the fiber to a hash with non-symbol keys" do - fiber = Fiber.new(storage: {life: 42}) { Fiber.current.storage } - -> { fiber.storage = {life: 43, Object.new => 44} }.should raise_error(TypeError) end end end @@ -41,6 +41,18 @@ class TestFiberStorage < Test::Unit::TestCase Warning[:experimental] = old end def test_inherited_storage Fiber.new(storage: {foo: :bar}) do f = Fiber.new do |