Skip to content

Commit 5f96a41

Browse files
committed
Fix phpGH-11338: SplFileInfo empty getBasename with more than on slash
Regressed in 13e4ce3.
1 parent c473787 commit 5f96a41

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

ext/spl/spl_directory.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,9 @@ static void spl_filesystem_info_set_filename(spl_filesystem_object *intern, zend
432432

433433
path_len = ZSTR_LEN(path);
434434
if (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
435-
path_len--;
435+
do {
436+
path_len--;
437+
} while (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len - 1));
436438
intern->file_name = zend_string_init(ZSTR_VAL(path), path_len, 0);
437439
} else {
438440
intern->file_name = zend_string_copy(path);

ext/spl/tests/gh11338.phpt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
GH-11338 (SplFileInfo empty getBasename with more than on slash)
3+
--FILE--
4+
<?php
5+
6+
function test($path) {
7+
echo "Testing: '$path'\n";
8+
$file = new \SplFileInfo($path);
9+
var_dump($file->getBasename());
10+
var_dump($file->getFilename());
11+
}
12+
13+
test('/dir/anotherdir/basedir//');
14+
test('/dir/anotherdir/basedir/');
15+
test('/dir/anotherdir/basedir');
16+
test('/dir/anotherdir//basedir');
17+
test('///');
18+
test('//');
19+
test('/');
20+
test('');
21+
22+
?>
23+
--EXPECT--
24+
Testing: '/dir/anotherdir/basedir//'
25+
string(7) "basedir"
26+
string(7) "basedir"
27+
Testing: '/dir/anotherdir/basedir/'
28+
string(7) "basedir"
29+
string(7) "basedir"
30+
Testing: '/dir/anotherdir/basedir'
31+
string(7) "basedir"
32+
string(7) "basedir"
33+
Testing: '/dir/anotherdir//basedir'
34+
string(7) "basedir"
35+
string(7) "basedir"
36+
Testing: '///'
37+
string(0) ""
38+
string(1) "/"
39+
Testing: '//'
40+
string(0) ""
41+
string(1) "/"
42+
Testing: '/'
43+
string(0) ""
44+
string(1) "/"
45+
Testing: ''
46+
string(0) ""
47+
string(0) ""

0 commit comments

Comments
 (0)