Skip to content

Commit 4b2dc58

Browse files
authored
Fix format specifiers and arguments in com_dotnet (phpGH-15398)
This is mostly about minor glitches (signedness or length confusion), but also fixes two occasions where `zend_string`s still have been regarded as `char *`. We also add a regression test case for failing property name lookup, since that is the most relevant issue we're fixing here.
1 parent 04320d2 commit 4b2dc58

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

ext/com_dotnet/com_com.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ HRESULT php_com_invoke_helper(php_com_dotnet_object *obj, DISPID id_member,
392392

393393
default:
394394
desc = php_win32_error_to_msg(hr);
395-
spprintf(&msg, 0, "Error [0x%08x] %s", hr, desc);
395+
spprintf(&msg, 0, "Error [0x%08lx] %s", hr, desc);
396396
php_win32_error_msg_free(desc);
397397
break;
398398
}
@@ -639,7 +639,7 @@ zend_result php_com_do_invoke(php_com_dotnet_object *obj, zend_string *name,
639639

640640
if (FAILED(hr)) {
641641
char *winerr = php_win32_error_to_msg(hr);
642-
spprintf(&msg, 0, "Unable to lookup `%s': %s", name, winerr);
642+
spprintf(&msg, 0, "Unable to lookup `%s': %s", ZSTR_VAL(name), winerr);
643643
php_win32_error_msg_free(winerr);
644644
php_com_throw_exception(hr, msg);
645645
efree(msg);

ext/com_dotnet/com_dotnet.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ PHP_METHOD(dotnet, __construct)
245245
if (FAILED(hr)) {
246246
char buf[1024];
247247
char *err = php_win32_error_to_msg(hr);
248-
snprintf(buf, sizeof(buf), "Failed to init .Net runtime [%s] [0x%08x] %s", where, hr, err);
248+
snprintf(buf, sizeof(buf), "Failed to init .Net runtime [%s] [0x%08lx] %s", where, hr, err);
249249
php_win32_error_msg_free(err);
250250
php_com_throw_exception(hr, buf);
251251
RETURN_THROWS();
@@ -258,7 +258,7 @@ PHP_METHOD(dotnet, __construct)
258258
if (FAILED(hr)) {
259259
char buf[1024];
260260
char *err = php_win32_error_to_msg(hr);
261-
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] [0x%08x] %s", where, hr, err);
261+
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] [0x%08lx] %s", where, hr, err);
262262
php_win32_error_msg_free(err);
263263
php_com_throw_exception(hr, buf);
264264
ZVAL_NULL(object);
@@ -270,7 +270,7 @@ PHP_METHOD(dotnet, __construct)
270270
if (FAILED(hr)) {
271271
char buf[1024];
272272
char *err = php_win32_error_to_msg(hr);
273-
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] [0x%08x] %s", where, hr, err);
273+
snprintf(buf, sizeof(buf), "Failed to re-init .Net domain [%s] [0x%08lx] %s", where, hr, err);
274274
php_win32_error_msg_free(err);
275275
php_com_throw_exception(hr, buf);
276276
ZVAL_NULL(object);
@@ -344,7 +344,7 @@ PHP_METHOD(dotnet, __construct)
344344
if (ret == FAILURE) {
345345
char buf[1024];
346346
char *err = php_win32_error_to_msg(hr);
347-
snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08x] %s", where, hr, err);
347+
snprintf(buf, sizeof(buf), "Failed to instantiate .Net object [%s] [0x%08lx] %s", where, hr, err);
348348
php_win32_error_msg_free(err);
349349
php_com_throw_exception(hr, buf);
350350
RETURN_THROWS();

ext/com_dotnet/com_typeinfo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ bool php_com_process_typeinfo(ITypeInfo *typeinfo, HashTable *id_to_name, bool p
543543
/* first element is the function name */
544544
SysFreeString(names[0]);
545545

546-
php_printf("\t/* DISPID=%d */\n", func->memid);
546+
php_printf("\t/* DISPID=%ld */\n", func->memid);
547547

548548
if (func->elemdescFunc.tdesc.vt != VT_VOID) {
549549
php_printf("\t/* %s [%d] */\n",

ext/com_dotnet/com_wrapper.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static inline void trace(char *fmt, ...)
5353
va_list ap;
5454
char buf[4096];
5555

56-
snprintf(buf, sizeof(buf), "T=%08x ", GetCurrentThreadId());
56+
snprintf(buf, sizeof(buf), "T=%08lx ", GetCurrentThreadId());
5757
OutputDebugString(buf);
5858

5959
va_start(ap, fmt);
@@ -70,7 +70,7 @@ static inline void trace(char *fmt, ...)
7070
if (COMG(rshutdown_started)) { \
7171
trace(" PHP Object:%p (name:unknown) %s\n", Z_OBJ(disp->object), methname); \
7272
} else { \
73-
trace(" PHP Object:%p (name:%s) %s\n", Z_OBJ(disp->object), Z_OBJCE(disp->object)->name->val, methname); \
73+
trace(" PHP Object:%p (name:%s) %s\n", Z_OBJ(disp->object), ZSTR_VAL(Z_OBJCE(disp->object)->name), methname); \
7474
} \
7575
if (GetCurrentThreadId() != disp->engine_thread) { \
7676
return RPC_E_WRONG_THREAD; \
@@ -109,7 +109,7 @@ static ULONG STDMETHODCALLTYPE disp_release(IDispatchEx *This)
109109
FETCH_DISP("Release");
110110

111111
ret = InterlockedDecrement(&disp->refcount);
112-
trace("-- refcount now %d\n", ret);
112+
trace("-- refcount now %lu\n", ret);
113113
if (ret == 0) {
114114
/* destroy it */
115115
disp_destructor(disp);
@@ -201,7 +201,7 @@ static HRESULT STDMETHODCALLTYPE disp_getdispid(
201201

202202
name = php_com_olestring_to_string(bstrName, COMG(code_page));
203203

204-
trace("Looking for %s, namelen=%d in %p\n", ZSTR_VAL(name), ZSTR_LEN(name), disp->name_to_dispid);
204+
trace("Looking for %s, namelen=%lu in %p\n", ZSTR_VAL(name), ZSTR_LEN(name), disp->name_to_dispid);
205205

206206
/* Lookup the name in the hash */
207207
if ((tmp = zend_hash_find(disp->name_to_dispid, name)) != NULL) {
@@ -235,7 +235,7 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
235235
if (NULL != (name = zend_hash_index_find(disp->dispid_to_name, id))) {
236236
/* TODO: add support for overloaded objects */
237237

238-
trace("-- Invoke: %d %20s [%d] flags=%08x args=%d\n", id, Z_STRVAL_P(name), Z_STRLEN_P(name), wFlags, pdp->cArgs);
238+
trace("-- Invoke: %ld %20s [%lu] flags=%08x args=%u\n", id, Z_STRVAL_P(name), Z_STRLEN_P(name), wFlags, pdp->cArgs);
239239

240240
/* convert args into zvals.
241241
* Args are in reverse order */
@@ -246,7 +246,7 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
246246

247247
arg = &pdp->rgvarg[ pdp->cArgs - 1 - i];
248248

249-
trace("alloc zval for arg %d VT=%08x\n", i, V_VT(arg));
249+
trace("alloc zval for arg %u VT=%08x\n", i, V_VT(arg));
250250

251251
php_com_wrap_variant(&params[i], arg, COMG(code_page));
252252
}
@@ -275,7 +275,7 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
275275
VARIANT *srcvar = &obj->v;
276276
VARIANT *dstvar = &pdp->rgvarg[ pdp->cArgs - 1 - i];
277277
if ((V_VT(dstvar) & VT_BYREF) && obj->modified ) {
278-
trace("percolate modified value for arg %d VT=%08x\n", i, V_VT(dstvar));
278+
trace("percolate modified value for arg %u VT=%08x\n", i, V_VT(dstvar));
279279
php_com_copy_variant(dstvar, srcvar);
280280
}
281281
}
@@ -311,7 +311,7 @@ static HRESULT STDMETHODCALLTYPE disp_invokeex(
311311
}
312312

313313
} else {
314-
trace("InvokeEx: I don't support DISPID=%d\n", id);
314+
trace("InvokeEx: I don't support DISPID=%ld\n", id);
315315
}
316316

317317
return ret;
@@ -508,7 +508,7 @@ static php_dispatchex *disp_constructor(zval *object)
508508
{
509509
php_dispatchex *disp = (php_dispatchex*)CoTaskMemAlloc(sizeof(php_dispatchex));
510510

511-
trace("constructing a COM wrapper for PHP object %p (%s)\n", object, Z_OBJCE_P(object)->name);
511+
trace("constructing a COM wrapper for PHP object %p (%s)\n", object, ZSTR_VAL(Z_OBJCE_P(object)->name));
512512

513513
if (disp == NULL)
514514
return NULL;
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Property name lookup error
3+
--EXTENSIONS--
4+
com_dotnet
5+
--FILE--
6+
<?php
7+
$dict = new COM("Scripting.Dictionary");
8+
try {
9+
$dict->unknownProperty;
10+
} catch (com_exception $ex) {
11+
echo $ex->getMessage(), "\n";
12+
}
13+
?>
14+
--EXPECTF--
15+
Unable to lookup `unknownProperty': %s

0 commit comments

Comments
 (0)