From 03c1d8982a592ba40d61a9ba1213860f7c734583 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Tue, 19 Mar 2019 21:21:02 +0900 Subject: [PATCH] dir.c: fix Dir.glob starts with brace * dir.c (ruby_glob0): expand braces if a glob pattern starts with brace. [ruby-core:91728] [Bug #15649] --- dir.c | 29 +++++++++++++++++++++++++++++ test/ruby/test_dir.rb | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/dir.c b/dir.c index 7e5b80793f..3348fd59e8 100644 --- a/dir.c +++ b/dir.c @@ -2433,6 +2433,24 @@ push_caller(const char *path, VALUE val, void *enc) return status; } +static int ruby_glob0(const char *path, int fd, const char *base, int flags, + const ruby_glob_funcs_t *funcs, VALUE arg, rb_encoding *enc); + +struct push_glob0_args { + int fd; + const char *base; + int flags; + const ruby_glob_funcs_t *funcs; + VALUE arg; +}; + +static int +push_glob0_caller(const char *path, VALUE val, void *enc) +{ + struct push_glob0_args *arg = (struct push_glob0_args *)val; + return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc); +} + static int ruby_glob0(const char *path, int fd, const char *base, int flags, const ruby_glob_funcs_t *funcs, VALUE arg, @@ -2445,6 +2463,17 @@ ruby_glob0(const char *path, int fd, const char *base, int flags, int status, dirsep = FALSE; start = root = path; + + if (*root == '{') { + struct push_glob0_args args; + args.fd = fd; + args.base = base; + args.flags = flags; + args.funcs = funcs; + args.arg = arg; + return ruby_brace_expand(path, flags, push_glob0_caller, (VALUE)&args, enc, Qfalse); + } + flags |= FNM_SYSCASE; #if defined DOSISH root = rb_enc_path_skip_prefix(root, root + strlen(root), enc); diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 51ea8e95e0..93ae3c3ed9 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -205,6 +205,14 @@ def test_glob_recursive_directory end end + def test_glob_starts_with_brace + Dir.chdir(@root) do + bug15649 = '[ruby-core:91728] [Bug #15649]' + assert_equal(["#{@root}/a", "#{@root}/b"], + Dir.glob("{#{@root}/a,#{@root}/b}"), bug15649) + end + end + if Process.const_defined?(:RLIMIT_NOFILE) def test_glob_too_may_open_files assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}", chdir: @root) -- 2.21.0.windows.1