diff options
author | Cliff Pruitt <cliff.pruitt@cliffpruitt.com> | 2019-07-31 12:11:31 -0400 |
---|---|---|
committer | Cliff Pruitt <cliff.pruitt@cliffpruitt.com> | 2019-07-31 12:11:31 -0400 |
commit | fc6ec7bfc7d1cc13380ac91eac3caeaa5e37df02 (patch) | |
tree | 0c788144fede2f6db763828b289acb87368a050e /activesupport | |
parent | 4c8c8c87b07844b26564ca6aa2052660e601bc3d (diff) | |
download | rails-fc6ec7bfc7d1cc13380ac91eac3caeaa5e37df02.tar.gz rails-fc6ec7bfc7d1cc13380ac91eac3caeaa5e37df02.tar.bz2 rails-fc6ec7bfc7d1cc13380ac91eac3caeaa5e37df02.zip |
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.
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/inflector/transliterate.rb | 1 | ||||
-rw-r--r-- | 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 |