lua-users home
lua-l archive

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


>While string compiled with loadstring() is evaluated in a safe sandbox,
>function that are called from that chunk could freely refer to globals,
>like os.execute. Right?

No. See the program below, which is what I think you're trying to achieve.
Note that a simple "return" suffices in loadstring. (In production code,
you have to check whether loadstring retuns nil, of course.)
--lhf

 function sandbox(env, f, ...)
  local g = getfenv(f)
  setfenv(f, env)
  local retval = f(unpack(arg))
  setfenv(f, g)
  return retval
 end

 function run(B,s)
  local f=loadstring("return "..s)
  print(sandbox(B, f))
 end

 DATE=10
 B={ DATE=20, print=print, }
 run(B,"DATE")
 run(B,"os.execute'date'")