lua-users home
lua-l archive

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


On 03/12/2017 12:16 PM, Rain Gloom wrote:
> The biggest problem that simple _ENV sandboxing can't avoid is
> infinitely long running code, one would need either the debug library to
> block a script after N instructions or use their own parser.

It is possible to have infinitely running code without recursions.
It's because code can create new functions and call them.

Here is an example:

local create_func =
  function(body)
    return function() return body() end
  end

local a = 0
local inc_counter
inc_counter =
  function()
    a = a + 1
    print(a, inc_counter)
    inc_counter = create_func(inc_counter)
    return inc_counter()
  end

local g = create_func(inc_counter)
g()

You can see that each time new function is executed. And there is
no stack overflow due "tail recursion optimization". So this code
theoretically will run forever.

-- Martin