Skip to content

Commit f28aeae

Browse files
committed
Merge branch 'PHP-7.1' into PHP-7.2
* PHP-7.1: Fixed RecursiveDirectoryIterator with long path or with edge case length
2 parents 77ced46 + 99fe185 commit f28aeae

File tree

2 files changed

+103
-9
lines changed

2 files changed

+103
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
--TEST--
2+
RecursiveDirectoryIterator with dir path long or of edge case length
3+
--SKIPIF--
4+
<?php
5+
include dirname(__FILE__) . DIRECTORY_SEPARATOR . "util.inc";
6+
7+
skip_if_not_win();
8+
9+
if (strlen(dirname(__FILE__)) > 259) die("Unsuitable starting path length");
10+
?>
11+
--FILE--
12+
<?php
13+
14+
$need_len = 1024;
15+
//$need_len = 259;
16+
$dir = dirname(__FILE__);
17+
while ($need_len - strlen($dir) > 32) {
18+
$dir .= DIRECTORY_SEPARATOR . str_repeat("a", 32);
19+
}
20+
$dir .= DIRECTORY_SEPARATOR . str_repeat("a", $need_len - strlen($dir));
21+
mkdir($dir, 0700, true);
22+
23+
$fl = $dir . DIRECTORY_SEPARATOR . "hello.txt";
24+
file_put_contents($fl, "");
25+
26+
27+
$start = substr($dir, 0, strpos($dir, DIRECTORY_SEPARATOR, strlen(dirname(__FILE__))+1));
28+
$iter = new RecursiveIteratorIterator(
29+
new RecursiveDirectoryIterator(
30+
$start,
31+
FilesystemIterator::SKIP_DOTS
32+
),
33+
RecursiveIteratorIterator::CHILD_FIRST
34+
);
35+
36+
foreach ($iter as $item) {
37+
if (!$item->isDir()) {
38+
var_dump($item->getPathname());
39+
}
40+
}
41+
42+
$iter->rewind();
43+
foreach ($iter as $item) {
44+
if ($item->isDir()) {
45+
rmdir($item->getPathname());
46+
} else {
47+
unlink($item->getPathname());
48+
}
49+
}
50+
rmdir($start);
51+
var_dump(file_exists($start));
52+
53+
/*unlink($fl);
54+
do {
55+
rmdir($dir);
56+
$dir = dirname($dir);
57+
} while (dirname(__FILE__) != $dir);*/
58+
59+
?>
60+
==DONE==
61+
--EXPECTF--
62+
string(%d) "%shello.txt"
63+
bool(false)
64+
==DONE==
65+

win32/readdir.c

+38-9
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ DIR *opendir(const char *dir)
2929
DIR *dp;
3030
wchar_t *filespecw, *resolvedw;
3131
HANDLE handle;
32-
int index;
3332
char resolved_path_buff[MAXPATHLEN];
34-
size_t resolvedw_len, filespecw_len;
33+
size_t resolvedw_len, filespecw_len, index;
34+
zend_bool might_need_prefix;
3535

3636
if (!VCWD_REALPATH(dir, resolved_path_buff)) {
3737
return NULL;
@@ -48,16 +48,27 @@ DIR *opendir(const char *dir)
4848
return NULL;
4949
}
5050

51+
might_need_prefix = resolvedw_len >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(resolvedw[0]) && L':' == resolvedw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(resolvedw[2]);
52+
5153
filespecw_len = resolvedw_len + 2;
54+
if (filespecw_len >= _MAX_PATH && might_need_prefix) {
55+
filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
56+
}
5257
filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
5358
if (filespecw == NULL) {
5459
free(dp);
5560
free(resolvedw);
5661
return NULL;
5762
}
5863

59-
wcscpy(filespecw, resolvedw);
60-
index = (int)filespecw_len - 1;
64+
if (filespecw_len >= _MAX_PATH && might_need_prefix) {
65+
wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
66+
wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, resolvedw);
67+
index = resolvedw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
68+
} else {
69+
wcscpy(filespecw, resolvedw);
70+
index = resolvedw_len - 1;
71+
}
6172
if (index >= 0 && filespecw[index] == L'/' || index == 0 && filespecw[index] == L'\\')
6273
filespecw[index] = L'\0';
6374
wcscat(filespecw, L"\\*");
@@ -178,24 +189,42 @@ int rewinddir(DIR *dp)
178189
/* Re-set to the beginning */
179190
wchar_t *filespecw;
180191
HANDLE handle;
181-
int index;
192+
size_t dirw_len, filespecw_len, index;
193+
zend_bool might_need_prefix;
182194

183195
FindClose(dp->handle);
184196

185197
dp->offset = 0;
186198
dp->finished = 0;
187199

188-
filespecw = (wchar_t *)malloc((wcslen((wchar_t *)dp->dirw) + 2 + 1)*sizeof(wchar_t));
200+
/* XXX save the dir len into the struct. */
201+
dirw_len = wcslen((wchar_t *)dp->dirw);
202+
203+
might_need_prefix = dirw_len >= 3 && PHP_WIN32_IOUTIL_IS_LETTERW(dp->dirw[0]) && L':' == dp->dirw[1] && PHP_WIN32_IOUTIL_IS_SLASHW(dp->dirw[2]);
204+
205+
filespecw_len = dirw_len + 2;
206+
if (filespecw_len >= _MAX_PATH && might_need_prefix) {
207+
filespecw_len += PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW;
208+
}
209+
210+
filespecw = (wchar_t *)malloc((filespecw_len + 1)*sizeof(wchar_t));
189211
if (filespecw == NULL) {
190212
return -1;
191213
}
192214

193-
wcscpy(filespecw, (wchar_t *)dp->dirw);
194-
index = (int)wcslen(filespecw) - 1;
215+
if (filespecw_len >= _MAX_PATH && might_need_prefix) {
216+
wcscpy(filespecw, PHP_WIN32_IOUTIL_LONG_PATH_PREFIXW);
217+
wcscpy(filespecw + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW, dp->dirw);
218+
index = dirw_len + PHP_WIN32_IOUTIL_LONG_PATH_PREFIX_LENW - 1;
219+
} else {
220+
wcscpy(filespecw, dp->dirw);
221+
index = dirw_len - 1;
222+
}
223+
195224
if (index >= 0 && (filespecw[index] == L'/' ||
196225
(filespecw[index] == L'\\' && index == 0)))
197226
filespecw[index] = L'\0';
198-
wcscat(filespecw, L"/*");
227+
wcscat(filespecw, L"\\*");
199228

200229
if ((handle = FindFirstFileExW(filespecw, FindExInfoBasic, &(dp->fileinfo), FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH)) == INVALID_HANDLE_VALUE) {
201230
dp->finished = 1;

0 commit comments

Comments
 (0)