Skip to content

Commit 5ce0ba0

Browse files
committed
merge revision(s) 35e1248: [Backport #20755]
[Bug #20755] Frozen string should not be writable via IO::Buffer
1 parent 12ea98e commit 5ce0ba0

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

io_buffer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,8 @@ rb_io_buffer_get_bytes(VALUE self, void **base, size_t *size)
843843
static inline void
844844
io_buffer_get_bytes_for_writing(struct rb_io_buffer *buffer, void **base, size_t *size)
845845
{
846-
if (buffer->flags & RB_IO_BUFFER_READONLY) {
846+
if (buffer->flags & RB_IO_BUFFER_READONLY ||
847+
(!NIL_P(buffer->source) && OBJ_FROZEN(buffer->source))) {
847848
rb_raise(rb_eIOBufferAccessError, "Buffer is not writable!");
848849
}
849850

test/ruby/test_io_buffer.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ def test_slice_readonly
248248
assert_equal "Hello World", hello
249249
end
250250

251+
def test_transfer
252+
hello = %w"Hello World".join(" ")
253+
buffer = IO::Buffer.for(hello)
254+
transferred = buffer.transfer
255+
assert_equal "Hello World", transferred.get_string
256+
assert_predicate buffer, :null?
257+
assert_raise IO::Buffer::AccessError do
258+
transferred.set_string("Goodbye")
259+
end
260+
assert_equal "Hello World", hello
261+
end
262+
263+
def test_transfer_in_block
264+
hello = %w"Hello World".join(" ")
265+
buffer = IO::Buffer.for(hello, &:transfer)
266+
assert_equal "Hello World", buffer.get_string
267+
buffer.set_string("Ciao!")
268+
assert_equal "Ciao! World", hello
269+
hello.freeze
270+
assert_raise IO::Buffer::AccessError do
271+
buffer.set_string("Hola")
272+
end
273+
assert_equal "Ciao! World", hello
274+
end
275+
251276
def test_locked
252277
buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::LOCKED)
253278

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
1212
#define RUBY_VERSION_TEENY 5
1313
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
14-
#define RUBY_PATCHLEVEL 105
14+
#define RUBY_PATCHLEVEL 106
1515

1616
#include "ruby/version.h"
1717
#include "ruby/internal/abi.h"

0 commit comments

Comments
 (0)