Browse Source

Fix issue with SplFileInfo::getExtension() on files with only a leading '.' character

Currently, there is an assert() that fails on files like .gitignore crashing PHP. This patch fixes that.
Instead, now an empty string is returned (since the file has no extension). A test has been added to test this behavior.
pull/1417/head
Anthony Ferrara 11 years ago
parent
commit
4b78636f3f
  1. 5
      ext/spl/spl_directory.c
  2. 13
      ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt

5
ext/spl/spl_directory.c

@ -939,8 +939,9 @@ SPL_METHOD(SplFileInfo, getExtension)
ret = php_basename(fname, flen, NULL, 0);
p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
if (p) {
assert(p > ZSTR_VAL(ret));
if (p && p > ZSTR_VAL(ret)) {
/* Check for the string length, incase the only '.' is the
* first character of the string */
idx = (int)(p - ZSTR_VAL(ret));
RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
zend_string_release(ret);

13
ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt

@ -0,0 +1,13 @@
--TEST--
SPL: Spl File Info test getExtension with leading dot
--FILE--
<?php
$file = __DIR__ . '/.test';
touch($file);
$fileInfo = new SplFileInfo($file);
var_dump($fileInfo->getExtension());
unlink($file);
?>
--EXPECT--
string(0) ""
Loading…
Cancel
Save