]> granicus.if.org Git - php/commitdiff
Fix issue with SplFileInfo::getExtension() on files with only a leading '.' character
authorAnthony Ferrara <ircmaxell@gmail.com>
Mon, 13 Jul 2015 17:12:45 +0000 (13:12 -0400)
committerAnthony Ferrara <ircmaxell@gmail.com>
Mon, 13 Jul 2015 17:12:45 +0000 (13:12 -0400)
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.

ext/spl/spl_directory.c
ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt [new file with mode: 0644]

index bf23c644c91c86bb059947454d1113fa8f54990d..0ee1ae0530c92c48c3300ff6fbcb81d16a09ac67 100644 (file)
@@ -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 (file)
index 0000000..0ade304
--- /dev/null
@@ -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