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

ruby-changes:41642

From: nobu <ko1@a...>
Date: Tue, 2 Feb 2016 14:42:39 +0900 (JST)
Subject: [ruby-changes:41642] nobu:r53716 (trunk): win32.c: reuse fullpath buffer

nobu	2016-02-02 14:43:44 +0900 (Tue, 02 Feb 2016)

  New Revision: 53716

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

  Log:
    win32.c: reuse fullpath buffer
    
    * win32/win32.c (open_dir_handle): reuse the fullpath buffer
      instead of allocating another buffer for copy.

  Modified files:
    trunk/win32/win32.c
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 53715)
+++ win32/win32.c	(revision 53716)
@@ -1869,12 +1869,9 @@ static HANDLE https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1869
 open_dir_handle(const WCHAR *filename, WIN32_FIND_DATAW *fd)
 {
     HANDLE fh;
-    static const WCHAR wildcard[] = L"\\*";
-    WCHAR fullname[MAX_PATH];
-    WCHAR *scanname;
+    WCHAR fullname[_MAX_PATH + rb_strlen_lit("\\*")];
     WCHAR *p;
     int len = 0;
-    VALUE v;
 
     //
     // Create the search pattern
@@ -1882,28 +1879,26 @@ open_dir_handle(const WCHAR *filename, W https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L1879
 
     fh = open_special(filename, 0, 0);
     if (fh != INVALID_HANDLE_VALUE) {
-	len = get_final_path(fh, fullname, numberof(fullname), 0);
+	len = get_final_path(fh, fullname, _MAX_PATH, 0);
 	CloseHandle(fh);
     }
-    if (len) {
-	filename = fullname;
-    }
-    else {
+    if (!len) {
 	len = lstrlenW(filename);
+	if (len >= _MAX_PATH) {
+	    errno = ENAMETOOLONG;
+	    return INVALID_HANDLE_VALUE;
+	}
+	MEMCPY(fullname, filename, WCHAR, len);
     }
-    scanname = ALLOCV_N(WCHAR, v, len + numberof(wildcard));
-    lstrcpyW(scanname, filename);
-    p = CharPrevW(scanname, scanname + len);
-    if (*p == L'/' || *p == L'\\' || *p == L':')
-	lstrcatW(scanname, wildcard + 1);
-    else
-	lstrcatW(scanname, wildcard);
+    p = &fullname[len-1];
+    if (!(isdirsep(*p) || *p == L':')) *++p = L'\\';
+    *++p = L'*';
+    *++p = L'\0';
 
     //
     // do the FindFirstFile call
     //
-    fh = FindFirstFileW(scanname, fd);
-    ALLOCV_END(v);
+    fh = FindFirstFileW(fullname, fd);
     if (fh == INVALID_HANDLE_VALUE) {
 	errno = map_errno(GetLastError());
     }

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

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