Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe version of pcall and error function to luacontroller environment #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions mesecons_luacontroller/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,25 @@ end
local function safe_string_find(...)
if (select(4, ...)) ~= true then
debug.sethook() -- Clear hook
error("string.find: 'plain' (fourth parameter) must always be true in a Luacontroller")
error("string.find: 'plain' (fourth parameter) must always be true in a Luacontroller", 2)
end

return string.find(...)
end

-- pcall can capture certain errors after the debug hook is removed.
-- Detect these errors, and propagate them.
local function safe_pcall(f, ...)
local values = {pcall(f, ...)}
local ok, err = values[1], values[2]

if not debug.gethook() then
error(string.gsub(err, "%(load%)%:.+:%s", ""), 2)
end

return unpack(values)
end

local function remove_functions(x)
local tp = type(x)
if tp == "function" then
Expand Down Expand Up @@ -430,7 +443,7 @@ end
local safe_globals = {
-- Don't add pcall/xpcall unless willing to deal with the consequences (unless very careful, incredibly likely to allow killing server indirectly)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-- Don't add pcall/xpcall unless willing to deal with the consequences (unless very careful, incredibly likely to allow killing server indirectly)

"assert", "error", "ipairs", "next", "pairs", "select",
"tonumber", "tostring", "type", "unpack", "_VERSION"
"tonumber", "tostring", "type", "unpack", "_VERSION", "error"
}

local function create_environment(pos, mem, event, itbl, send_warning)
Expand All @@ -452,6 +465,7 @@ local function create_environment(pos, mem, event, itbl, send_warning)
print = safe_print,
interrupt = get_interrupt(pos, itbl, send_warning),
digiline_send = get_digiline_send(pos, itbl, send_warning),
pcall = safe_pcall,
string = {
byte = string.byte,
char = string.char,
Expand Down