3

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.

1 Answer 1

3

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.

2
  • 3
    This has been reported before: lua-users.org/lists/lua-l/2011-10/msg00549.html. Thanks anyway.
    – lhf
    Nov 11, 2013 at 12:07
  • Oh, glad I end up with the same solution! I've been unable to find this issue by googling so I decided to post this message in StackOverflow. Thank you anyway!
    – Claudi
    Nov 11, 2013 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.