dir.c (ruby_brace_expand): RB_GC_GUARD safety

The lifetime of a String VALUE must match or exceed the lifetime
of its R*_PTR result; otherwise the GC can while reclaim the
VALUE while the R*_PTR result is in use.

* dir.c (ruby_brace_expand): add var parameter for GC guard
  (ruby_brace_glob_with_enc): adjust call
  (file_s_fnmatch): ditto
  (push_glob): remove misplaced GC guard

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
normal 2017-07-25 05:55:22 +00:00
parent d822aa2998
commit 8e2d0deb88

12
dir.c
View File

@ -2268,7 +2268,7 @@ push_pattern(const char *path, VALUE ary, void *enc)
static int static int
ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg, ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
rb_encoding *enc) rb_encoding *enc, VALUE var)
{ {
const int escape = !(flags & FNM_NOESCAPE); const int escape = !(flags & FNM_NOESCAPE);
const char *p = str; const char *p = str;
@ -2313,7 +2313,7 @@ ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
} }
memcpy(buf+shift, t, p-t); memcpy(buf+shift, t, p-t);
strlcpy(buf+shift+(p-t), rbrace+1, len-(shift+(p-t))); strlcpy(buf+shift+(p-t), rbrace+1, len-(shift+(p-t)));
status = ruby_brace_expand(buf, flags, func, arg, enc); status = ruby_brace_expand(buf, flags, func, arg, enc, var);
if (status) break; if (status) break;
} }
GLOB_FREE(buf); GLOB_FREE(buf);
@ -2322,6 +2322,7 @@ ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
status = glob_call_func(func, s, arg, enc); status = glob_call_func(func, s, arg, enc);
} }
RB_GC_GUARD(var);
return status; return status;
} }
@ -2349,7 +2350,7 @@ ruby_brace_glob_with_enc(const char *str, int flags, ruby_glob_func *func, VALUE
args.funcs.error = NULL; args.funcs.error = NULL;
args.value = arg; args.value = arg;
args.flags = flags; args.flags = flags;
return ruby_brace_expand(str, flags, glob_brace, (VALUE)&args, enc); return ruby_brace_expand(str, flags, glob_brace, (VALUE)&args, enc, Qfalse);
} }
int int
@ -2409,9 +2410,8 @@ push_glob(VALUE ary, VALUE str, VALUE base, int flags)
enc = rb_utf8_encoding(); enc = rb_utf8_encoding();
#endif #endif
RB_GC_GUARD(str);
return ruby_brace_expand(RSTRING_PTR(str), flags, return ruby_brace_expand(RSTRING_PTR(str), flags,
push_caller, (VALUE)&args, enc); push_caller, (VALUE)&args, enc, str);
} }
static VALUE static VALUE
@ -2905,7 +2905,7 @@ file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
args.value = path; args.value = path;
args.flags = flags; args.flags = flags;
if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace, if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
(VALUE)&args, rb_enc_get(pattern)) > 0) (VALUE)&args, rb_enc_get(pattern), pattern) > 0)
return Qtrue; return Qtrue;
} }
else { else {