Search code examples
lua

luaL_dostring() crashes when given script has syntax error


i try to integrate Lua in a embedded project using GCC on a Cortex-M4. i am able to load and run a Lua script, calling Lua functions from C, calling C functions from Lua. but the C program crashes (HardFault_Handler trap rises) when the given script passed as parameter in luaL_dostring() contains any Lua syntax errors.

here the relevant C code that crashes due to the syntax error in Lua:

      //create Lua VM...
      luaVm = lua_newstate(luaAlloc, NULL);

      //load libraries...
      luaopen_base(luaVm);
      luaopen_math(luaVm);
      luaopen_table(luaVm);
      luaopen_string(luaVm);

      //launch script...
      luaL_dostring(luaVm,  "function onTick()\n"
                            "  locaal x = 7\n"  //syntax error
                            "end\n"
                            "\n" );

when doing the same with correct Lua syntax, then it works:

  luaL_dostring(luaVm,  "function onTick()\n"
                        "  local x = 7\n"  
                        "end\n"
                        "\n" );

when debugging and stepping through luaL_dostring(), i can follow the Lua parsing line for line, and when reaching the line with the syntax error, then the C program crashes.

can anybody help? thanks.


have disabled setjmp/longjmp in Lua source code in the following way:

//#define LUAI_THROW(L,c)    longjmp((c)->b, 1) //TODO oli4 orig
//#define LUAI_TRY(L,c,a)    if (setjmp((c)->b) == 0) { a } //TODO oli4 orig

#define LUAI_THROW(L,c)    while(1) //TODO oli4 special
#define LUAI_TRY(L,c,a)    { a }    //TODO oli4 special

...so there is no setjmp/longjmp used anymore, but i still have the crash :-(

must have another cause???


Solution

  • found the problem: it is the sprintf function called on Lua syntax error. in fact, on my platform sprintf seems not support floating point presentation. so i changed luaconf.h the following way, limiting the presentation to integer format.

    //#define LUA_NUMBER_FMT     "%.14g"
    #define LUA_NUMBER_FMT     "%d"