From f85e5e01bafeca387e833b9d79cab43a8b22aa3d Mon Sep 17 00:00:00 2001 From: nagachika Date: Sun, 16 Mar 2025 20:10:00 +0900 Subject: merge revision(s) f423f6e10c0c226dfed98e7cb7a5d489191dfa35: [Backport #21131] Ensure IO.copy_stream buffer is an independent string Otherwise, changes to the buffer by the destination write method could result in data changing for supposedly independent strings. Fixes [Bug #21131] --- test/ruby/test_io.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 51c9f2b83c..16de2e8a76 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1116,6 +1116,34 @@ class TestIO < Test::Unit::TestCase } end + def test_copy_stream_dup_buffer + bug21131 = '[ruby-core:120961] [Bug #21131]' + mkcdtmpdir do + dst_class = Class.new do + def initialize(&block) + @block = block + end + + def write(data) + @block.call(data.dup) + data.bytesize + end + end + + rng = Random.new(42) + body = Tempfile.new("ruby-bug", binmode: true) + body.write(rng.bytes(16_385)) + body.rewind + + payload = [] + IO.copy_stream(body, dst_class.new{|cls| payload << cls}) + body.rewind + assert_equal(body.read, payload.join, bug21131) + ensure + body&.close + end + end + def test_copy_stream_write_in_binmode bug8767 = '[ruby-core:56518] [Bug #8767]' mkcdtmpdir { -- cgit v1.2.3