Search code examples
cluamingw32

Lua 5.2 compiled with MINGW32 crashes my host program when a Lua error occurs


Compiling Lua 5.2 using MINGW32 creates a library (lua52.dll) that may crash the host application when a Lua error occurs.

I detected this bug by making a Lua syntax error in a test script. If the script had no errors at all, the host program ran it successfully. However, when loading an invalid script (e.g. with a missing THEN in an IF block) the host program crashed.

Here is a fragment of my code:

//Load the script    
int status = luaL_loadfile(L, "foo.lua");
if (status == 0) {
    //Run the script
    ....
} else {
    warn("LUA script error: %d. %s", status, lua_tostring(L, -1));       
    lua_pop(L, 1);
}    

If the script has no syntax errors, statusis 0 and the script is successfully executed. Otherwise, luaL_loadfile does not return and the program crashes.


Solution

  • The problem is in the CFLAGS specified in the Lua's Makefile. Originally, src/Makefile contains the following line:

    CFLAGS= -O2 -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
    

    The problem is fixed by changing it to:

    CFLAGS= -O2 -fno-omit-frame-pointer -Wall -DLUA_COMPAT_ALL $(SYSCFLAGS) $(MYCFLAGS)
    

    EXPLANATION

    Lua's error handling is based on the use of functions setjmp and longjmp. The -O2 optimization flag implicitly makes the compiler to omit the frame pointer. To avoid this, we pass -fno-omit-frame-pointer to override this setting.

    Hope this helps.