com php-src: Fix issue with SplFileInfo::getExtension() on files with only a leading '.' character: ext/spl/spl_directory.c ext/spl/
tests/spl_fileinfo_getextension_leadingdot.phpt
| From: | Anthony Ferrara | Date: | Mon, 13 Jul 2015 17:12:45 +0000 |
| Subject: | com php-src: Fix issue with SplFileInfo::getExtension() on files with only a leading '.' character: ext/spl/spl_directory.c ext/spl/ tests/spl_fileinfo_getextension_leadingdot.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-87963@lists.php.net to get a copy of this message | ||
Commit: 4b78636f3f6c12c1ad0625d275bc0239ca4eb5b5
Author: Anthony Ferrara <ircmaxell@gmail.com> Mon, 13 Jul 2015 13:12:45 -0400
Parents: e10e0a02aae42d9c3cd4fdf16357a9badfd36a71
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=4b78636f3f6c12c1ad0625d275bc0239ca4eb5b5
Log:
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.
Changed paths:
M ext/spl/spl_directory.c
A ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt
Diff:
diff --git a/ext/spl/spl_directory..c b/ext/spl/spl_directory.c
index bf23c64..0ee1ae0 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 0000000..0ade304
--- /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--
+<?php
+$file = __DIR__ . '/.test';
+touch($file);
+$fileInfo = new SplFileInfo($file);
+
+var_dump($fileInfo->getExtension());
+unlink($file);
+?>
+--EXPECT--
+string(0) ""
\ No newline at end of file