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

ruby-changes:69715

From: Samuel <ko1@a...>
Date: Fri, 12 Nov 2021 12:46:23 +0900 (JST)
Subject: [ruby-changes:69715] 98b442e013 (master): More immutability and locking tests.

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

From 98b442e013afbb450f1c946d86ed625c39ab3233 Mon Sep 17 00:00:00 2001
From: Samuel Williams <samuel.williams@o...>
Date: Fri, 22 Oct 2021 15:04:22 +1300
Subject: More immutability and locking tests.

---
 io_buffer.c                 | 11 +++++++----
 test/ruby/test_io_buffer.rb | 29 +++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/io_buffer.c b/io_buffer.c
index af05388036b..1acc942987f 100644
--- a/io_buffer.c
+++ b/io_buffer.c
@@ -417,7 +417,6 @@ rb_io_buffer_to_s(VALUE self) https://github.com/ruby/ruby/blob/trunk/io_buffer.c#L417
     }
 
     return rb_str_cat2(result, ">");
-
 }
 
 static VALUE
@@ -701,6 +700,13 @@ size_t rb_io_buffer_copy(VALUE self, VALUE source, size_t offset) https://github.com/ruby/ruby/blob/trunk/io_buffer.c#L700
     const void *source_base = NULL;
     size_t source_size = 0;
 
+    struct rb_io_buffer *data = NULL;
+    TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
+
+    if (data->flags & RB_IO_BUFFER_IMMUTABLE) {
+        rb_raise(rb_eRuntimeError, "Buffer is immutable!");
+    }
+
     if (RB_TYPE_P(source, T_STRING)) {
         RSTRING_GETMEM(source, source_base, source_size);
     }
@@ -708,9 +714,6 @@ size_t rb_io_buffer_copy(VALUE self, VALUE source, size_t offset) https://github.com/ruby/ruby/blob/trunk/io_buffer.c#L714
         rb_io_buffer_get_immutable(source, &source_base, &source_size);
     }
 
-    struct rb_io_buffer *data = NULL;
-    TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
-
     rb_io_buffer_validate(data, offset, source_size);
 
     memcpy((char*)data->base + offset, source_base, source_size);
diff --git a/test/ruby/test_io_buffer.rb b/test/ruby/test_io_buffer.rb
index ec10d2e1e6c..1bd839e1632 100644
--- a/test/ruby/test_io_buffer.rb
+++ b/test/ruby/test_io_buffer.rb
@@ -44,6 +44,19 @@ class TestIOBuffer < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_buffer.rb#L44
     assert buffer.mapped?
   end
 
+  def test_new_immutable
+    buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::IMMUTABLE)
+    assert buffer.immutable?
+
+    assert_raise RuntimeError do
+      buffer.copy("", 0)
+    end
+
+    assert_raise RuntimeError do
+      buffer.copy("!", 1)
+    end
+  end
+
   def test_file_mapped
     buffer = File.open(__FILE__) {|file| IO::Buffer.map(file)}
     assert_include buffer.to_str, "Hello World"
@@ -103,6 +116,22 @@ class TestIOBuffer < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_buffer.rb#L116
     # end
   end
 
+  def test_locked
+    buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::LOCKED)
+
+    assert_raise RuntimeError do
+      buffer.resize(256, 0)
+    end
+
+    assert_equal 128, buffer.size
+
+    assert_raise RuntimeError do
+      buffer.free
+    end
+
+    assert_equal 128, buffer.size
+  end
+
   def test_invalidation
     input, output = IO.pipe
 
-- 
cgit v1.2.1


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

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