[Bug #20752] Slice of readonly IO::Buffer also should be readonly

This commit is contained in:
Nobuyoshi Nakada 2024-09-30 16:24:03 +09:00
parent b93c51c114
commit 637067440f
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2024-09-30 11:40:03 +00:00
2 changed files with 13 additions and 0 deletions

View File

@ -1532,6 +1532,7 @@ rb_io_buffer_slice(struct rb_io_buffer *buffer, VALUE self, size_t offset, size_
struct rb_io_buffer *slice = NULL;
TypedData_Get_Struct(instance, struct rb_io_buffer, &rb_io_buffer_type, slice);
slice->flags |= (buffer->flags & RB_IO_BUFFER_READONLY);
slice->base = (char*)buffer->base + offset;
slice->size = length;

View File

@ -236,6 +236,18 @@ class TestIOBuffer < Test::Unit::TestCase
end
end
def test_slice_readonly
hello = %w"Hello World".join(" ").freeze
buffer = IO::Buffer.for(hello)
slice = buffer.slice
assert_predicate slice, :readonly?
assert_raise IO::Buffer::AccessError do
# This breaks the literal in string pool and many other tests in this file.
slice.set_string("Adios", 0, 5)
end
assert_equal "Hello World", hello
end
def test_locked
buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::LOCKED)