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

ruby-changes:39513

From: nobu <ko1@a...>
Date: Mon, 17 Aug 2015 11:58:00 +0900 (JST)
Subject: [ruby-changes:39513] nobu:r51594 (trunk): io.c: raise at incomplete char

nobu	2015-08-17 11:57:39 +0900 (Mon, 17 Aug 2015)

  New Revision: 51594

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

  Log:
    io.c: raise at incomplete char
    
    * io.c (rb_io_each_codepoint): raise an exception at incomplete
      character before EOF when conversion takes place.  [Bug #11444]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/io.c
    trunk/test/ruby/test_io_m17n.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51593)
+++ ChangeLog	(revision 51594)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Aug 17 11:57:36 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (rb_io_each_codepoint): raise an exception at incomplete
+	  character before EOF when conversion takes place.  [Bug #11444]
+
 Sun Aug 16 17:33:45 2015  SHIBATA Hiroshi  <hsbt@r...>
 
 	* gems/bundled_gems: update latest version of bundled gems.
Index: io.c
===================================================================
--- io.c	(revision 51593)
+++ io.c	(revision 51594)
@@ -3727,13 +3727,16 @@ rb_io_each_codepoint(VALUE io) https://github.com/ruby/ruby/blob/trunk/io.c#L3727
 		}
 		if (more_char(fptr) == MORE_CHAR_FINISHED) {
                     clear_readconv(fptr);
-		    /* ignore an incomplete character before EOF */
+		    if (!MBCLEN_CHARFOUND_P(r)) {
+			enc = fptr->encs.enc;
+			goto invalid;
+		    }
 		    return io;
 		}
 	    }
 	    if (MBCLEN_INVALID_P(r)) {
-		rb_raise(rb_eArgError, "invalid byte sequence in %s",
-			 rb_enc_name(fptr->encs.enc));
+		enc = fptr->encs.enc;
+		goto invalid;
 	    }
 	    n = MBCLEN_CHARFOUND_LEN(r);
 	    if (fptr->encs.enc) {
Index: NEWS
===================================================================
--- NEWS	(revision 51593)
+++ NEWS	(revision 51594)
@@ -80,6 +80,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L80
 
 * IO
   * IO#close doesn't raise when the IO object is closed.  [Feature #10718]
+  * IO#each_codepoint raises an exception at incomplete character
+    before EOF when conversion takes place.  [Bug #11444]
 
 * Module
   * Module#define_method and Object.define_singleton_method now
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 51593)
+++ test/ruby/test_io_m17n.rb	(revision 51594)
@@ -1,6 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L1
 # coding: US-ASCII
 require 'test/unit'
 require 'tmpdir'
+require 'tempfile'
 require 'timeout'
 
 class TestIO_M17N < Test::Unit::TestCase
@@ -2564,22 +2565,40 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L2565
   end
 
   def test_each_codepoint_need_more
-    code = <<-'end;'
-      c = nil
-      begin
-        STDIN.set_encoding(Encoding::UTF_8).each_codepoint{|i| c = i}
-      rescue ArgumentError => e
-        STDERR.puts e.message
-      else
-        printf "%x", c
-      end
-    end;
-    args = ['-e', code]
     bug11444 = '[ruby-core:70379] [Bug #11444]'
-    assert_in_out_err(args, "\u{1f376}".b[0,3], [],
-                      ["invalid byte sequence in UTF-8"],
-                      bug11444, timeout: 1)
-    assert_in_out_err(args, "x"*8190+"\u{1f376}", ["1f376"], [],
-                      bug11444, timeout: 1)
+    tests = [
+      ["incomplete multibyte", "\u{1f376}".b[0,3], [], ["invalid byte sequence in UTF-8"]],
+      ["multibyte at boundary", "x"*8190+"\u{1f376}", ["1f376"], []],
+    ]
+    failure = []
+    ["bin", "text"].product(tests) do |mode, (test, data, out, err)|
+      code = <<-"end;"
+        c = nil
+        begin
+          open(ARGV[0], "r#{mode[0]}:utf-8") do |f|
+            f.each_codepoint{|i| c = i}
+          end
+        rescue ArgumentError => e
+          STDERR.puts e.message
+        else
+          printf "%x", c
+        end
+      end;
+      Tempfile.create("codepoint") do |f|
+        args = ['-e', code, f.path]
+        f.print data
+        f.close
+        begin
+          assert_in_out_err(args, "", out, err,
+                            "#{bug11444}: #{test} in #{mode} mode",
+                            timeout: 1)
+        rescue Exception => e
+          failure << e
+        end
+      end
+    end
+    unless failure.empty?
+      flunk failure.join("\n---\n")
+    end
   end
 end

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

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