summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-02 05:43:44 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-02 05:43:44 +0000
commiteee616315d406e91a232c33434f5195d72578b65 (patch)
tree7f961ead1fc7f6950088aabe596d8205dd489187
parent21daa56b2aa2d4afc6e61b0e6b0f4dd78df620ca (diff)
win32.c: reuse fullpath buffer
* win32/win32.c (open_dir_handle): reuse the fullpath buffer instead of allocating another buffer for copy. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53716 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--win32/win32.c31
1 files changed, 13 insertions, 18 deletions
diff --git a/win32/win32.c b/win32/win32.c
index 39ff749..265e6d4 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -1869,12 +1869,9 @@ static HANDLE
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, WIN32_FIND_DATAW *fd)
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());
}