Page 1 of 1

Inproper use of lua_pcall.

Posted: Wed Feb 20, 2013 1:06 am
by Hypnotoad
In using a lua debugger I have picked up the following warning, which causes the debugger to stop:
Warning 1005: lua_call/lua_pcall called with too few arguments on the stack wrote: This warning indicates that the lua_call or lua_pcall Lua API function was called from C with fewer than the expected number of arguments on the Lua stack. These functions both expect there to be one plus the number of arguments the function accepts on the stack.
This originates from lua_baseentity.cpp: Line 3675 of getStatusEffect.

Further examination of the function shows that the followin segment of code is unnecesary:

Code: Select all

    if( lua_pcall(L,2,1,0) )
    {
        return 0;
    }
This is due to the fact that if PStatusEffect returns null, there is no further need to make the lua_pcall. Ideally the lua_pcall should be moved to the end of the else statement just above it.
Since lua_pushnil is called if PStatusEffect is null, the only other suitable place for this would be in the else statment.
As the else statement adds a second parameter, this will qualify the lua_pcall.

Re: Inproper use of lua_pcall.

Posted: Wed Feb 20, 2013 2:26 am
by atom0s
Not checking for an error on the lua_pcall can lead to an error string being left on the stack btw. If an error happens the stack should be checked for an error and if its found it should be popped from the stack.

Just a heads up to DSP team as well, if the status is nil and the pushnil happens it shouldn't be calling pcall then.

Fixed function:

Code: Select all

inline int32 CLuaBaseEntity::getStatusEffect(lua_State *L)
{
    DSP_DEBUG_BREAK_IF(m_PBaseEntity == NULL);
    DSP_DEBUG_BREAK_IF(m_PBaseEntity->objtype == TYPE_NPC);

    DSP_DEBUG_BREAK_IF(lua_isnil(L,1) || !lua_isnumber(L,1));

    uint8 n = lua_gettop(L);

    CStatusEffect* PStatusEffect = ((CBattleEntity*)m_PBaseEntity)->StatusEffectContainer->GetStatusEffect(
        (EFFECT)lua_tointeger(L,1),
        (n >= 2) ? (uint16)lua_tointeger(L,2) : 0);

    if (PStatusEffect == NULL)
    {
        lua_pushnil(L);
    }
    else
    {
        lua_pop(L,1);
        lua_pushstring(L, CLuaStatusEffect::className);
        lua_gettable(L,LUA_GLOBALSINDEX);
        lua_pushstring(L,"new");
        lua_gettable(L,-2);
        lua_insert(L,-2);
        lua_pushlightuserdata(L,(void*)PStatusEffect);

        if( lua_pcall(L,2,1,0) )
        {
            return 0;
        }
    }

    return 1;
}

Re: Inproper use of lua_pcall.

Posted: Wed Feb 20, 2013 7:17 pm
by whasf
Thank you, committed r2695