From: "nagachika (Tomoyuki Chikanaga) via ruby-core" Date: 2024-10-21T08:50:16+00:00 Subject: [ruby-core:119547] [Ruby master Bug#20755] IO::Buffer#transfer transfers mutable reference to String's memory but not lock ownership Issue #20755 has been updated by nagachika (Tomoyuki Chikanaga). Backport changed from 3.1: REQUIRED, 3.2: REQUIRED, 3.3: REQUIRED to 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED ruby_3_2 commit:087e4ed6cc9da9cfca1a107058905446ff474bd1 merged revision(s) commit:35e124832e29b65c84d4e0e4e434616859f9bdf5. ---------------------------------------- Bug #20755: IO::Buffer#transfer transfers mutable reference to String's memory but not lock ownership https://bugs.ruby-lang.org/issues/20755#change-110161 * Author: hanazuki (Kasumi Hanazuki) * Status: Closed * Assignee: ioquatix (Samuel Williams) * ruby -v: ruby 3.4.0dev (2024-09-18T02:16:22Z master 4797b0704a) +PRISM [x86_64-linux] * Backport: 3.1: REQUIRED, 3.2: DONE, 3.3: REQUIRED ---------------------------------------- `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 irb(main):001> str = +'a' => "a" irb(main):002> buf = IO::Buffer.for(str) {|b| b.transfer } => # ... irb(main):003> str.freeze => "a" irb(main):004> buf.set_string('b') => 1 irb(main):005> str => "b" ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/