Project

General

Profile

Actions

Bug #20755

closed

IO::Buffer#transfer transfers mutable reference to String's memory but not lock ownership

Added by hanazuki (Kasumi Hanazuki) 2 months ago. Updated 17 days ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-09-18T02:16:22Z master 4797b0704a) +PRISM [x86_64-linux]
[ruby-core:119257]

Description

IO::Buffer.for with a block yields a mutable IO::Buffer that shares underlying memory with a mutable String. While the block is executed, the String instance is temporarily locked and cannot be modified or frozen.
If you call #transfer on the yielded IO::Buffer in the block, however, a mutable IO::Buffer can be escaped from the .for method. Through this IO::Buffer instance, you can modify the underlying String memory even after the String instance is frozen.

irb(main):001> str = +'a'
=> "a"
irb(main):002> buf = IO::Buffer.for(str) {|b| b.transfer }
=>
#<IO::Buffer 0x00007f244d1ede98+1 EXTERNAL SLICE>
...
irb(main):003> str.freeze
=> "a"
irb(main):004> buf.set_string('b')
=> 1
irb(main):005> str
=> "b"
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0