Inproper use of lua_pcall.

Post Reply
Hypnotoad
Posts: 15
Joined: Fri Dec 14, 2012 7:25 am

Inproper use of lua_pcall.

Post by Hypnotoad » Wed Feb 20, 2013 1:06 am

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.
Attachments
inproper_syntax.patch
(500 Bytes) Downloaded 171 times
Last edited by Hypnotoad on Wed Feb 20, 2013 2:57 am, edited 1 time in total.

User avatar
atom0s
Developer
Posts: 537
Joined: Thu Oct 25, 2012 9:52 am

Re: Inproper use of lua_pcall.

Post by atom0s » Wed Feb 20, 2013 2:26 am

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;
}

User avatar
whasf
Site Admin
Posts: 1312
Joined: Thu Jul 19, 2012 9:11 pm

Re: Inproper use of lua_pcall.

Post by whasf » Wed Feb 20, 2013 7:17 pm

Thank you, committed r2695
-- Whasf

Post Reply