Skip to content

Fix property access of PHP objects wrapped in variant #16331

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

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 9 additions & 7 deletions ext/com_dotnet/com_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
* and expose it as a COM exception */

if (wFlags & DISPATCH_PROPERTYGET) {
retval = zend_read_property(Z_OBJCE(disp->object), Z_OBJ(disp->object), Z_STRVAL_P(name), Z_STRLEN_P(name)+1, 1, &rv);
retval = zend_read_property(Z_OBJCE(disp->object), Z_OBJ(disp->object), Z_STRVAL_P(name), Z_STRLEN_P(name), 1, &rv);
ret = S_OK;
} else if (wFlags & DISPATCH_PROPERTYPUT) {
zend_update_property(Z_OBJCE(disp->object), Z_OBJ(disp->object), Z_STRVAL_P(name), Z_STRLEN_P(name), &params[0]);
ret = S_OK;
} else if (wFlags & DISPATCH_METHOD) {
zend_try {
retval = &rv;
Expand Down Expand Up @@ -305,7 +307,7 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
VariantInit(pvarRes);
php_com_variant_from_zval(pvarRes, retval, COMG(code_page));
}
zval_ptr_dtor(retval);
// zval_ptr_dtor(retval); // TODO needed for function calls?
Comment on lines -308 to +310
Copy link
Member

Choose a reason for hiding this comment

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

Yes I believe this is needed for function calls

Copy link
Member Author

Choose a reason for hiding this comment

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

See

Note that this doesn't fix calling methods, which is broken since 2005, because DISPATCH_METHOD|DISPATCH_PROPERTYGET is passed as flag, and the latter is always prioritized in disp_invokeex() (what is likely good for regular COM objects).

I'm planning to fix this in a follow-up PR. Thus I left the comment to not forget about destroying.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I overlooked this. This looks ok to me then

} else if (pvarRes) {
VariantInit(pvarRes);
}
Expand Down Expand Up @@ -425,7 +427,7 @@ static void generate_dispids(php_dispatchex *disp)
zend_string *name = NULL;
zval *tmp, tmp2;
int keytype;
zend_ulong pid;
zend_long pid;

if (disp->dispid_to_name == NULL) {
ALLOC_HASHTABLE(disp->dispid_to_name);
Expand Down Expand Up @@ -458,8 +460,8 @@ static void generate_dispids(php_dispatchex *disp)

/* add the mappings */
ZVAL_STR_COPY(&tmp2, name);
pid = zend_hash_next_free_element(disp->dispid_to_name);
zend_hash_index_update(disp->dispid_to_name, pid, &tmp2);
zend_hash_next_index_insert(disp->dispid_to_name, &tmp2);
pid = zend_hash_next_free_element(disp->dispid_to_name) - 1;

ZVAL_LONG(&tmp2, pid);
zend_hash_update(disp->name_to_dispid, name, &tmp2);
Expand Down Expand Up @@ -493,8 +495,8 @@ static void generate_dispids(php_dispatchex *disp)

/* add the mappings */
ZVAL_STR_COPY(&tmp2, name);
pid = zend_hash_next_free_element(disp->dispid_to_name);
zend_hash_index_update(disp->dispid_to_name, pid, &tmp2);
zend_hash_next_index_insert(disp->dispid_to_name, &tmp2);
pid = zend_hash_next_free_element(disp->dispid_to_name) - 1;

ZVAL_LONG(&tmp2, pid);
zend_hash_update(disp->name_to_dispid, name, &tmp2);
Expand Down
33 changes: 33 additions & 0 deletions ext/com_dotnet/tests/variant_variation2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Testing reading and writing of properties
--EXTENSIONS--
com_dotnet
--FILE--
<?php
class MyClass {
public $foo = "foo";
public string $bar = "bar";
}

$o = new MyClass();
$v = new variant($o);
var_dump($v->foo);
var_dump($v->bar);
$v->foo = "new foo";
var_dump($v->foo instanceof variant);
var_dump((string) $v->foo);
var_dump($o->foo instanceof variant);
var_dump((string) $o->foo);
$v->bar = "new bar";
var_dump($v->bar);
var_dump($o->bar);
?>
--EXPECT--
string(3) "foo"
string(3) "bar"
bool(true)
string(7) "new foo"
bool(true)
string(7) "new foo"
string(7) "new bar"
string(7) "new bar"