From: Anthony Ferrara Date: Mon, 13 Jul 2015 17:12:45 +0000 (-0400) Subject: Fix issue with SplFileInfo::getExtension() on files with only a leading '.' character X-Git-Tag: php-7.1.1RC1~35^2~8^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4b78636f3f6c12c1ad0625d275bc0239ca4eb5b5;p=php 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. --- diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index bf23c644c9..0ee1ae0530 100644 --- a/ext/spl/spl_directory.c +++ b/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); diff --git a/ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt b/ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt new file mode 100644 index 0000000000..0ade304a35 --- /dev/null +++ b/ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt @@ -0,0 +1,13 @@ +--TEST-- +SPL: Spl File Info test getExtension with leading dot +--FILE-- +getExtension()); +unlink($file); +?> +--EXPECT-- +string(0) "" \ No newline at end of file