diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 74e2b95..27c7f65 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -915,9 +915,6 @@ strio_extend(struct StringIO *ptr, long pos, long len) if (pos > olen) MEMZERO(RSTRING_PTR(ptr->string) + olen, char, pos - olen); } - else { - rb_str_modify(ptr->string); - } } /* @@ -1465,6 +1462,7 @@ strio_write(VALUE self, VALUE str) } else { strio_extend(ptr, ptr->pos, len); + rb_str_modify(ptr->string); memmove(RSTRING_PTR(ptr->string)+ptr->pos, RSTRING_PTR(str), len); } RB_GC_GUARD(str); diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 216b06d..a5c1ff7 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -922,6 +922,16 @@ def test_encoding_write assert_equal("abc".encode("utf-32be"), s.string) end + def test_write_str_modify + seek_pos = [0, 2, 4, 6] + seek_pos.each do |pos| + s = StringIO.new('abcd') + s.seek(pos) + s.write("\xff\xff\xff") + refute(s.string.ascii_only?) + end + end + def test_encoding_read s = StringIO.new("abc".encode("utf-32be"), "r:utf-8") assert_equal("\0\0\0a\0\0\0b\0\0\0c", s.read)