Skip to content

Commit ac82a34

Browse files
committed
rewrite the getcwd part
Also fixes a possible memory leak. Still not ideal, as seems CWD longer than MAX_PATH is still not supported. But a heap allocation is not needed anyway, as MAXPATHLEN value is the maximum supported.
1 parent 7020e6f commit ac82a34

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

Zend/zend_virtual_cwd.c

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -328,32 +328,19 @@ CWD_API int php_sys_stat_ex(const char *path, zend_stat_t *buf, int lstat) /* {{
328328
buf->st_dev = buf->st_rdev = 0;
329329
} else {
330330
wchar_t cur_path[MAXPATHLEN+1];
331-
DWORD len = sizeof(cur_path);
332-
wchar_t *tmp = cur_path;
333-
334-
while(1) {
335-
DWORD r = GetCurrentDirectoryW(len, tmp);
336-
if (r < len) {
337-
if (tmp[1] == L':') {
338-
if (pathw[0] >= L'A' && pathw[0] <= L'Z') {
339-
buf->st_dev = buf->st_rdev = pathw[0] - L'A';
340-
} else {
341-
buf->st_dev = buf->st_rdev = pathw[0] - L'a';
342-
}
331+
332+
if (NULL != _wgetcwd(cur_path, sizeof(cur_path)/sizeof(wchar_t))) {
333+
if (cur_path[1] == L':') {
334+
if (pathw[0] >= L'A' && pathw[0] <= L'Z') {
335+
buf->st_dev = buf->st_rdev = pathw[0] - L'A';
343336
} else {
344-
buf->st_dev = buf->st_rdev = -1;
337+
buf->st_dev = buf->st_rdev = pathw[0] - L'a';
345338
}
346-
break;
347-
} else if (!r) {
348-
buf->st_dev = buf->st_rdev = -1;
349-
break;
350339
} else {
351-
len = r+1;
352-
tmp = (wchar_t*)malloc(len*sizeof(wchar_t));
340+
buf->st_dev = buf->st_rdev = -1;
353341
}
354-
}
355-
if (tmp != cur_path) {
356-
free(tmp);
342+
} else {
343+
buf->st_dev = buf->st_rdev = -1;
357344
}
358345
}
359346

0 commit comments

Comments
 (0)