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) about 2 months ago. Updated about 6 hours 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 #1

Updated by nobu (Nobuyoshi Nakada) about 2 months ago

  • Backport changed from 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN to 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED

Updated by mame (Yusuke Endoh) about 1 month ago

  • Assignee set to ioquatix (Samuel Williams)
Actions #4

Updated by nobu (Nobuyoshi Nakada) about 1 month ago

  • Status changed from Open to Closed

Applied in changeset git|35e124832e29b65c84d4e0e4e434616859f9bdf5.


[Bug #20755] Frozen string should not be writable via IO::Buffer

Updated by nagachika (Tomoyuki Chikanaga) 18 days ago

  • Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED to 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED

Updated by k0kubun (Takashi Kokubun) about 6 hours ago

  • Backport changed from 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED to 3.1: REQUIRED, 3.2: DONE, 3.3: DONE
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0