Skip to content

Commit

Permalink
Fix GH-11338: SplFileInfo empty getBasename with more than one slash
Browse files Browse the repository at this point in the history
Regressed in 13e4ce3.

Closes GH-11340.
  • Loading branch information
nielsdos committed May 30, 2023
1 parent 761b9a4 commit bce5360
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS
Expand Up @@ -16,6 +16,10 @@ PHP NEWS
. Fix allocation loop in zend_shared_alloc_startup(). (nielsdos)
. Access violation on smm_shared_globals with ALLOC_FALLBACK. (KoudelkaB)

- SPL:
. Fixed bug GH-11338 (SplFileInfo empty getBasename with more than one
slash). (nielsdos)

- Standard:
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
. Fix exception handling in array_multisort(). (ilutov)
Expand Down
4 changes: 3 additions & 1 deletion ext/spl/spl_directory.c
Expand Up @@ -432,7 +432,9 @@ static void spl_filesystem_info_set_filename(spl_filesystem_object *intern, zend

path_len = ZSTR_LEN(path);
if (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
path_len--;
do {
path_len--;
} while (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len - 1));
intern->file_name = zend_string_init(ZSTR_VAL(path), path_len, 0);
} else {
intern->file_name = zend_string_copy(path);
Expand Down
47 changes: 47 additions & 0 deletions ext/spl/tests/gh11338.phpt
@@ -0,0 +1,47 @@
--TEST--
GH-11338 (SplFileInfo empty getBasename with more than on slash)
--FILE--
<?php

function test($path) {
echo "Testing: '$path'\n";
$file = new \SplFileInfo($path);
var_dump($file->getBasename());
var_dump($file->getFilename());
}

test('/dir/anotherdir/basedir//');
test('/dir/anotherdir/basedir/');
test('/dir/anotherdir/basedir');
test('/dir/anotherdir//basedir');
test('///');
test('//');
test('/');
test('');

?>
--EXPECT--
Testing: '/dir/anotherdir/basedir//'
string(7) "basedir"
string(7) "basedir"
Testing: '/dir/anotherdir/basedir/'
string(7) "basedir"
string(7) "basedir"
Testing: '/dir/anotherdir/basedir'
string(7) "basedir"
string(7) "basedir"
Testing: '/dir/anotherdir//basedir'
string(7) "basedir"
string(7) "basedir"
Testing: '///'
string(0) ""
string(1) "/"
Testing: '//'
string(0) ""
string(1) "/"
Testing: '/'
string(0) ""
string(1) "/"
Testing: ''
string(0) ""
string(0) ""

0 comments on commit bce5360

Please sign in to comment.