Skip to content

Fix GlobIterator without constructor breaks count() #18314

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

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -1599,12 +1599,12 @@ PHP_METHOD(GlobIterator, count)
RETURN_THROWS();
}

/* The spl_filesystem_object_get_method_check() function is called prior to calling this function.
* Therefore, the directory entry cannot be NULL. However, if it is not NULL, then it must be a glob iterator
* by construction. */
ZEND_ASSERT(spl_intern_is_glob(intern));

RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
if (EXPECTED(spl_intern_is_glob(intern))) {
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
} else {
/* This can happen by avoiding constructors in specially-crafted code. */
zend_throw_error(NULL, "GlobIterator is not initialized");
}
}
/* }}} */
#endif /* HAVE_GLOB */
Expand Down
14 changes: 14 additions & 0 deletions ext/spl/tests/GlobIterator_constructor_count.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GlobIterator without constructor breaks count()
--FILE--
<?php
$rc = new ReflectionClass(GlobIterator::class);
$in = $rc->newInstanceWithoutConstructor();
try {
count($in);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
GlobIterator is not initialized
Loading