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

Conversation

rdococ
Copy link

@rdococ rdococ commented May 2, 2020

Currently propagates all errors generated by the luacontroller code itself (e.g. string length overflow), which might not be the desired behavior, but can be fixed if necessary.

I'm not sure if anyone actually wants this feature, but it could be useful for certain forms of non-local control flow, and judging by the comment I saw by the safe_globals definition, it seems like there was some interest in it.

@@ -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)

@TurkeyMcMac
Copy link
Contributor

I suggest you write safe_pcall something like this (I haven't tested the code):

local function finish_safe_pcall(...)
	local ok, err = ...

	if not ok and not debug.gethook() then
		error(err, 0)
	end

	return ...
end

local function safe_pcall(f, ...)
	return finish_safe_pcall(pcall(f, ...))
end

This handles nil return values correctly.

Calling error with 0 as its second argument throws the given message without modifying it.

@Bituvo
Copy link

Bituvo commented Nov 13, 2023

I support this! I would not like to manually check inputs for errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants