views:

82

answers:

2

I have embedded Lua in an Objective-C application using LuaObjCBridge. I need to know how to stop the Lua process if it taking too much time (infinite loop?).

Would running it in a separate thread help?

+4  A: 

The usual way to do this is to use lua_sethook to schedule a callback every count VM instructions; when the callback lua_Hook function occurs after a excessive time your hook function can raise an error forcing control to your protected call.

Doug Currie
But how much will this kill speed?
John Smith
+3  A: 

Doug's answer already provides the default necessary to restrict normal lua code execution. If you need to limit this for security reasons, you should know that there are known ways to use lua library calls, such as string pattern matching functions, to create practical infinite loops. The instruction count hook won't catch these for you since the lua instruction count is not incrementing while the c function call is executing. For a solution of this calibre, you need OS-level restrictions (separate process, interrupt from SIGALRM?)

kaizer.se
Thanks, kaizer.se, +1.
Doug Currie