From 4b78636f3f6c12c1ad0625d275bc0239ca4eb5b5 Mon Sep 17 00:00:00 2001 From: Anthony Ferrara Date: Mon, 13 Jul 2015 13:12:45 -0400 Subject: [PATCH] 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. --- ext/spl/spl_directory.c | 5 +++-- .../tests/spl_fileinfo_getextension_leadingdot.phpt | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 ext/spl/tests/spl_fileinfo_getextension_leadingdot.phpt 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 -- 2.40.0