* 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
This commit is contained in:
nobu 2013-02-23 00:48:44 +00:00
Родитель 143a2f7ef1
Коммит 3331d6774f
3 изменённых файлов: 41 добавлений и 2 удалений

Просмотреть файл

@ -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

22
dir.c
Просмотреть файл

@ -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);

Просмотреть файл

@ -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