Skip to content

Commit 2960301

Browse files
committed
Catch potential exceptions during to string conversion
As of PHP 7.4.0, exceptions are allowed to be thrown from inside `__toString()` methods; we have to cater to that, and catch these exceptions early. Closes GH-6042
1 parent 247105a commit 2960301

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

ext/com_dotnet/com_com.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,37 @@ PHP_FUNCTION(com_create_instance)
8585

8686
if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
8787
"Server", sizeof("Server")-1))) {
88-
convert_to_string_ex(tmp);
88+
if (!try_convert_to_string(tmp)) {
89+
return;
90+
}
8991
server_name = Z_STRVAL_P(tmp);
9092
server_name_len = Z_STRLEN_P(tmp);
9193
ctx = CLSCTX_REMOTE_SERVER;
9294
}
9395

9496
if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
9597
"Username", sizeof("Username")-1))) {
96-
convert_to_string_ex(tmp);
98+
if (!try_convert_to_string(tmp)) {
99+
return;
100+
}
97101
user_name = Z_STRVAL_P(tmp);
98102
user_name_len = Z_STRLEN_P(tmp);
99103
}
100104

101105
if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
102106
"Password", sizeof("Password")-1))) {
103-
convert_to_string_ex(tmp);
107+
if (!try_convert_to_string(tmp)) {
108+
return;
109+
}
104110
password = Z_STRVAL_P(tmp);
105111
password_len = Z_STRLEN_P(tmp);
106112
}
107113

108114
if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(server_params),
109115
"Domain", sizeof("Domain")-1))) {
110-
convert_to_string_ex(tmp);
116+
if (!try_convert_to_string(tmp)) {
117+
return;
118+
}
111119
domain_name = Z_STRVAL_P(tmp);
112120
domain_name_len = Z_STRLEN_P(tmp);
113121
}
@@ -720,7 +728,9 @@ PHP_FUNCTION(com_event_sink)
720728
if ((tmp = zend_hash_index_find(Z_ARRVAL_P(sink), 1)) != NULL && Z_TYPE_P(tmp) == IS_STRING)
721729
dispname = Z_STRVAL_P(tmp);
722730
} else if (sink != NULL) {
723-
convert_to_string(sink);
731+
if (!try_convert_to_string(sink)) {
732+
return;
733+
}
724734
dispname = Z_STRVAL_P(sink);
725735
}
726736

ext/com_dotnet/com_handlers.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ static zval *com_property_read(zval *object, zval *member, int type, void **cahc
3838
obj = CDNO_FETCH(object);
3939

4040
if (V_VT(&obj->v) == VT_DISPATCH) {
41-
VariantInit(&v);
41+
if (!try_convert_to_string(member)) {
42+
return rv;
43+
}
4244

43-
convert_to_string_ex(member);
45+
VariantInit(&v);
4446

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

6870
if (V_VT(&obj->v) == VT_DISPATCH) {
71+
if (!try_convert_to_string(member)) {
72+
return value;
73+
}
74+
6975
VariantInit(&v);
7076

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

207212
if (V_VT(&obj->v) == VT_DISPATCH) {
208-
convert_to_string_ex(member);
213+
if (!try_convert_to_string(member)) {
214+
return 0;
215+
}
209216
if (SUCCEEDED(php_com_get_id_of_name(obj, Z_STRVAL_P(member), Z_STRLEN_P(member), &dispid))) {
210217
/* TODO: distinguish between property and method! */
211218
return 1;

ext/com_dotnet/com_saproxy.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ static zval *saproxy_read_dimension(zval *object, zval *offset, int type, zval *
108108
}
109109
ZVAL_COPY_VALUE(&args[i-1], offset);
110110

111-
convert_to_string(&proxy->indices[0]);
111+
if (!try_convert_to_string(&proxy->indices[0])) {
112+
efree(args);
113+
return rv;
114+
}
112115
VariantInit(&v);
113116

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

226-
convert_to_string(&proxy->indices[0]);
229+
if (!try_convert_to_string(&proxy->indices[0])) {
230+
efree(args);
231+
return;
232+
}
227233
VariantInit(&v);
228234
if (SUCCESS == php_com_do_invoke(proxy->obj, Z_STRVAL(proxy->indices[0]),
229235
Z_STRLEN(proxy->indices[0]), DISPATCH_PROPERTYPUT, &v, proxy->dimensions + 1,

0 commit comments

Comments
 (0)