From e94ece76d8d73b5e8a61531fe68f5e08ee78f8dc Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 23 Jan 2008 06:04:13 +0000 Subject: [PATCH] * string.c (str_make_independent): should set length. * string.c (rb_str_associate): hide associated array from ObjectSpace. * string.c (rb_str_associated): return associated array with freezing instead of false. [ruby-dev:33282] * string.c (rb_str_freeze): freeze associated array together. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15180 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++++++++++ string.c | 11 +++++++++-- test/ruby/test_pack.rb | 14 ++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 016be011cb..a3c2fae6c8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Wed Jan 23 15:04:11 2008 Nobuyoshi Nakada + + * string.c (str_make_independent): should set length. + + * string.c (rb_str_associate): hide associated array from ObjectSpace. + + * string.c (rb_str_associated): return associated array with freezing + instead of false. [ruby-dev:33282] + + * string.c (rb_str_freeze): freeze associated array together. + Wed Jan 23 13:39:48 2008 Tanaka Akira * re.c (rb_reg_prepare_re): fix SEGV by diff --git a/string.c b/string.c index 56bf0fa759..eacced74ca 100644 --- a/string.c +++ b/string.c @@ -742,6 +742,7 @@ str_make_independent(VALUE str) STR_SET_NOEMBED(str); ptr[len] = 0; RSTRING(str)->as.heap.ptr = ptr; + RSTRING(str)->as.heap.len = len; RSTRING(str)->as.heap.aux.capa = len; STR_UNSET_NOCAPA(str); } @@ -771,6 +772,7 @@ rb_str_associate(VALUE str, VALUE add) RESIZE_CAPA(str, RSTRING_LEN(str)); } FL_SET(str, STR_ASSOC); + RBASIC(add)->klass = 0; RSTRING(str)->as.heap.aux.shared = add; } } @@ -779,8 +781,9 @@ VALUE rb_str_associated(VALUE str) { if (STR_ASSOC_P(str)) { - if (OBJ_FROZEN(str)) return Qfalse; - return RSTRING(str)->as.heap.aux.shared; + VALUE ary = RSTRING(str)->as.heap.aux.shared; + if (OBJ_FROZEN(str)) OBJ_FREEZE(ary); + return ary; } return Qfalse; } @@ -953,6 +956,10 @@ rb_str_substr(VALUE str, long beg, long len) VALUE rb_str_freeze(VALUE str) { + if (STR_ASSOC_P(str)) { + VALUE ary = RSTRING(str)->as.heap.aux.shared; + OBJ_FREEZE(ary); + } return rb_obj_freeze(str); } diff --git a/test/ruby/test_pack.rb b/test/ruby/test_pack.rb index e67465b33a..f78c48dee6 100644 --- a/test/ruby/test_pack.rb +++ b/test/ruby/test_pack.rb @@ -57,4 +57,18 @@ class TestPack < Test::Unit::TestCase assert_raises(RangeError) { [0x80000000].pack("U") } assert_raises(RangeError) { [0x100000000].pack("U") } end + + def test_pack_P + a = ["abc"] + assert_equal a, a.pack("P").unpack("P*") + assert_equal "a", a.pack("P").unpack("P")[0] + assert_equal a, a.pack("P").freeze.unpack("P*") + end + + def test_pack_p + a = ["abc"] + assert_equal a, a.pack("p").unpack("p*") + assert_equal a[0], a.pack("p").unpack("p")[0] + assert_equal a, a.pack("p").freeze.unpack("p*") + end end