From 265bd9e661061d364568f78b3c2649377bfe96b1 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 12 Aug 2009 04:00:03 +0000 Subject: [PATCH] * string.c (rb_str_new_frozen): must not change encoding of frozen shared string. [ruby-dev:39068] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24509 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ string.c | 7 ++++--- test/ruby/test_string.rb | 13 +++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index c28c11de87..ea7d328568 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Aug 12 12:59:51 2009 Nobuyoshi Nakada + + * string.c (rb_str_new_frozen): must not change encoding of frozen + shared string. [ruby-dev:39068] + Wed Aug 12 11:51:51 2009 Nobuyoshi Nakada * configure.in (rb_cv_broken_crypt): needs more checks. diff --git a/string.c b/string.c index 06edf3b8d1..39df54b195 100644 --- a/string.c +++ b/string.c @@ -671,13 +671,14 @@ rb_str_new_frozen(VALUE orig) assert(OBJ_FROZEN(str)); ofs = RSTRING_LEN(str) - RSTRING_LEN(orig); if ((ofs > 0) || (klass != RBASIC(str)->klass) || - (!OBJ_TAINTED(str) && OBJ_TAINTED(orig))) { + (!OBJ_TAINTED(str) && OBJ_TAINTED(orig)) || + ENCODING_GET(str) != ENCODING_GET(orig)) { str = str_new3(klass, str); RSTRING(str)->as.heap.ptr += ofs; RSTRING(str)->as.heap.len -= ofs; + rb_enc_cr_str_exact_copy(str, orig); + OBJ_INFECT(str, orig); } - rb_enc_cr_str_exact_copy(str, orig); - OBJ_INFECT(str, orig); } else if (STR_EMBED_P(orig)) { str = str_new(klass, RSTRING_PTR(orig), RSTRING_LEN(orig)); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 1d65c84c1c..36968cae3f 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1791,4 +1791,17 @@ class TestString < Test::Unit::TestCase ("aaaaaaaa".."zzzzzzzz").each {|s| s.to_sym } INPUT end + + def test_shared_force_encoding + s = "\u{3066}\u{3059}\u{3068}".gsub(//, '') + h = {} + h[s] = nil + k = h.keys[0] + assert_equal(s, k, '[ruby-dev:39068]') + assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]') + s.dup.force_encoding(Encoding::ASCII_8BIT).gsub(//, '') + k = h.keys[0] + assert_equal(s, k, '[ruby-dev:39068]') + assert_equal(Encoding::UTF_8, k.encoding, '[ruby-dev:39068]') + end end