[前][次][番号順一覧][スレッド一覧]

ruby-changes:74430

From: Jemma <ko1@a...>
Date: Thu, 10 Nov 2022 23:27:10 +0900 (JST)
Subject: [ruby-changes:74430] 199b59f065 (master): Fix bug in array pack with shared strings

https://git.ruby-lang.org/ruby.git/commit/?id=199b59f065

From 199b59f065ce6f1c13b8424f35a70c513523211b Mon Sep 17 00:00:00 2001
From: Jemma Issroff <jemmaissroff@g...>
Date: Wed, 9 Nov 2022 17:04:35 -0500
Subject: Fix bug in array pack with shared strings

If string literals are long and they become shared, we need to make them
independent before we can write to them. [Bug #19116]
---
 pack.c                  | 1 +
 test/ruby/test_array.rb | 6 ++++++
 2 files changed, 7 insertions(+)

diff --git a/pack.c b/pack.c
index 2817491b77..294d7dfa35 100644
--- a/pack.c
+++ b/pack.c
@@ -217,6 +217,7 @@ pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer) https://github.com/ruby/ruby/blob/trunk/pack.c#L217
     else {
         if (!RB_TYPE_P(buffer, T_STRING))
             rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer));
+        rb_str_modify(buffer);
         res = buffer;
     }
 
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 20e6ee7917..f58f8a2778 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1294,6 +1294,12 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1294
 =end
   end
 
+  def test_pack_with_buffer
+    n = [ 65, 66, 67 ]
+    str = "a" * 100
+    assert_equal("aaaABC", n.pack("@3ccc", buffer: str.dup), "[Bug #19116]")
+  end
+
   def test_pop
     a = @cls[ 'cat', 'dog' ]
     assert_equal('dog', a.pop)
-- 
cgit v1.2.3


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]