Skip to content

Commit 7a780a3

Browse files
[Bug #20595] Fix corruption of encoding name string (ruby#11063)
Fix corruption of encoding name string [Bug #20595] enc_set_default_encoding will free the C string if the encoding is nil, but the C string can be used by the encoding name string. This will cause the encoding name string to be corrupted. Consider the following code: Encoding.default_internal = Encoding::ASCII_8BIT names = Encoding.default_internal.names p names Encoding.default_internal = nil p names It outputs: ["ASCII-8BIT", "BINARY", "internal"] ["ASCII-8BIT", "BINARY", "\x00\x00\x00\x00\x00\x00\x00\x00"] Co-authored-by: Matthew Valentine-House <matt@eightbitraptor.com>
1 parent 0176283 commit 7a780a3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

encoding.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ enc_names_i(st_data_t name, st_data_t idx, st_data_t args)
12911291
VALUE *arg = (VALUE *)args;
12921292

12931293
if ((int)idx == (int)arg[0]) {
1294-
VALUE str = rb_fstring_cstr((char *)name);
1294+
VALUE str = rb_interned_str_cstr((char *)name);
12951295
rb_ary_push(arg[1], str);
12961296
}
12971297
return ST_CONTINUE;

test/ruby/test_m17n.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,17 @@ def test_inspect_with_default_internal
17211721
assert_equal(e("[\"\xB4\xC1\xBB\xFA\"]"), s, bug11787)
17221722
end
17231723

1724+
def test_encoding_names_of_default_internal
1725+
# [Bug #20595]
1726+
assert_separately(%w(-W0), "#{<<~"begin;"}\n#{<<~"end;"}")
1727+
begin;
1728+
Encoding.default_internal = Encoding::ASCII_8BIT
1729+
names = Encoding.default_internal.names
1730+
Encoding.default_internal = nil
1731+
assert_include names, "int" + "ernal", "[Bug #20595]"
1732+
end;
1733+
end
1734+
17241735
def test_greek_capital_gap
17251736
bug12204 = '[ruby-core:74478] [Bug #12204] GREEK CAPITAL RHO and SIGMA'
17261737
assert_equal("\u03A3", "\u03A1".succ, bug12204)

0 commit comments

Comments
 (0)