lua-users home
lua-l archive

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

On Jan 4, 2008 12:26 AM, Kurt Nordstrom <> wrote:
> However, I am looking for a way to put limits on the code that users can
> run.  Namely, I don't want to see somebody kick something off with an
> infinite loop and lock up the entire game.

I had a similiar situation, and took to a solution that looks like this:

void inmlua_hookhandler(lua_State *L, lua_Debug *ar)
  char output[256];
  if (ar->event == LUA_HOOKLINE)
    g_inmgame->luacontrol.line_count += 1;

    if ( g_inmgame->luacontrol.line_count >= INM_LUA_MAX_COMMANDS )
      snprintf( output, 255,
                "inmlua_hookhandler: Runaway script detected. [Line:%i]\n",
                ar->currentline );
      output[255] = 0;
      lua_pushstring(L, output );

Where I load the script, I perform this initialization:

  /* Prepare New Hook. */
  lua_sethook( L , inmlua_hookhandler, LUA_MASKLINE, 0 );
  g_inmgame->luacontrol.line_count = 0;

The magic number I use is 1000000 lines, which turns out to be about a
second or two from My Machine (tm), which during debugging is about
when I've noticed that there was a problem. This serves as an
all-else-fails way out of something that would likely require killing
by the OS.