diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | ext/stringio/stringio.c | 10 | ||||
-rw-r--r-- | test/stringio/test_stringio.rb | 18 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 33 insertions, 3 deletions
@@ -1,3 +1,9 @@ +Wed Sep 3 12:48:14 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * ext/stringio/stringio.c (strio_write): use rb_str_append to + reuse coderange bits other than ASCII-8BIT, and keep + taintedness. [ruby-dev:48118] [Bug #9769] + Wed Sep 3 12:41:35 2014 Nobuyoshi Nakada <nobu@ruby-lang.org> * hash.c (env_shift): fix memory leak on Windows, free environment diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 15ef44aa35..2e7c367276 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1172,7 +1172,6 @@ strio_write(VALUE self, VALUE str) long len, olen; rb_encoding *enc, *enc2; - RB_GC_GUARD(str); if (!RB_TYPE_P(str, T_STRING)) str = rb_obj_as_string(str); enc = rb_enc_get(ptr->string); @@ -1188,7 +1187,13 @@ strio_write(VALUE self, VALUE str) ptr->pos = olen; } if (ptr->pos == olen) { - rb_str_cat(ptr->string, RSTRING_PTR(str), len); + if (enc2 == rb_ascii8bit_encoding()) { + rb_enc_str_buf_cat(ptr->string, RSTRING_PTR(str), len, enc); + OBJ_INFECT(ptr->string, str); + } + else { + rb_str_buf_append(ptr->string, str); + } } else { strio_extend(ptr, ptr->pos, len); @@ -1196,6 +1201,7 @@ strio_write(VALUE self, VALUE str) OBJ_INFECT(ptr->string, str); } OBJ_INFECT(ptr->string, self); + RB_GC_GUARD(str); ptr->pos += len; return LONG2NUM(len); } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index a2a2365149..4b7e904f4b 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -111,6 +111,24 @@ class TestStringIO < Test::Unit::TestCase f.close unless f.closed? end + def test_write_infection + bug9769 = '[ruby-dev:48118] [Bug #9769]' + s = "".untaint + f = StringIO.new(s, "w") + f.print("bar".taint) + f.close + assert_predicate(s, :tainted?, bug9769) + ensure + f.close unless f.closed? + end + + def test_write_encoding + s = "".force_encoding(Encoding::UTF_8) + f = StringIO.new(s) + f.print("\u{3053 3093 306b 3061 306f ff01}".b) + assert_equal(Encoding::UTF_8, s.encoding, "honor the original encoding over ASCII-8BIT") + end + def test_mode_error f = StringIO.new("", "r") assert_raise(IOError) { f.write("foo") } @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.0.0" #define RUBY_RELEASE_DATE "2014-09-03" -#define RUBY_PATCHLEVEL 544 +#define RUBY_PATCHLEVEL 545 #define RUBY_RELEASE_YEAR 2014 #define RUBY_RELEASE_MONTH 9 |