[ruby/stringio] Update the coderange after overwrite

Fix https://bugs.ruby-lang.org/issues/20185

https://github.com/ruby/stringio/commit/8230552a46
This commit is contained in:
Nobuyoshi Nakada 2024-01-16 23:59:55 +09:00 committed by git
parent fef8ccff11
commit 6283ae8d36
2 changed files with 16 additions and 0 deletions

View File

@ -1464,8 +1464,14 @@ strio_write(VALUE self, VALUE str)
}
}
else {
int cr0 = ENC_CODERANGE(ptr->string);
int cr = ENC_CODERANGE_UNKNOWN;
if (rb_enc_asciicompat(enc) && rb_enc_asciicompat(enc2)) {
cr = ENC_CODERANGE_AND(cr0, ENC_CODERANGE(str));
}
strio_extend(ptr, ptr->pos, len);
memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len);
if (cr != cr0) ENC_CODERANGE_SET(ptr->string, cr);
}
RB_GC_GUARD(str);
ptr->pos += len;

View File

@ -953,6 +953,16 @@ class TestStringIO < Test::Unit::TestCase
$VERBOSE = verbose
end
def test_coderange_after_overwrite
s = StringIO.new("".b)
s.write("a=b&c=d")
s.rewind
assert_predicate(s.string, :ascii_only?)
s.write "\u{431 43e 433 443 441}"
assert_not_predicate(s.string, :ascii_only?)
end
def assert_string(content, encoding, str, mesg = nil)
assert_equal([content, encoding], [str, str.encoding], mesg)
end