diff options
author | Takumasa Ochi <[email protected]> | 2023-08-14 21:56:44 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2024-10-18 11:30:42 +0900 |
commit | 20dc1e5c255da87afb3829fa015142f23ee56b57 (patch) | |
tree | 1ebcf23c7c787650960986928a2d3de8cfbce7b8 | |
parent | 57e1b64c812210f7bd703bc5d8d459d7a1197629 (diff) |
[ruby/json] Always dup argument to preserve original encoding for force_encoding
https://github.com/ruby/json/commit/db9a489ca2
-rw-r--r-- | ext/json/parser/parser.rl | 5 | ||||
-rw-r--r-- | test/json/json_parser_test.rb | 6 |
2 files changed, 7 insertions, 4 deletions
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl index e3b3f325e6..61674b7812 100644 --- a/ext/json/parser/parser.rl +++ b/ext/json/parser/parser.rl @@ -673,10 +673,7 @@ static VALUE convert_encoding(VALUE source) } if (encindex == binary_encindex) { - if (OBJ_FROZEN(source)) { - source = rb_str_dup(source); - } - return rb_enc_associate_index(source, utf8_encindex); + return rb_enc_associate_index(rb_str_dup(source), utf8_encindex); } return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding()); diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index 610ea06f3f..d76281e6bc 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -26,6 +26,12 @@ class JSONParserTest < Test::Unit::TestCase assert_equal Encoding::UTF_16, source.encoding end if defined?(Encoding::UTF_16) + def test_argument_encoding_for_binary + source = "{}".encode("ASCII-8BIT") + JSON::Parser.new(source) + assert_equal Encoding::ASCII_8BIT, source.encoding + end if defined?(Encoding::ASCII_8BIT) + def test_error_message_encoding pend if RUBY_ENGINE == 'truffleruby' |