diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-23 00:48:44 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-02-23 00:48:44 +0000 |
commit | 3331d6774f355e261644d7042cdeee0f03db3bc8 (patch) | |
tree | 70a5eaf2c74e09287324337f6c099b5e5bef9d10 | |
parent | 143a2f7ef1a16e22709af87a227e9a6323aefd84 (diff) | |
download | ruby-3331d6774f355e261644d7042cdeee0f03db3bc8.tar.gz |
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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39413 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | dir.c | 22 | ||||
-rw-r--r-- | test/ruby/test_fnmatch.rb | 15 |
3 files changed, 41 insertions, 2 deletions
@@ -1,3 +1,9 @@ +Sat Feb 23 09:48:41 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * 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@marc-andre.ca> * doc/NEWS-*: Update NEWS from their respective branches @@ -1919,7 +1919,24 @@ fnmatch_brace(const char *pattern, VALUE val, void *enc) { 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, VALUE obj) 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); diff --git a/test/ruby/test_fnmatch.rb b/test/ruby/test_fnmatch.rb index d186638a7b..0b3f604df5 100644 --- a/test/ruby/test_fnmatch.rb +++ b/test/ruby/test_fnmatch.rb @@ -109,4 +109,19 @@ class TestFnmatch < Test::Unit::TestCase 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 |