From bf995029babeca5bb9486b977eda0e1759ea7892 Mon Sep 17 00:00:00 2001 From: kikito Date: Sat, 14 Sep 2013 12:54:49 +0200 Subject: [PATCH] passing false as a quota deactivates the hooks --- sandbox.lua | 17 ++++++++++++----- spec/sandbox_spec.lua | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/sandbox.lua b/sandbox.lua index ff0a8d4..b138db7 100644 --- a/sandbox.lua +++ b/sandbox.lua @@ -120,19 +120,26 @@ function sandbox.protect(f, options) 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) env._G = env._G or env setfenv(f, env) return function(...) - local timeout = function() - cleanup() - error('Quota exceeded: ' .. tostring(quota)) + + if quota then + local timeout = function() + cleanup() + error('Quota exceeded: ' .. tostring(quota)) + end + sethook(timeout, "", quota) end - sethook(timeout, "", quota) string.rep = nil local ok, result = pcall(f, ...) diff --git a/spec/sandbox_spec.lua b/spec/sandbox_spec.lua index e81e819..7191ed5 100644 --- a/spec/sandbox_spec.lua +++ b/spec/sandbox_spec.lua @@ -83,6 +83,10 @@ describe('sandbox.run', function() assert_error(function() sandbox.run("for i=1,100 do end", {quota = 20}) 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)