Fix corruption of internal encoding string

[Bug #20598]

Just like [Bug #20595], Encoding#name_list and Encoding#aliases can have
their strings corrupted when Encoding.default_internal is set to nil.

Co-authored-by: Matthew Valentine-House <matt@eightbitraptor.com>
This commit is contained in:
Peter Zhu 2024-06-27 14:02:49 -04:00
parent 9c5e9d29f0
commit 176c4bb3c7
2 changed files with 16 additions and 10 deletions

View File

@ -1798,7 +1798,7 @@ static int
rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg) rb_enc_name_list_i(st_data_t name, st_data_t idx, st_data_t arg)
{ {
VALUE ary = (VALUE)arg; VALUE ary = (VALUE)arg;
VALUE str = rb_fstring_cstr((char *)name); VALUE str = rb_interned_str_cstr((char *)name);
rb_ary_push(ary, str); rb_ary_push(ary, str);
return ST_CONTINUE; return ST_CONTINUE;
} }
@ -1843,7 +1843,7 @@ rb_enc_aliases_enc_i(st_data_t name, st_data_t orig, st_data_t arg)
str = rb_fstring_cstr(rb_enc_name(enc)); str = rb_fstring_cstr(rb_enc_name(enc));
rb_ary_store(ary, idx, str); rb_ary_store(ary, idx, str);
} }
key = rb_fstring_cstr((char *)name); key = rb_interned_str_cstr((char *)name);
rb_hash_aset(aliases, key, str); rb_hash_aset(aliases, key, str);
return ST_CONTINUE; return ST_CONTINUE;
} }

View File

@ -1722,14 +1722,20 @@ class TestM17N < Test::Unit::TestCase
end end
def test_encoding_names_of_default_internal def test_encoding_names_of_default_internal
# [Bug #20595] # [Bug #20595] [Bug #20598]
assert_separately(%w(-W0), "#{<<~"begin;"}\n#{<<~"end;"}") [
begin; "default_internal.names",
"name_list",
"aliases.keys"
].each do |method|
assert_separately(%w(-W0), <<~RUBY)
exp_name = "int" + "ernal"
Encoding.default_internal = Encoding::ASCII_8BIT Encoding.default_internal = Encoding::ASCII_8BIT
names = Encoding.default_internal.names name = Encoding.#{method}.find { |x| x == exp_name }
Encoding.default_internal = nil Encoding.default_internal = nil
assert_include names, "int" + "ernal", "[Bug #20595]" assert_equal exp_name, name, "Encoding.#{method} [Bug #20595] [Bug #20598]"
end; RUBY
end
end end
def test_greek_capital_gap def test_greek_capital_gap