passing false as a quota deactivates the hooks

This commit is contained in:
kikito 2013-09-14 12:54:49 +02:00
parent 48ae2844e9
commit bf995029ba
2 changed files with 16 additions and 5 deletions

View File

@ -120,19 +120,26 @@ function sandbox.protect(f, options)
options = options or {} options = options or {}
local quota = options.quota or 500000 local quota = false
if options.quota ~= false then
quota = options.quota or 500000
end
local env = merge(options.env or {}, BASE_ENV) local env = merge(options.env or {}, BASE_ENV)
env._G = env._G or env env._G = env._G or env
setfenv(f, env) setfenv(f, env)
return function(...) return function(...)
if quota then
local timeout = function() local timeout = function()
cleanup() cleanup()
error('Quota exceeded: ' .. tostring(quota)) error('Quota exceeded: ' .. tostring(quota))
end end
sethook(timeout, "", quota) sethook(timeout, "", quota)
end
string.rep = nil string.rep = nil
local ok, result = pcall(f, ...) local ok, result = pcall(f, ...)

View File

@ -83,6 +83,10 @@ describe('sandbox.run', function()
assert_error(function() sandbox.run("for i=1,100 do end", {quota = 20}) end) assert_error(function() sandbox.run("for i=1,100 do end", {quota = 20}) end)
end) end)
it('does not use quotes if the quote param is false', function()
assert_not_error(function() sandbox.run("for i=1,1000000 do end", {quota = false}) end)
end)
end) end)