Skip to content

Commit a963b1f

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix GH-12721: SplFileInfo::getFilename() segfault in combination with GlobIterator and no directory separator
2 parents 77ea5c5 + 0a3b891 commit a963b1f

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

ext/spl/spl_directory.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,16 @@ PHP_METHOD(SplFileInfo, getFilename)
913913

914914
path = spl_filesystem_object_get_path(intern);
915915

916-
ZEND_ASSERT(path);
917916
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
918917
/* +1 to skip the trailing / of the path in the file name */
919918
size_t path_len = ZSTR_LEN(path) + 1;
920919
RETVAL_STRINGL(ZSTR_VAL(intern->file_name) + path_len, ZSTR_LEN(intern->file_name) - path_len);
921920
} else {
922921
RETVAL_STR_COPY(intern->file_name);
923922
}
924-
zend_string_release_ex(path, /* persistent */ false);
923+
if (path) {
924+
zend_string_release_ex(path, /* persistent */ false);
925+
}
925926
}
926927
/* }}} */
927928

@@ -961,14 +962,16 @@ PHP_METHOD(SplFileInfo, getExtension)
961962

962963
path = spl_filesystem_object_get_path(intern);
963964

964-
if (ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
965+
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
965966
fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
966967
flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
967968
} else {
968969
fname = ZSTR_VAL(intern->file_name);
969970
flen = ZSTR_LEN(intern->file_name);
970971
}
971-
zend_string_release_ex(path, /* persistent */ false);
972+
if (path) {
973+
zend_string_release_ex(path, /* persistent */ false);
974+
}
972975

973976
ret = php_basename(fname, flen, NULL, 0);
974977

@@ -1040,7 +1043,9 @@ PHP_METHOD(SplFileInfo, getBasename)
10401043
fname = ZSTR_VAL(intern->file_name);
10411044
flen = ZSTR_LEN(intern->file_name);
10421045
}
1043-
zend_string_release_ex(path, /* persistent */ false);
1046+
if (path) {
1047+
zend_string_release_ex(path, /* persistent */ false);
1048+
}
10441049

10451050
RETURN_STR(php_basename(fname, flen, suffix, slen));
10461051
}

ext/spl/tests/gh12721.phpt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
GH-12721 (SplFileInfo::getFilename() segfault in combination with GlobIterator and no directory separator)
3+
--FILE--
4+
<?php
5+
6+
file_put_contents('file1.gh12721', 'hello');
7+
8+
echo "--- No slash ---\n";
9+
10+
foreach (new GlobIterator('*.gh12721') as $fileInfo) {
11+
echo $fileInfo->getFilename(), "\n";
12+
echo $fileInfo->getExtension(), "\n";
13+
echo $fileInfo->getBasename(), "\n";
14+
var_dump($fileInfo->getFileInfo());
15+
}
16+
17+
echo "--- With slash ---\n";
18+
19+
foreach (new GlobIterator('./*.gh12721') as $fileInfo) {
20+
echo $fileInfo->getFilename(), "\n";
21+
echo $fileInfo->getExtension(), "\n";
22+
echo $fileInfo->getBasename(), "\n";
23+
var_dump($fileInfo->getFileInfo());
24+
}
25+
26+
?>
27+
--CLEAN--
28+
<?php
29+
@unlink('file1.gh12721');
30+
?>
31+
--EXPECTF--
32+
--- No slash ---
33+
file1.gh12721
34+
gh12721
35+
file1.gh12721
36+
object(SplFileInfo)#4 (2) {
37+
["pathName":"SplFileInfo":private]=>
38+
string(13) "file1.gh12721"
39+
["fileName":"SplFileInfo":private]=>
40+
string(13) "file1.gh12721"
41+
}
42+
--- With slash ---
43+
file1.gh12721
44+
gh12721
45+
file1.gh12721
46+
object(SplFileInfo)#3 (2) {
47+
["pathName":"SplFileInfo":private]=>
48+
string(15) "%sfile1.gh12721"
49+
["fileName":"SplFileInfo":private]=>
50+
string(13) "file1.gh12721"
51+
}

0 commit comments

Comments
 (0)