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

ruby-changes:27361

From: nobu <ko1@a...>
Date: Sat, 23 Feb 2013 09:48:57 +0900 (JST)
Subject: [ruby-changes:27361] nobu:r39413 (trunk): dir.c: encoding check

nobu	2013-02-23 09:48:44 +0900 (Sat, 23 Feb 2013)

  New Revision: 39413

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

  Log:
    dir.c: encoding check
    
    * dir.c (file_s_fnmatch, fnmatch_brace): encoding-incompatible pattern
      and string do not match, instead of exception.  [ruby-dev:47069]
      [Bug #7911]

  Modified files:
    trunk/ChangeLog
    trunk/dir.c
    trunk/test/ruby/test_fnmatch.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39412)
+++ ChangeLog	(revision 39413)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Feb 23 09:48:41 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* dir.c (file_s_fnmatch, fnmatch_brace): encoding-incompatible pattern
+	  and string do not match, instead of exception.  [ruby-dev:47069]
+	  [Bug #7911]
+
 Sat Feb 23 08:57:46 2013  Marc-Andre Lafortune  <ruby-core@m...>
 
 	* doc/NEWS-*: Update NEWS from their respective branches
Index: dir.c
===================================================================
--- dir.c	(revision 39412)
+++ dir.c	(revision 39413)
@@ -1919,7 +1919,24 @@ fnmatch_brace(const char *pattern, VALUE https://github.com/ruby/ruby/blob/trunk/dir.c#L1919
 {
     struct brace_args *arg = (struct brace_args *)val;
     VALUE path = arg->value;
+    rb_encoding *enc_pattern = enc;
+    rb_encoding *enc_path = rb_enc_get(path);
 
+    if (enc_pattern != enc_path) {
+	if (!rb_enc_asciicompat(enc_pattern))
+	    return FNM_NOMATCH;
+	if (!rb_enc_asciicompat(enc_path))
+	    return FNM_NOMATCH;
+	if (!rb_enc_str_asciionly_p(path)) {
+	    int cr = ENC_CODERANGE_7BIT;
+	    long len = strlen(pattern);
+	    if (rb_str_coderange_scan_restartable(pattern, pattern + len,
+						  enc_pattern, &cr) != len)
+		return FNM_NOMATCH;
+	    if (cr != ENC_CODERANGE_7BIT)
+		return FNM_NOMATCH;
+	}
+    }
     return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
 }
 
@@ -2029,8 +2046,9 @@ file_s_fnmatch(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/dir.c#L2046
 	    return Qtrue;
     }
     else {
-	if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
-		    flags) == 0)
+	rb_encoding *enc = rb_enc_compatible(pattern, path);
+	if (!enc) return Qfalse;
+	if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
 	    return Qtrue;
     }
     RB_GC_GUARD(pattern);
Index: test/ruby/test_fnmatch.rb
===================================================================
--- test/ruby/test_fnmatch.rb	(revision 39412)
+++ test/ruby/test_fnmatch.rb	(revision 39413)
@@ -109,4 +109,19 @@ class TestFnmatch < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fnmatch.rb#L109
     assert_file.for(feature5422).not_fnmatch?( "{.g,t}*", ".gem")
     assert_file.for(feature5422).fnmatch?("{.g,t}*", ".gem", File::FNM_EXTGLOB)
   end
+
+  def test_unmatched_encoding
+    bug7911 = '[ruby-dev:47069] [Bug #7911]'
+    path = "\u{3042}"
+    pattern_ascii = 'a'.encode('US-ASCII')
+    pattern_eucjp = path.encode('EUC-JP')
+    assert_nothing_raised(ArgumentError, bug7911) do
+      assert(!File.fnmatch(pattern_ascii, path))
+      assert(!File.fnmatch(pattern_eucjp, path))
+      assert(!File.fnmatch(pattern_ascii, path, File::FNM_CASEFOLD))
+      assert(!File.fnmatch(pattern_eucjp, path, File::FNM_CASEFOLD))
+      assert(File.fnmatch("{*,#{pattern_ascii}}", path, File::FNM_EXTGLOB))
+      assert(File.fnmatch("{*,#{pattern_eucjp}}", path, File::FNM_EXTGLOB))
+    end
+  end
 end

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

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