Skip to content

Commit 1684c52

Browse files
authored
Fix GlobIterator without constructor breaks count() (php#18314)
As reported by OpenAI AARDVARK.
1 parent fd7ebd4 commit 1684c52

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

ext/spl/spl_directory.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -1599,12 +1599,12 @@ PHP_METHOD(GlobIterator, count)
15991599
RETURN_THROWS();
16001600
}
16011601

1602-
/* The spl_filesystem_object_get_method_check() function is called prior to calling this function.
1603-
* Therefore, the directory entry cannot be NULL. However, if it is not NULL, then it must be a glob iterator
1604-
* by construction. */
1605-
ZEND_ASSERT(spl_intern_is_glob(intern));
1606-
1607-
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
1602+
if (EXPECTED(spl_intern_is_glob(intern))) {
1603+
RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
1604+
} else {
1605+
/* This can happen by avoiding constructors in specially-crafted code. */
1606+
zend_throw_error(NULL, "GlobIterator is not initialized");
1607+
}
16081608
}
16091609
/* }}} */
16101610
#endif /* HAVE_GLOB */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GlobIterator without constructor breaks count()
3+
--FILE--
4+
<?php
5+
$rc = new ReflectionClass(GlobIterator::class);
6+
$in = $rc->newInstanceWithoutConstructor();
7+
try {
8+
count($in);
9+
} catch (Error $e) {
10+
echo $e->getMessage(), "\n";
11+
}
12+
?>
13+
--EXPECT--
14+
GlobIterator is not initialized

0 commit comments

Comments
 (0)