Skip to content

Catch potential exceptions during to string conversion #6042

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
20 changes: 15 additions & 5 deletions ext/com_dotnet/com_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,37 @@ PHP_FUNCTION(com_create_instance)

if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
"Server", sizeof("Server")-1))) {
convert_to_string_ex(tmp);
if (!try_convert_to_string(tmp)) {
return;
}
server_name = Z_STRVAL_P(tmp);
server_name_len = Z_STRLEN_P(tmp);
ctx = CLSCTX_REMOTE_SERVER;
}

if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
"Username", sizeof("Username")-1))) {
convert_to_string_ex(tmp);
if (!try_convert_to_string(tmp)) {
return;
}
user_name = Z_STRVAL_P(tmp);
user_name_len = Z_STRLEN_P(tmp);
}

if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
"Password", sizeof("Password")-1))) {
convert_to_string_ex(tmp);
if (!try_convert_to_string(tmp)) {
return;
}
password = Z_STRVAL_P(tmp);
password_len = Z_STRLEN_P(tmp);
}

if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
"Domain", sizeof("Domain")-1))) {
convert_to_string_ex(tmp);
if (!try_convert_to_string(tmp)) {
return;
}
domain_name = Z_STRVAL_P(tmp);
domain_name_len = Z_STRLEN_P(tmp);
}
Expand Down Expand Up @@ -720,7 +728,9 @@ PHP_FUNCTION(com_event_sink)
if ((tmp = zend_hash_index_find(Z_ARRVAL_P(sink), 1)) != NULL && Z_TYPE_P(tmp) == IS_STRING)
dispname = Z_STRVAL_P(tmp);
} else if (sink != NULL) {
convert_to_string(sink);
if (!try_convert_to_string(sink)) {
return;
Copy link
Member

Choose a reason for hiding this comment

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

As these are in a PHP_FUNCTION you could use RETURN_THROWS()

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yes, thanks! I'll do so when merging into master (PR targets PHP 7.4).

Copy link
Member

Choose a reason for hiding this comment

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

Right I didn't see the target :)

}
dispname = Z_STRVAL_P(sink);
}

Expand Down
15 changes: 11 additions & 4 deletions ext/com_dotnet/com_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ static zval *com_property_read(zval *object, zval *member, int type, void **cahc
obj = CDNO_FETCH(object);

if (V_VT(&obj->v) == VT_DISPATCH) {
VariantInit(&v);
if (!try_convert_to_string(member)) {
return rv;
}

convert_to_string_ex(member);
VariantInit(&v);

res = php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member),
DISPATCH_METHOD|DISPATCH_PROPERTYGET, &v, 0, NULL, 1);
Expand All @@ -66,9 +68,12 @@ static zval *com_property_write(zval *object, zval *member, zval *value, void **
obj = CDNO_FETCH(object);

if (V_VT(&obj->v) == VT_DISPATCH) {
if (!try_convert_to_string(member)) {
return value;
}

VariantInit(&v);

convert_to_string_ex(member);
if (SUCCESS == php_com_do_invoke(obj, Z_STRVAL_P(member), Z_STRLEN_P(member),
DISPATCH_PROPERTYPUT|DISPATCH_PROPERTYPUTREF, &v, 1, value, 0)) {
VariantClear(&v);
Expand Down Expand Up @@ -205,7 +210,9 @@ static int com_property_exists(zval *object, zval *member, int check_empty, void
obj = CDNO_FETCH(object);

if (V_VT(&obj->v) == VT_DISPATCH) {
convert_to_string_ex(member);
if (!try_convert_to_string(member)) {
return 0;
}
if (SUCCEEDED(php_com_get_id_of_name(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), &dispid))) {
/* TODO: distinguish between property and method! */
return 1;
Expand Down
10 changes: 8 additions & 2 deletions ext/com_dotnet/com_saproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ static zval *saproxy_read_dimension(zval *object, zval *offset, int type, zval *
}
ZVAL_COPY_VALUE(&args[i-1], offset);

convert_to_string(&proxy->indices[0]);
if (!try_convert_to_string(&proxy->indices[0])) {
efree(args);
return rv;
}
VariantInit(&v);

res = php_com_do_invoke(proxy->obj, Z_STRVAL(proxy->indices[0]),
Expand Down Expand Up @@ -223,7 +226,10 @@ static void saproxy_write_dimension(zval *object, zval *offset, zval *value)
ZVAL_COPY_VALUE(&args[i-1], offset);
ZVAL_COPY_VALUE(&args[i], value);

convert_to_string(&proxy->indices[0]);
if (!try_convert_to_string(&proxy->indices[0])) {
efree(args);
return;
}
VariantInit(&v);
if (SUCCESS == php_com_do_invoke(proxy->obj, Z_STRVAL(proxy->indices[0]),
Z_STRLEN(proxy->indices[0]), DISPATCH_PROPERTYPUT, &v, proxy->dimensions + 1,
Expand Down