From ea1b1ea1aa98bc9488564ef18aa4032aa1cb5536 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 8 Nov 2023 14:05:52 +0100 Subject: String#force_encoding don't clear coderange if encoding is unchanged Some code out there blind calls `force_encoding` without checking what the original encoding was, which clears the coderange uselessly. If the String is big, it can be a rather costly mistake. For instance the `rack-utf8_sanitizer` gem does this on request bodies. --- test/objspace/test_objspace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/objspace') diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index bc6799b49f..1f1709fb76 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -585,7 +585,7 @@ class TestObjSpace < Test::Unit::TestCase def test_dump_string_coderange assert_includes ObjectSpace.dump("TEST STRING"), '"coderange":"7bit"' - unknown = "TEST STRING".dup.force_encoding(Encoding::BINARY) + unknown = "TEST STRING".dup.force_encoding(Encoding::UTF_16BE) 2.times do # ensure that dumping the string doesn't mutate it assert_includes ObjectSpace.dump(unknown), '"coderange":"unknown"' end -- cgit v1.2.3