[前][次][番号順一覧][スレッド一覧]

ruby-changes:50827

From: nobu <ko1@a...>
Date: Thu, 29 Mar 2018 14:56:09 +0900 (JST)
Subject: [ruby-changes:50827] nobu:r63034 (trunk): dir.c: do not assume NUL terminator

nobu	2018-03-29 14:56:04 +0900 (Thu, 29 Mar 2018)

  New Revision: 63034

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63034

  Log:
    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.

  Modified files:
    trunk/dir.c
Index: dir.c
===================================================================
--- dir.c	(revision 63033)
+++ dir.c	(revision 63034)
@@ -2534,6 +2534,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/dir.c#L2534
 rb_push_glob(VALUE str, VALUE base, int flags) /* '\0' is delimiter */
 {
     long offset = 0;
+    long len;
     VALUE ary;
 
     /* can contain null bytes as separators */
@@ -2546,19 +2547,21 @@ rb_push_glob(VALUE str, VALUE base, int https://github.com/ruby/ruby/blob/trunk/dir.c#L2547
     }
     ary = rb_ary_new();
 
-    while (offset < RSTRING_LEN(str)) {
-	char *p, *pend;
+    while (offset < (len = RSTRING_LEN(str))) {
 	int status;
-	p = RSTRING_PTR(str) + offset;
-	status = push_glob(ary, rb_enc_str_new(p, strlen(p), rb_enc_get(str)),
+        long rest = len - offset;
+        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);
 	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;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]