lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


	Hi Eva

> I want to execute a little script from another script using loadfile(). Because 
> of the fact that I cannot make sure that this script doesn't contain code I 
> don't want to be executed (like os.exit() or something) I tried to just allow 
> certain functions using an environment like that:
> 
> 
>     -- create local environment for loadstring()
>     local fenv = {}
>     fenv.print = _G.print
> 
>     setfenv (loadstring, fenv)
> 
>     result, error = loadstring (s)
> 
> With this environment it should be possible to just call the printing function 
> within the chunk to be loaded. Maybe this approach is too naive -  Lua forbits 
> changing the environment for the function loadstring() ;-(
	From the Reference Manual:

"setfenv (f, table)

Sets the environment to be used by the given function. f can be a Lua function or a number that specifies the function at that stack level: Level 1 is the function calling setfenv. setfenv returns the given function.

As a special case, when f is 0 setfenv changes the environment of the running thread. In this case, setfenv returns no values."

	You can add "setfenv(0, {print=print}\n" to your string.  This
will change the environment on-the-fly!
	Or you could also change the environment of the resulting function:

-- untested!
local f = assert (loadstring (s))
setfenv (f, fenv)

> Does anybody have some idea how I can force Lua to allow the executing of some 
> special functions and to prohibit other functions at the same time?
> 
> thanks for help!
	Have you checked VEnv (http://www.keplerproject.org/venv) ?
		Tomás