lua-users home
lua-l archive

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


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.

As a side note, C++ finally made warnings about `char*` -> `const char*` an error in recent versions of C++ and also still has implicit `void*` casts to other pointer type banned, so there are a few places in the code where compiling as a (recent) version of C++ will trigger an outright error, which are:

lobject.c, Line 255: char *pdot = strchr(s, '.');
Just add `const`: const char *pdot = strchr(s, '.');
Rest of the code seems to compile fine in C and C++ after this change

lstrlib.c, Line 936: char *ppoint = memchr(buff, point, nb);
Add a cast so that the line reads: char *ppoint = (char*)memchr(buff, point, nb);
Rest of the code seems to compile fine in both C and C++ mode after this change

Apologies for the long message, but I just wanted to explain everything that was happening.