Skip to content

ext/spl: Reject abstract classes when instantiating a stored class - #25600

Draft
iliaal wants to merge 2 commits into
php:masterfrom
iliaal:fix/gh-25585-spl-uninstantiable
Draft

iliaal wants to merge 2 commits into
php:masterfrom
iliaal:fix/gh-25585-spl-uninstantiable

Conversation

@iliaal

@iliaal iliaal commented Nov 19, 2026

Copy link
Copy Markdown
Member

Depends on #25592 (must merge up first). ArrayObject::getIterator(), SplFileInfo::openFile(), getFileInfo(), getPathInfo() and FilesystemIterator with CURRENT_AS_FILEINFO bypass object_init_ex(), so they instantiate an abstract class given as the iterator, file or info class. Create the SplFileInfo objects through object_init_ex() and check ZEND_ACC_UNINSTANTIABLE in getIterator(), which has to pass the source object to spl_array_object_new_ex(). Code that relied on this gets Error: Cannot instantiate abstract class X, hence master and an UPGRADING entry.

Follow-up to GH-25585.

ArrayObject::getIterator(), SplFileInfo::openFile(), getFileInfo(),
getPathInfo() and FilesystemIterator in CURRENT_AS_FILEINFO mode create
objects of a class given to the ArrayObject constructor, setIteratorClass(),
setFileClass(), setInfoClass() or the method itself, bypassing
object_init_ex(). Constants in default property values were never resolved,
so the object received the unevaluated constant expression, and with
opcache, where the class is immutable and its mutable data is created by
that resolution, object_properties_init() dereferenced NULL. Resolve the
class constants before creating the object, as new does.

Fixes phpGH-25585
ArrayObject::getIterator(), SplFileInfo::openFile(), getFileInfo(),
getPathInfo() and FilesystemIterator in CURRENT_AS_FILEINFO mode bypassed
object_init_ex(), so an abstract class given to the ArrayObject constructor,
unserialize(), setIteratorClass(), setFileClass(), setInfoClass() or the
method itself was instantiated. Create the SplFileInfo objects through
object_init_ex() and check the class flags in getIterator(), which has to
pass the source object to spl_array_object_new_ex(). A failed instantiation
now leaves NULL in the filesystem iterator's current value, so reset it to
make the next access retry and throw again.

$info = new SplFileInfo(__FILE__);
test("openFile()", function () use ($info) {
$info->setFileClass("AbstractFileObject");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It should be rejected here, not when trying to instantiate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants