[前][次][番号順一覧][スレッド一覧]

ruby-changes:33596

From: nobu <ko1@a...>
Date: Wed, 23 Apr 2014 11:03:55 +0900 (JST)
Subject: [ruby-changes:33596] nobu:r45677 (trunk): stringio.c: use rb_str_append other than ASCII-8BIT

nobu	2014-04-23 11:03:43 +0900 (Wed, 23 Apr 2014)

  New Revision: 45677

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45677

  Log:
    stringio.c: use rb_str_append other than ASCII-8BIT
    
    * 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]

  Modified files:
    trunk/ChangeLog
    trunk/ext/stringio/stringio.c
    trunk/test/stringio/test_stringio.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45676)
+++ ChangeLog	(revision 45677)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Apr 23 11:03:41 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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 Apr 23 00:43:00 2014  NAKAMURA Usaku  <usa@r...>
 
 	* win32/win32.c, include/ruby/win32.h (ustatfs): implementation of
Index: ext/stringio/stringio.c
===================================================================
--- ext/stringio/stringio.c	(revision 45676)
+++ ext/stringio/stringio.c	(revision 45677)
@@ -1185,7 +1185,13 @@ strio_write(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/stringio/stringio.c#L1185
 	ptr->pos = olen;
     }
     if (ptr->pos == olen) {
-	rb_enc_str_buf_cat(ptr->string, RSTRING_PTR(str), len, enc);
+	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);
Index: test/stringio/test_stringio.rb
===================================================================
--- test/stringio/test_stringio.rb	(revision 45676)
+++ test/stringio/test_stringio.rb	(revision 45677)
@@ -119,6 +119,24 @@ class TestStringIO < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/stringio/test_stringio.rb#L119
     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") }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]