Skip to content

Commit

Permalink
Fixed : Calling COM object methods with nil argument crash the pr…
Browse files Browse the repository at this point in the history
…ogram (should indicate an empty parameter) #177

Fixed: `COM` object makes the program crash when a method or property returns a `Nothing` value #173
  • Loading branch information
samyeyo committed Jan 21, 2024
1 parent 8bb9425 commit 375a0ac
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/core/sys/Com.c
Expand Up @@ -105,12 +105,15 @@ static int COM_method_call(lua_State *L) {
}
params.cArgs = n;
args = calloc(n+1, sizeof(VARIANT));
for (i = (n - 1); i >= 0; i--) {

for (i = (n-1); i >= 0; i--) {
VARIANT *var;
var = &args[i];
VariantInit(var);
switch(lua_type(L, idx)) {
case LUA_TNIL: var->vt = VT_NULL; break;
case LUA_TNIL: V_VT(var) = VT_EMPTY;
V_I1(var) = 0;
break;
case LUA_TBOOLEAN: var->vt = VT_BOOL; var->boolVal = lua_toboolean(L, idx); break;
number: case LUA_TNUMBER: if (lua_isinteger(L, idx)) {
var->vt = VT_I4;
Expand All @@ -127,7 +130,7 @@ number: case LUA_TNUMBER: if (lua_isinteger(L, idx)) {
wchar_t *name;
BOOL done = FALSE;
VARDESC *vardesc;
for (UINT i = 0; !done && (i < attr-> cVars); i++) {
for (UINT i = 0; !done && (i < attr->cVars); i++) {
ITypeInfo_GetVarDesc(t, i, &vardesc);
ITypeInfo_GetDocumentation(t, vardesc->memid, &name, NULL, NULL, NULL);
if (wcscmp(str, name) == 0) {
Expand All @@ -147,7 +150,7 @@ number: case LUA_TNUMBER: if (lua_isinteger(L, idx)) {
case LUA_TTABLE: {
COM *o = lua_self(L, idx, COM);
V_DISPATCH(var) = o->this;
IDispatch_AddRef(o->this);
IDispatch_AddRef(o->this);
if (method & DISPATCH_PROPERTYPUT)
method = DISPATCH_PROPERTYPUTREF;
V_VT(var) = VT_DISPATCH;
Expand All @@ -173,16 +176,19 @@ number: case LUA_TNUMBER: if (lua_isinteger(L, idx)) {
hr = S_OK;
goto done;
}
lua_pushwstring(L, (LPCWSTR)execpInfo.bstrDescription);
SysFreeString(execpInfo.bstrDescription);
if (execpInfo.bstrHelpFile)
SysFreeString(execpInfo.bstrHelpFile);
if (execpInfo.bstrSource)
SysFreeString(execpInfo.bstrSource);
if (execpInfo.bstrDescription) {
lua_pushwstring(L, (LPCWSTR)execpInfo.bstrDescription);
SysFreeString(execpInfo.bstrDescription);
break;
}
default: lua_pushstring(L, "unknown error");
break;
case DISP_E_BADVARTYPE: lua_pushstring(L, "Bad argument type");
case DISP_E_BADVARTYPE: lua_pushstring(L, "Bad argument type"); break;
case DISP_E_TYPEMISMATCH: lua_pushfstring(L, "type mismatch for parameter %d", puArgErr); break;
default: lua_pushstring(L, "unknown error");
}
} else switch(result.vt) {
case VT_EMPTY:
Expand Down Expand Up @@ -218,9 +224,13 @@ done: case VT_NULL: lua_pushnil(L); break;
case VT_DECIMAL: VariantChangeType(&result, &result, 0, VT_R8);
lua_pushnumber(L, V_R8(&result)); break;
case VT_DISPATCH: {
lua_pushlightuserdata(L, result.pdispVal);
lua_pushinstance(L, COM, 1);
goto cleanup;
if (result.pdispVal) {
lua_pushlightuserdata(L, result.pdispVal);
lua_pushinstance(L, COM, 1);
goto cleanup;
}
lua_pushnil(L);
break;
}
case VT_DATE: {
SYSTEMTIME st;
Expand Down

0 comments on commit 375a0ac

Please sign in to comment.