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

ruby-changes:34814

From: nobu <ko1@a...>
Date: Tue, 22 Jul 2014 13:33:44 +0900 (JST)
Subject: [ruby-changes:34814] nobu:r46897 (trunk): string.c: raise at invalid byte sequence

nobu	2014-07-22 13:33:38 +0900 (Tue, 22 Jul 2014)

  New Revision: 46897

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

  Log:
    string.c: raise at invalid byte sequence
    
    * string.c (rb_str_count): raise at invalid byte sequence argument
      even if single-byte optimization is effective.
      [ruby-dev:48442] [Bug #10078]

  Modified files:
    trunk/string.c
    trunk/test/ruby/test_m17n_comb.rb
Index: string.c
===================================================================
--- string.c	(revision 46896)
+++ string.c	(revision 46897)
@@ -6075,15 +6075,17 @@ rb_str_count(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/string.c#L6075
     rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
     for (i=0; i<argc; i++) {
 	VALUE tstr = argv[i];
-	const unsigned char *utstr;
+	const char *ptstr;
 
 	StringValue(tstr);
 	enc = rb_enc_check(str, tstr);
 	if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
-	    (utstr = (const OnigUChar *)RSTRING_PTR(tstr), ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, utstr, utstr+1)) &&
+	    (ptstr = RSTRING_PTR(tstr),
+	     ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
 	    !is_broken_string(str)) {
 	    int n = 0;
-	    unsigned char c = utstr[0];
+	    int clen;
+	    unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
 
 	    s = RSTRING_PTR(str);
 	    if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
Index: test/ruby/test_m17n_comb.rb
===================================================================
--- test/ruby/test_m17n_comb.rb	(revision 46896)
+++ test/ruby/test_m17n_comb.rb	(revision 46897)
@@ -88,7 +88,7 @@ class TestM17NComb < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n_comb.rb#L88
     r
   end
 
-  def assert_enccall(recv, meth, *args, &block)
+  def encdumpcall(recv, meth, *args, &block)
     desc = ''
     if String === recv
       desc << encdump(recv)
@@ -111,6 +111,11 @@ class TestM17NComb < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n_comb.rb#L111
     if block
       desc << ' {}'
     end
+    desc
+  end
+
+  def assert_enccall(recv, meth, *args, &block)
+    desc = encdumpcall(recv, meth, *args, &block)
     result = nil
     assert_nothing_raised(desc) {
       result = recv.send(meth, *args, &block)
@@ -710,12 +715,13 @@ class TestM17NComb < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n_comb.rb#L715
 
   def test_str_count
     combination(STRINGS, STRINGS) {|s1, s2|
+      desc = proc {encdumpcall(s1, :count, s2)}
       if !s1.valid_encoding? || !s2.valid_encoding?
-        assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.count(s2) }
+        assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.count(s2) }
         next
       end
       if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
-        assert_raise(Encoding::CompatibilityError) { s1.count(s2) }
+        assert_raise(Encoding::CompatibilityError, desc) { s1.count(s2) }
         next
       end
       n = enccall(s1, :count, s2)

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

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