moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

LuaCall / While loop where control variable is never changed.

Open blakepell opened this issue 4 years ago • 1 comments

I haven't had a problem with this but the last while check looks buggy. The while loop at the end has a check where the control variable is never changed. Presumably, if it gets there and copied < nresults then it's going to create an endless loop on the L.Push(DynValue.Nil); line.

If it's trying to fill the additional spots with L.Push(DynValue.Nil) does copied need to be incremented in that loop?

protected static void LuaCall(LuaState L, lua_Integer nargs, lua_Integer nresults = LUA_MULTRET)
{
    DynValue[] args = L.GetTopArray(nargs);

    L.Discard(nargs);

    DynValue func = L.Pop();

    DynValue ret = L.ExecutionContext.Call(func, args);

    if (nresults != 0)
    {
        if (nresults == -1)
        {
            nresults = (ret.Type == DataType.Tuple) ? ret.Tuple.Length : 1;
        }

        DynValue[] vals = (ret.Type == DataType.Tuple) ? ret.Tuple : new DynValue[1] { ret };

        int copied = 0;

        for (int i = 0; i < vals.Length && copied < nresults; i++, copied++)
        {
            L.Push(vals[i]);
        }

        while (copied < nresults)
        {
            L.Push(DynValue.Nil);
        }
    }
}

Should the last while loop be:

while (copied < nresults)
{
    L.Push(DynValue.Nil);
    copied++;
}

blakepell avatar Mar 05 '21 20:03 blakepell

Fix in #326

Joy-less avatar Aug 15 '23 12:08 Joy-less