ruby-changes:70393
From: Samuel <ko1@a...>
Date: Tue, 21 Dec 2021 08:26:05 +0900 (JST)
Subject: [ruby-changes:70393] 71bf5cef75 (master): Fix handling of frozens strings.
https://git.ruby-lang.org/ruby.git/commit/?id=71bf5cef75 From 71bf5cef75b9d244a06261c9fc0b84fbe5a1592f Mon Sep 17 00:00:00 2001 From: Samuel Williams <samuel.williams@o...> Date: Mon, 20 Dec 2021 09:43:22 +1300 Subject: Fix handling of frozens strings. --- io_buffer.c | 7 ++++++- test/ruby/test_io_buffer.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/io_buffer.c b/io_buffer.c index a97c687239d..d2a12b98fbd 100644 --- a/io_buffer.c +++ b/io_buffer.c @@ -279,7 +279,12 @@ rb_io_buffer_type_for(VALUE klass, VALUE string) https://github.com/ruby/ruby/blob/trunk/io_buffer.c#L279 rb_str_locktmp(string); - io_buffer_initialize(data, RSTRING_PTR(string), RSTRING_LEN(string), RB_IO_BUFFER_EXTERNAL, string); + enum rb_io_buffer_flags flags = RB_IO_BUFFER_EXTERNAL; + + if (RB_OBJ_FROZEN(string)) + flags |= RB_IO_BUFFER_IMMUTABLE; + + io_buffer_initialize(data, RSTRING_PTR(string), RSTRING_LEN(string), flags, string); return instance; } diff --git a/test/ruby/test_io_buffer.rb b/test/ruby/test_io_buffer.rb index 385a133421d..76fa1bd7749 100644 --- a/test/ruby/test_io_buffer.rb +++ b/test/ruby/test_io_buffer.rb @@ -83,6 +83,7 @@ class TestIOBuffer < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_buffer.rb#L83 def test_string_mapped string = "Hello World" buffer = IO::Buffer.for(string) + refute buffer.immutable? # Cannot modify string as it's locked by the buffer: assert_raise RuntimeError do @@ -99,6 +100,13 @@ class TestIOBuffer < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_buffer.rb#L100 assert_equal "Hello World", string end + def test_string_mapped_frozen + string = "Hello World".freeze + buffer = IO::Buffer.for(string) + + assert buffer.immutable? + end + def test_non_string not_string = Object.new -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/