Skip to content

zend_compile_implements() assumes that the class entry has no interfaces already #22354

@DanielEScherzer

Description

@DanielEScherzer

Description

See

php-src/Zend/zend_compile.c

Lines 8959 to 8977 in b0c7865

static void zend_compile_implements(zend_ast *ast) /* {{{ */
{
zend_ast_list *list = zend_ast_get_list(ast);
zend_class_entry *ce = CG(active_class_entry);
zend_class_name *interface_names;
uint32_t i;
interface_names = emalloc(sizeof(zend_class_name) * list->children);
for (i = 0; i < list->children; ++i) {
zend_ast *class_ast = list->child[i];
interface_names[i].name =
zend_resolve_const_class_name_reference(class_ast, "interface name");
interface_names[i].lc_name = zend_string_tolower(interface_names[i].name);
}
ce->num_interfaces = list->children;
ce->interface_names = interface_names;
}

If an attribute validator added an interface to the class, it gets silently dropped and triggers leaks, because class attributes are compiled before implements, see

php-src/Zend/zend_compile.c

Lines 9110 to 9116 in b0c7865

if (decl->child[3]) {
zend_compile_attributes(&ce->attributes, decl->child[3], 0, ZEND_ATTRIBUTE_TARGET_CLASS, 0);
}
if (implements_ast) {
zend_compile_implements(implements_ast);
}

Discovered while working on https://github.com/DanielEScherzer/CustomCast, not sure if I noticed this earlier and just didn't report it but it has been there for a while

PHP Version

8.4+

Operating System

No response

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions