diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-05-06 23:23:56 +0200 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-05-06 23:23:56 +0200 |
| commit | 01d0fe59935c8465a92a2508049f58292545a99f (patch) | |
| tree | 110d751669866f173e31d07aca1522f79518c31f | |
| parent | 777bfcd9ba4453ad610ca7cf4c23c0de05387045 (diff) | |
| download | frippy-01d0fe59935c8465a92a2508049f58292545a99f.tar.gz frippy-01d0fe59935c8465a92a2508049f58292545a99f.zip | |
Factoids: Add memory usage check to lua
| -rw-r--r-- | src/plugins/factoids/sandbox.lua | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/plugins/factoids/sandbox.lua b/src/plugins/factoids/sandbox.lua index 59e6d9e..2da45e7 100644 --- a/src/plugins/factoids/sandbox.lua +++ b/src/plugins/factoids/sandbox.lua @@ -90,7 +90,7 @@ sandbox_env.eval = eval sandbox_env.sleep = safesleep -- Check if the factoid timed out -function checktime(event, line) +function checktime() if os.time() - time >= timeout then error("Timed out after " .. timeout .. " seconds", 0) else @@ -99,12 +99,24 @@ function checktime(event, line) end end +-- Check if the factoid uses too much memory +function checkmem() + if collectgarbage("count") > maxmem then + error("Factoid used over " .. maxmem .. " kbyte of ram") + end +end + local f, e = load(factoid, nil, nil, sandbox_env) -- Add timeout hook time = os.time() +-- The timeout is defined in seconds timeout = 30 debug.sethook(checktime, "l") +-- Add memory check hook +-- The max memory is defined in kilobytes +maxmem = 1000 +debug.sethook(checkmem, "l") if f then f() |
