Skip to content

[SPL] Fix FilesystemIterator::FOLLOW_SYMLINKS bug #6695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

camporter
Copy link
Contributor

FilesystemIterator::FOLLOW_SYMLINKS is currently treated as a directory
key mode flag, even though it does not change the way that the key
during iteration is determined. To address this, SPL_FILE_DIR_KEY has been
changed to exclude masking FOLLOW_SYMLINKS for comparison.

Addresses bug 80724, though there might be disagreement about the intent of the FOLLOW_SYMLINKS flag.

Submitted against master because this is technically a BC break.

FilesystemIterator::FOLLOW_SYMLINKS is currently treated as a directory
key mode flag, even though it does not change the way that the key
during iteration is set. To address this, SPL_FILE_DIR_KEY has been
changed to exclude masking FOLLOW_SYMLINKS for comparison.

Addresses bug 80724.
@@ -138,7 +138,7 @@ static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_files
#define SPL_FILE_DIR_KEY_AS_FILENAME 0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
#define SPL_FILE_DIR_FOLLOW_SYMLINKS 0x00000200 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
#define SPL_FILE_DIR_KEY_MODE_MASK 0x00000F00 /* mask RecursiveDirectoryTree::key() */
#define SPL_FILE_DIR_KEY(intern,mode) ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)
#define SPL_FILE_DIR_KEY(intern,mode) ((intern->flags&SPL_FILE_DIR_KEY_AS_FILENAME)==mode)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to move SPL_FILE_DIR_FOLLOW_SYMLINKS down into the SPL_FILE_DIR_OTHERS_MASK category instead. As written, this macro doesn't really make sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in changing the value? I wasn't sure if that would be ok to do, unless it's fine since it's non BC anyway?

Currently SPL_FILE_DIR_KEY_MODE_MASK doesn't make sense, so changing the value would allow it to make sense again...

I can move it down too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for master, it's okay to change the value.

@nikic
Copy link
Member

nikic commented Feb 15, 2021

Looks like there's a memory leak in the added test:

005+ [Mon Feb 15 05:34:01 2021]  Script:  '/Users/runner/work/1/s/ext/spl/tests/bug80724.php'
006+ /Users/runner/work/1/s/Zend/zend_objects_API.h(91) :  Freeing 0x0000000112c7da00 (1208 bytes), script=/Users/runner/work/1/s/ext/spl/tests/bug80724.php
007+ Last leak repeated 1 time
008+ === Total 2 memory leaks detected ===

Most likely a pre-existing issue.

@nikic
Copy link
Member

nikic commented Feb 15, 2021

This is enough to cause a leak:

<?php
$iterator = new FilesystemIterator(__DIR__);
foreach ($iterator as $value) {
    break;
}   

@nikic
Copy link
Member

nikic commented Feb 15, 2021

The leak should be fixed with 44a80b6.

@camporter
Copy link
Contributor Author

Thank you, I didn't spend much time looking into the leak, glad it's fixed 👍

@@ -136,12 +136,12 @@ static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_files

#define SPL_FILE_DIR_KEY_AS_PATHNAME 0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
#define SPL_FILE_DIR_KEY_AS_FILENAME 0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
#define SPL_FILE_DIR_FOLLOW_SYMLINKS 0x00000200 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
#define SPL_FILE_DIR_KEY_MODE_MASK 0x00000F00 /* mask RecursiveDirectoryTree::key() */
#define SPL_FILE_DIR_KEY(intern,mode) ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)

#define SPL_FILE_DIR_SKIPDOTS 0x00001000 /* Tells whether it should skip dots or not */
#define SPL_FILE_DIR_UNIXPATHS 0x00002000 /* Whether to unixify path separators */
#define SPL_FILE_DIR_OTHERS_MASK 0x00003000 /* mask used for get/setFlags */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The OTHERS_MASK should now be 0x00007000 to include the FOLLOW_SYMLINKS flag as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Changed and updated tests.

@php-pulls php-pulls closed this in 24e7299 Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants