lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 15.10.2016 um 01:45 schröbte ThePhD:
I have a strange bug I came across while trying to help someone with an
issue with Sol. Lua says it can compile as C++. So, I tried to compile the
following with C++ and I get a strange error after throwing an error that
gets caught by the LUA_TRY macro and then returned:

// Compile Lua 5.3.3 as C++
// no extern "C" { ... }
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

int thrower(lua_State* L) {
    throw 0;
}

int main() {

    lua_State* L = luaL_newstate();

    lua_pushcclosure(L, thrower, 0);
    int top_before = lua_gettop(L);
    int code = lua_pcallk(L, 0, 0, LUA_NOREF, 0, NULL);
    int top_after  = lua_gettop(L);

    return 0;
}

`code == -1`, which isn't a valid return for `lua_pcallk`, and `top_before
== 1 `, and `top_after == 1` as well (the function is not cleaned off the
stack).

It seems like a bug in the Lua implementation.

It looks that way. `lua_resume` specifically handles a -1 status and converts it into `LUA_ERRRUN`, but this is missing for `lua_pcallk`. `LUA_ERRRUN` would probably be the correct return value in this situation, because there is no error message handler function at stack index -2 (which is the value of `LUA_NOREF`). You probably want to use a positive number (or `LUA_MULTRET` which is -1) there instead.

Philipp