Skip to content

Commit 8230552

Browse files
committed
Update the coderange after overwrite
Fix https://bugs.ruby-lang.org/issues/20185
1 parent 824d2f6 commit 8230552

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

ext/stringio/stringio.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,8 +1464,14 @@ strio_write(VALUE self, VALUE str)
14641464
}
14651465
}
14661466
else {
1467+
int cr0 = ENC_CODERANGE(ptr->string);
1468+
int cr = ENC_CODERANGE_UNKNOWN;
1469+
if (rb_enc_asciicompat(enc) && rb_enc_asciicompat(enc2)) {
1470+
cr = ENC_CODERANGE_AND(cr0, ENC_CODERANGE(str));
1471+
}
14671472
strio_extend(ptr, ptr->pos, len);
14681473
memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len);
1474+
if (cr != cr0) ENC_CODERANGE_SET(ptr->string, cr);
14691475
}
14701476
RB_GC_GUARD(str);
14711477
ptr->pos += len;

test/stringio/test_stringio.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,16 @@ def test_binary_encoding_read_and_default_internal
953953
$VERBOSE = verbose
954954
end
955955

956+
def test_coderange_after_overwrite
957+
s = StringIO.new("".b)
958+
959+
s.write("a=b&c=d")
960+
s.rewind
961+
assert_predicate(s.string, :ascii_only?)
962+
s.write "\u{431 43e 433 443 441}"
963+
assert_not_predicate(s.string, :ascii_only?)
964+
end
965+
956966
def assert_string(content, encoding, str, mesg = nil)
957967
assert_equal([content, encoding], [str, str.encoding], mesg)
958968
end

0 commit comments

Comments
 (0)