dir.c: do not assume NUL terminator

* dir.c (rb_push_glob): do not assume string is NUL terminated
  always, shared substring may not in the future.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63034 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-03-29 05:56:04 +00:00
Родитель 9c5d90b516
Коммит 97e6aaca7c
1 изменённых файлов: 13 добавлений и 10 удалений

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

@ -2534,6 +2534,7 @@ static VALUE
rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */ rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */
{ {
long offset = 0; long offset = 0;
long len;
VALUE ary; VALUE ary;
/* can contain null bytes as separators */ /* can contain null bytes as separators */
@ -2546,19 +2547,21 @@ rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */
} }
ary = rb_ary_new(); ary = rb_ary_new();
while (offset < RSTRING_LEN(str)) { while (offset < (len = RSTRING_LEN(str))) {
char *p, *pend;
int status; int status;
p = RSTRING_PTR(str) + offset; long rest = len - offset;
status = push_glob(ary, rb_enc_str_new(p, strlen(p), rb_enc_get(str)), const char *pbeg = RSTRING_PTR(str), *p = pbeg + offset;
const char *pend = memchr(p, '\0', rest);
if (pend) {
rest = ++pend - p;
offset = pend - pbeg;
}
else {
offset = len;
}
status = push_glob(ary, rb_str_subseq(str, p-pbeg, rest),
base, flags); base, flags);
if (status) GLOB_JUMP_TAG(status); if (status) GLOB_JUMP_TAG(status);
if (offset >= RSTRING_LEN(str)) break;
p += strlen(p) + 1;
pend = RSTRING_PTR(str) + RSTRING_LEN(str);
while (p < pend && !*p)
p++;
offset = p - RSTRING_PTR(str);
} }
return ary; return ary;