Skip to content

Commit bce5360

Browse files
committed
Fix GH-11338: SplFileInfo empty getBasename with more than one slash
Regressed in 13e4ce3. Closes GH-11340.
1 parent 761b9a4 commit bce5360

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PHP NEWS
1616
. Fix allocation loop in zend_shared_alloc_startup(). (nielsdos)
1717
. Access violation on smm_shared_globals with ALLOC_FALLBACK. (KoudelkaB)
1818

19+
- SPL:
20+
. Fixed bug GH-11338 (SplFileInfo empty getBasename with more than one
21+
slash). (nielsdos)
22+
1923
- Standard:
2024
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
2125
. Fix exception handling in array_multisort(). (ilutov)

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)