Skip to content

Prevent direct instantiation of com_safearray_proxy #10278

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 3 commits into from
Oct 7, 2024
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
1 change: 1 addition & 0 deletions ext/com_dotnet/com_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ PHP_MINIT_FUNCTION(com_dotnet)

php_com_saproxy_class_entry = register_class_com_safearray_proxy();
/* php_com_saproxy_class_entry->constructor->common.fn_flags |= ZEND_ACC_PROTECTED; */
php_com_saproxy_class_entry->create_object = php_com_saproxy_create_object;
php_com_saproxy_class_entry->default_object_handlers = &php_com_saproxy_handlers;
php_com_saproxy_class_entry->get_iterator = php_com_saproxy_iter_get;

Expand Down
14 changes: 12 additions & 2 deletions ext/com_dotnet/com_saproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ typedef struct {

#define SA_FETCH(zv) (php_com_saproxy*)Z_OBJ_P(zv)

zend_object *php_com_saproxy_create_object(zend_class_entry *class_type)
{
php_com_saproxy *intern = emalloc(sizeof(*intern));
Copy link
Member

Choose a reason for hiding this comment

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

You can use zend_object_alloc which will do the allocation and memset

Copy link
Member Author

Choose a reason for hiding this comment

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

That would't zero the last sizeof(zend_object) bytes of the structure, due to com_dotnet objects having the zend_object as first member, not as last, as usual. I'm planning to change this soonish.

Copy link
Member

Choose a reason for hiding this comment

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

Right, that's weird...

Copy link
Member Author

Choose a reason for hiding this comment

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

That was the standard struct layout in PHP 5; all bundled extension had been changed for PHP 7, but com_dotnet had been overlooked.

memset(intern, 0, sizeof(*intern));
zend_object_std_init(&intern->std, class_type);
return &intern->std;
}

static inline void clone_indices(php_com_saproxy *dest, php_com_saproxy *src, int ndims)
{
int i;
Expand Down Expand Up @@ -317,7 +325,7 @@ static zend_function *saproxy_method_get(zend_object **object, zend_string *name

static zend_function *saproxy_constructor_get(zend_object *object)
{
/* user cannot instantiate */
zend_throw_error(NULL, "Cannot directly construct com_safeproxy_array; it is for internal usage only");
return NULL;
}

Expand Down Expand Up @@ -365,7 +373,9 @@ static void saproxy_free_storage(zend_object *object)
//??? }
//??? }

OBJ_RELEASE(&proxy->obj->zo);
if (proxy->obj != NULL) {
OBJ_RELEASE(&proxy->obj->zo);
}

zend_object_std_dtor(object);

Expand Down
1 change: 1 addition & 0 deletions ext/com_dotnet/php_com_dotnet_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extern zend_object_handlers php_com_object_handlers;
void php_com_object_enable_event_sink(php_com_dotnet_object *obj, bool enable);

/* com_saproxy.c */
zend_object *php_com_saproxy_create_object(zend_class_entry *class_type);
zend_object_iterator *php_com_saproxy_iter_get(zend_class_entry *ce, zval *object, int by_ref);
void php_com_saproxy_create(zend_object *com_object, zval *proxy_out, zval *index);
extern zend_object_handlers php_com_saproxy_handlers;
Expand Down
14 changes: 14 additions & 0 deletions ext/com_dotnet/tests/saproxy_instantiate.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Manual instantiation of com_safearray_proxy is not allowed
--EXTENSIONS--
com_dotnet
--FILE--
<?php
try {
new com_safearray_proxy();
} catch (Error $e) {
echo $e->getMessage(), PHP_EOL;
}
?>
--EXPECT--
Cannot directly construct com_safeproxy_array; it is for internal usage only