Skip to content

Commit a1f5c8a

Browse files
committed
Fix GH-9227: Trailing dots and spaces in filenames are ignored
Given that Windows ignores trailing dots and spaces in filenames, we catch that ourselves to avoid confusion with the respective filenames without these characters. Closes GH-9229.
1 parent 1109989 commit a1f5c8a

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed bug GH-9323 (Crash in ZEND_RETURN/GC/zend_call_function)
77
(Tim Starling)
8+
. Fixed bug GH-9227 (Trailing dots and spaces in filenames are ignored).
9+
(cmb)
810

911
- DOM:
1012
. Fixed bug #79451 (Using DOMDocument->replaceChild on doctype causes

Zend/zend_virtual_cwd.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
603603
if (!pathw) {
604604
return (size_t)-1;
605605
}
606+
PHP_WIN32_IOUTIL_CHECK_PATH_W(pathw, (size_t)-1, 1);
606607
hFind = FindFirstFileExW(pathw, FindExInfoBasic, &dataw, FindExSearchNameMatch, NULL, 0);
607608
if (INVALID_HANDLE_VALUE == hFind) {
608609
if (use_realpath == CWD_REALPATH) {
@@ -1139,7 +1140,13 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
11391140
path_length = tsrm_realpath_r(resolved_path, start, path_length, &ll, &t, use_realpath, 0, NULL);
11401141

11411142
if (path_length == (size_t)-1) {
1143+
#ifdef ZEND_WIN32
1144+
if (errno != EACCES) {
1145+
errno = ENOENT;
1146+
}
1147+
#else
11421148
errno = ENOENT;
1149+
#endif
11431150
return 1;
11441151
}
11451152

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
SPL: SplFileInfo::getExtension() basic test
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY !== "Windows") die("skip only for Windows");
6+
?>
7+
--FILE--
8+
<?php
9+
$file = md5('SplFileInfo::getExtension');
10+
$exts = array('.txt', '.extension', '..', '');
11+
foreach ($exts as $ext) {
12+
touch($file . $ext);
13+
$info = new SplFileInfo($file . $ext);
14+
var_dump($info->getExtension(), pathinfo($file . $ext, PATHINFO_EXTENSION));
15+
}
16+
?>
17+
--CLEAN--
18+
<?php
19+
$file = md5('SplFileInfo::getExtension');
20+
$exts = array('.txt', '.extension', '..', '');
21+
foreach ($exts as $ext) {
22+
@unlink($file . $ext);
23+
}
24+
?>
25+
--EXPECT--
26+
string(3) "txt"
27+
string(3) "txt"
28+
string(9) "extension"
29+
string(9) "extension"
30+
string(0) ""
31+
string(0) ""
32+
string(0) ""
33+
string(0) ""

ext/spl/tests/SplFileInfo_getExtension_basic.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
--TEST--
22
SPL: SplFileInfo::getExtension() basic test
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY === "Windows") die("skip not for Windows");
6+
?>
37
--FILE--
48
<?php
59
$file = md5('SplFileInfo::getExtension');
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug GH-9227 (Trailing dots and spaces in filenames are ignored)
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY !== "Windows") die("skip for Windows only");
6+
?>
7+
--FILE--
8+
<?php
9+
var_dump(realpath(__DIR__ . "/gh9227.txt."));
10+
var_dump(file_put_contents(__DIR__ . "/gh9227.txt", "bar"));
11+
var_dump(realpath(__DIR__ . "/gh9227.txt."));
12+
?>
13+
--CLEAN--
14+
<?php
15+
@unlink(__DIR__ . "/gh9227.txt");
16+
?>
17+
--EXPECT--
18+
bool(false)
19+
int(3)
20+
bool(false)

0 commit comments

Comments
 (0)