From fc6ec7bfc7d1cc13380ac91eac3caeaa5e37df02 Mon Sep 17 00:00:00 2001 From: Cliff Pruitt Date: Wed, 31 Jul 2019 12:11:31 -0400 Subject: [PATCH] Prevent error on transliterate with frozen strings. ActiveSupport::Inflector.transliterate mutates strings by changing encodings. Prior to this commit passing a frozen string would raise a `FrozenError`. This change duplicates the internal string, if frozen, before transliterating. --- .../lib/active_support/inflector/transliterate.rb | 1 + activesupport/test/transliterate_test.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb index 3ba2d93ed8..1899a1212d 100644 --- a/activesupport/lib/active_support/inflector/transliterate.rb +++ b/activesupport/lib/active_support/inflector/transliterate.rb @@ -60,6 +60,7 @@ module ActiveSupport # Transliteration is restricted to UTF-8, US-ASCII and GB18030 strings # Other encodings will raise an ArgumentError. def transliterate(string, replacement = "?", locale: nil) + string = string.dup if string.frozen? raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String) allowed_encodings = [Encoding::UTF_8, Encoding::US_ASCII, Encoding::GB18030] diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 2e02b5e938..f13c5efa47 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -59,19 +59,19 @@ class TransliterateTest < ActiveSupport::TestCase end def test_transliterate_handles_strings_with_valid_utf8_encodings - string = String.new("A", encoding: Encoding::UTF_8) + string = String.new("A", encoding: Encoding::UTF_8).freeze assert_equal "A", ActiveSupport::Inflector.transliterate(string) end def test_transliterate_handles_strings_with_valid_us_ascii_encodings - string = String.new("A", encoding: Encoding::US_ASCII) + string = String.new("A", encoding: Encoding::US_ASCII).freeze transcoded = ActiveSupport::Inflector.transliterate(string) assert_equal "A", transcoded assert_equal Encoding::US_ASCII, transcoded.encoding end def test_transliterate_handles_strings_with_valid_gb18030_encodings - string = String.new("A", encoding: Encoding::GB18030) + string = String.new("A", encoding: Encoding::GB18030).freeze transcoded = ActiveSupport::Inflector.transliterate(string) assert_equal "A", transcoded assert_equal Encoding::GB18030, transcoded.encoding @@ -84,7 +84,7 @@ class TransliterateTest < ActiveSupport::TestCase Encoding::GB18030 ] incompatible_encodings.each do |encoding| - string = String.new("", encoding: encoding) + string = String.new("", encoding: encoding).freeze exception = assert_raises ArgumentError do ActiveSupport::Inflector.transliterate(string) end @@ -93,17 +93,17 @@ class TransliterateTest < ActiveSupport::TestCase end def test_transliterate_handles_strings_with_invalid_utf8_bytes - string = String.new("\255", encoding: Encoding::UTF_8) + string = String.new("\255", encoding: Encoding::UTF_8).freeze assert_equal "?", ActiveSupport::Inflector.transliterate(string) end def test_transliterate_handles_strings_with_invalid_us_ascii_bytes - string = String.new("\255", encoding: Encoding::US_ASCII) + string = String.new("\255", encoding: Encoding::US_ASCII).freeze assert_equal "?", ActiveSupport::Inflector.transliterate(string) end def test_transliterate_handles_strings_with_invalid_gb18030_bytes - string = String.new("\255", encoding: Encoding::GB18030) + string = String.new("\255", encoding: Encoding::GB18030).freeze assert_equal "?", ActiveSupport::Inflector.transliterate(string) end end